Skip to content

Latest commit

 

History

History
23 lines (13 loc) · 537 Bytes

File metadata and controls

23 lines (13 loc) · 537 Bytes

Problem 188: Best Time to Buy and Sell Stock IV

Difficulty: Hard

Problem

Say you have an array for which the ith element is the price of a given stock on day i.

Design an algorithm to find the maximum profit. You may complete at most k transactions.

Note: You may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).

Example

Input: [2,4,1], k = 2

Output: 2

Explanation: Buy on day 1 (price = 2) and sell on day 2 (price = 4), profit = 4-2 = 2.