188. Best Time to Buy and Sell Stock IV Hard
1/**
2 * [188] Best Time to Buy and Sell Stock IV
3 *
4 * 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.
5 * Find the maximum profit you can achieve. You may complete at most k transactions.
6 * Note: You may not engage in multiple transactions simultaneously (i.e., you must sell the stock before you buy again).
7 *
8 * Example 1:
9 *
10 * Input: k = 2, prices = [2,4,1]
11 * Output: 2
12 * Explanation: Buy on day 1 (price = 2) and sell on day 2 (price = 4), profit = 4-2 = 2.
13 *
14 * Example 2:
15 *
16 * Input: k = 2, prices = [3,2,6,5,0,3]
17 * Output: 7
18 * Explanation: Buy on day 2 (price = 2) and sell on day 3 (price = 6), profit = 6-2 = 4. Then buy on day 5 (price = 0) and sell on day 6 (price = 3), profit = 3-0 = 3.
19 *
20 *
21 * Constraints:
22 *
23 * 1 <= k <= 100
24 * 1 <= prices.length <= 1000
25 * 0 <= prices[i] <= 1000
26 *
27 */
28pub struct Solution {}
29
30// problem: https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iv/
31// discuss: https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iv/discuss/?currentPage=1&orderBy=most_votes&query=
32
33// submission codes start here
34
35impl Solution {
36 pub fn max_profit(k: i32, prices: Vec<i32>) -> i32 {
37 0
38 }
39}
40
41// submission codes end
42
43#[cfg(test)]
44mod tests {
45 use super::*;
46
47 #[test]
48 fn test_188() {
49 }
50}
51
Back
© 2025 bowen.ge All Rights Reserved.