1178. Best Time to Buy and Sell Stock IV — At Most K

MediumDynamic ProgrammingStock DP

Given daily stock prices and an integer k, you may complete at most k transactions (non-overlapping). Return the maximum profit. The input is JSON {prices, k}.

Input: JSON {prices, k}.

Output: Integer — the maximum profit.

Examples

Example 1
Input: {"prices":[2,4,1],"k":2}
Output: 2
Explanation: One transaction profits 2.
Example 2
Input: {"prices":[3,2,6,5,0,3],"k":2}
Output: 7
Explanation: Two transactions profit 4 and 3.
Example 3
Input: {"prices":[1],"k":0}
Output: 0
Explanation: No transactions allowed.

Constraints

Asked by

AmazonMicrosoftGoogleAdobeFlipkart
Solve this problem in the editor →