LeetCode 188 - Best Time to Buy and Sell Stock IV
Difficulty: hard
Stock Series:
Part 1: LeetCode 121 - Best Time to Buy and Sell Stock
Part 2: LeetCode 122 - Best Time to Buy and Sell Stock II
Part 3: LeetCode 123 - Best Time to Buy and Sell Stock III
Part 4: LeetCode 188 - Best Time to Buy and Sell Stock IV
Part 5: LeetCode 309 - Best Time to Buy and Sell Stock with Cooldown
Part 6: LeetCode 714 - Best Time to Buy and Sell Stock with Transaction Fee
Problem Description
English (Best Time to Buy and Sell Stock IV)
You are given an integer array prices
where prices[i]
is the price of a given stock on the $i^{th}$ 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).
Example 1:
1 |
|
Example 2:
1 |
|
Constraints:
1 <= k <= 100
1 <= prices.length <= 1000
0 <= prices[i] <= 1000
Chinese (买卖股票的最佳时机 IV)
给定一个整数数组 prices
,它的第 i 个元素 prices[i]
是一支给定的股票在第 i
天的价格,和一个整型 k
。
设计一个算法来计算你所能获取的最大利润。你最多可以完成 k
笔交易。也就是说,你最多可以买 k
次,卖 k
次。
注意: 你不能同时参与多笔交易(你必须在再次购买前出售掉之前的股票)。
示例 1:
1 |
|
示例 2:
1 |
|
提示:
0 <= k <= 100
0 <= prices.length <= 1000
0 <= prices[i] <= 1000
Solution
C++
1 |
|