Limited time: AI code review, hints, mock interviews, whiteboard analysis, and all Pro features are unlocked. Enroll

Best Time to Buy and Sell Stock IV

Hard

Topics
ArrayDynamic Programming

You are given an integer array nums where nums[i] is the price of a stock on day i, and an integer k. Return the maximum profit achievable with at most k transactions. You may not hold more than one share at a time.

Example 1

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

Example 2

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

Constraints

  • 0 <= k <= 100
  • 1 <= nums.length <= 1000
  • 0 <= nums[i] <= 1000
Run ⌘' · Submit ⌘⏎
SensAI SensAI
Beta
Listening...
Tap mic to stop voice mode

Unlock AI Features

One click to activate - no payment, no credit card. Just sign in and you're in.

AI code review and hints
SensAI chat assistant
AI mock interviews
Whiteboard analysis
100% free during early access