Skip to content

Practice Best Time to Buy and Sell Stock IV

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Hard#188
LeetCode Problem

Best Time to Buy and Sell Stock IV

Problem Statement

You are given an integer array prices where prices[i] is the price of a given stock on the ith day, and an integer k. Find the maximum profit you can achieve. You may complete at most k transactions: i.e. you may buy at most k times and sell at most k times. Note: You may not engage in multiple transactions simultaneously (i.e., you must sell the stock before you buy again).

Source: Best Time to Buy and Sell Stock IV on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
k = 2, prices = [2,4,1]
Output
2

# Example 2

Input
k = 2, prices = [3,2,6,5,0,3]
Output
7

Constraints

1 <= k <= 100
1 <= prices.length <= 1000
0 <= prices[i] <= 1000

Related LeetCode Problems

Practice more problems similar to Best Time to Buy and Sell Stock IV.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.