Skip to content

Practice Best Time to Buy and Sell Stock V

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

Medium#3573
LeetCode Problem

Best Time to Buy and Sell Stock V

Problem Statement

Given daily prices and k, complete at most k transactions. A normal transaction buys then sells on a later day for the price increase; a short transaction sells then buys back on a later day for the price decrease. Transactions cannot overlap, and a new one cannot start on the day the previous one ends. Return the maximum total profit.

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

Examples

# Example 1

Input
prices = [1,7,9,8,2], k = 2
Output
14

# Example 2

Input
prices = [12,16,19,19,8,1,19,13,9], k = 3
Output
36

Constraints

2 <= prices.length <= 10^3
1 <= prices[i] <= 10^9
1 <= k <= prices.length / 2

Related LeetCode Problems

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

Review your practice with PhantomCodeAI

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