123. Best Time to Buy and Sell Stock III Hard

@problem@discussion
#Array#Dynamic Programming



1/**
2 * [123] Best Time to Buy and Sell Stock III
3 *
4 * You are given an array prices where prices[i] is the price of a given stock on the i^th day.
5 * Find the maximum profit you can achieve. You may complete at most two 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: prices = [3,3,5,0,0,3,1,4]
11 * Output: 6
12 * Explanation: Buy on day 4 (price = 0) and sell on day 6 (price = 3), profit = 3-0 = 3.
13 * Then buy on day 7 (price = 1) and sell on day 8 (price = 4), profit = 4-1 = 3.
14 * Example 2:
15 * 
16 * Input: prices = [1,2,3,4,5]
17 * Output: 4
18 * Explanation: Buy on day 1 (price = 1) and sell on day 5 (price = 5), profit = 5-1 = 4.
19 * Note that you cannot buy on day 1, buy on day 2 and sell them later, as you are engaging multiple transactions at the same time. You must sell before buying again.
20 * 
21 * Example 3:
22 * 
23 * Input: prices = [7,6,4,3,1]
24 * Output: 0
25 * Explanation: In this case, no transaction is done, i.e. max profit = 0.
26 * 
27 *  
28 * Constraints:
29 * 
30 * 	1 <= prices.length <= 10^5
31 * 	0 <= prices[i] <= 10^5
32 * 
33 */
34pub struct Solution {}
35
36// problem: https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iii/
37// discuss: https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iii/discuss/?currentPage=1&orderBy=most_votes&query=
38
39// submission codes start here
40
41impl Solution {
42    pub fn max_profit(prices: Vec<i32>) -> i32 {
43        0
44    }
45}
46
47// submission codes end
48
49#[cfg(test)]
50mod tests {
51    use super::*;
52
53    #[test]
54    fn test_123() {
55    }
56}
57


Back
© 2025 bowen.ge All Rights Reserved.