|
| 1 | +/** |
| 2 | + * [122] Best Time to Buy and Sell Stock II |
| 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 as many transactions as you like (i.e., buy one and sell one share of the stock multiple times). |
| 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 = [7,1,5,3,6,4] |
| 11 | + * Output: 7 |
| 12 | + * Explanation: Buy on day 2 (price = 1) and sell on day 3 (price = 5), profit = 5-1 = 4. |
| 13 | + * Then buy on day 4 (price = 3) and sell on day 5 (price = 6), profit = 6-3 = 3. |
| 14 | + * |
| 15 | + * Example 2: |
| 16 | + * |
| 17 | + * Input: prices = [1,2,3,4,5] |
| 18 | + * Output: 4 |
| 19 | + * Explanation: Buy on day 1 (price = 1) and sell on day 5 (price = 5), profit = 5-1 = 4. |
| 20 | + * 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. |
| 21 | + * |
| 22 | + * Example 3: |
| 23 | + * |
| 24 | + * Input: prices = [7,6,4,3,1] |
| 25 | + * Output: 0 |
| 26 | + * Explanation: In this case, no transaction is done, i.e., max profit = 0. |
| 27 | + * |
| 28 | + * |
| 29 | + * Constraints: |
| 30 | + * |
| 31 | + * 1 <= prices.length <= 3 * 10^4 |
| 32 | + * 0 <= prices[i] <= 10^4 |
| 33 | + * |
| 34 | + */ |
| 35 | +pub struct Solution {} |
| 36 | + |
| 37 | +// problem: https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/ |
| 38 | +// discuss: https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/discuss/?currentPage=1&orderBy=most_votes&query= |
| 39 | + |
| 40 | +// submission codes start here |
| 41 | + |
| 42 | +impl Solution { |
| 43 | + pub fn max_profit(prices: Vec<i32>) -> i32 { |
| 44 | + let mut last_no_stock_balance : i32 = 0; |
| 45 | + let mut last_with_stock_balance : i32 = -2_147_483_648i32; |
| 46 | + |
| 47 | + for price in prices.iter() { |
| 48 | + let last_no_stock_balance_cache = last_no_stock_balance; |
| 49 | + last_no_stock_balance = std::cmp::max(last_no_stock_balance, last_with_stock_balance + price); |
| 50 | + |
| 51 | + last_with_stock_balance = std::cmp::max(last_with_stock_balance, last_no_stock_balance_cache - price) |
| 52 | + } |
| 53 | + last_no_stock_balance |
| 54 | + } |
| 55 | + |
| 56 | + pub fn max_profit1(prices: Vec<i32>) -> i32 { |
| 57 | + let n : usize = prices.len(); |
| 58 | + if n == 0 || n == 1 {return 0;} |
| 59 | + let mut local_mins : Vec<i32> = vec![]; |
| 60 | + let mut local_maxs : Vec<i32> = vec![]; |
| 61 | + |
| 62 | + let is_local_min = |i|{ |
| 63 | + if i==0 { |
| 64 | + prices[i] <= prices[i+1] |
| 65 | + } else if i == n-1 { |
| 66 | + false |
| 67 | + } else { |
| 68 | + prices[i-1] >= prices[i] && prices[i] <= prices[i+1] |
| 69 | + } |
| 70 | + }; |
| 71 | + |
| 72 | + let is_local_max = |i|{ |
| 73 | + if i==0 { |
| 74 | + false |
| 75 | + } else if i == n-1 { |
| 76 | + prices[i-1] <= prices[i] |
| 77 | + } else { |
| 78 | + prices[i-1] <= prices[i] && prices[i] >= prices[i+1] |
| 79 | + } |
| 80 | + }; |
| 81 | + let mut i : usize = 0; |
| 82 | + loop { |
| 83 | + let mut cur_local_min : i32 = 0; |
| 84 | + let mut cur_local_max : i32 = 0; |
| 85 | + while i < n && !is_local_min(i) { |
| 86 | + i+=1; |
| 87 | + } |
| 88 | + if i == n { break;} |
| 89 | + cur_local_min = prices[i]; |
| 90 | + |
| 91 | + i+=1; |
| 92 | + while i < n && !is_local_max(i) { |
| 93 | + i+=1; |
| 94 | + } |
| 95 | + if i == n { break;} |
| 96 | + cur_local_max = prices[i]; |
| 97 | + |
| 98 | + local_mins.push(cur_local_min); |
| 99 | + local_maxs.push(cur_local_max); |
| 100 | + } |
| 101 | + // println!("local_mins={:?}, local_maxs={:?}", local_mins, local_maxs); |
| 102 | + let mut sum : i32 = 0; |
| 103 | + for i in 0..local_maxs.len() { |
| 104 | + sum += local_maxs[i] - local_mins[i]; |
| 105 | + } |
| 106 | + sum |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +// submission codes end |
| 111 | + |
| 112 | +#[cfg(test)] |
| 113 | +mod tests { |
| 114 | + use super::*; |
| 115 | + |
| 116 | + #[test] |
| 117 | + fn test_122() { |
| 118 | + assert_eq!(Solution::max_profit(vec![7, 1, 5, 3, 6, 4]), 7); |
| 119 | + assert_eq!(Solution::max_profit(vec![1, 2, 3, 4, 5]), 4); |
| 120 | + assert_eq!(Solution::max_profit(vec![2, 2, 5]), 3); |
| 121 | + } |
| 122 | +} |
0 commit comments