Skip to content

Commit dd73cfd

Browse files
committed
review stock trading series
1 parent 007c99d commit dd73cfd

8 files changed

+471
-3
lines changed

Basic.md

Lines changed: 56 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -198,13 +198,66 @@ In [1,3,0,2,4] or [1,3,5,0,2,4], every consecutive 3 are not adjacent.
198198
* [1424. Diagonal Traverse II](src/problem/p1424_diagonal_traverse_ii.rs)
199199

200200
## Bit Operation
201-
## 2 Pointer
201+
### Basics
202+
* [190. Reverse Bits](src/problem/p0190_reverse_bits.rs) (Repeatedly test and set)
203+
* [191. Number of 1 Bits](src/problem/p0191_number_of_1_bits.rs) (Use x&=x-1 to repeatively unset the least significant 1-bit. )
204+
* [693. Binary Number with Alternating Bits](src/problem/p0693_binary_number_with_alternating_bits.rs) (Use x&(x+1)==0 to check whether x=(2^n-1))
205+
* [137. Single Number II](src/problem/p0260_single_number_ii.rs) (Find the single unique elements that appear k times, while the rest with n times. )
206+
* Implement a cyclic counter with period n for each bit when xoring elements
207+
* Return i-th bit counter if the i-th bit is set in binary k.
208+
* [260. Single Number III](src/problem/p0260_single_number_iii.rs) (Find the two unique elements, (x,y) that appear k (k is odd) times, while the rest with n times. )
209+
* Implement a cyclic counter with period n for each bit when xoring elements
210+
* Locate any i-th bit counter, which is none-zero.(x,y differs in i-th bit).
211+
* Separate all elements into two groups by i-th bit and redo p137 to discovery x and y.
212+
* [29. Divide Two Integers](src/problem/p0029_divide_two_integers.rs)
213+
* [371. Sum of Two Integers](src/problem/p0371_sum_of_two_integers.rs)
214+
* [201. Bitwise AND of Numbers Range](src/problem/p0201_bitwise_and_of_numbers_range.rs)
215+
216+
217+
## Two-Pointer
202218
## Array/String
203219
## DP
220+
## Series
221+
### Palindrome
222+
```
223+
NOTE: DP recursion for palindrome
224+
is_palindrome(i,j) |= is_palindrome(k,j-1) && s[k-1] == s[j] for any k
225+
```
226+
227+
* [5. Longest Palindromic Substring](src/problem/p0005_longest_palindromic_substring.rs)
228+
### Stocks Trading
229+
Key recursion:
230+
```
231+
// no_stock_balances[i][k] represents the max balance at the end of i-th day with at most k txns, conditioned on no stock hold
232+
// with_stock_balances[i][k] represents the max balance at the end of i-th day with at most k txns, conditioned on stocks holded
233+
no_stock_balances[i<=0][*]=0; // initial condition
234+
no_stock_balances[*][k=0]=0; // initial condition
235+
with_stock_balances[*][k=0]=MIN; // imply N.A. to work with the below max operation
236+
237+
// sell
238+
no_stock_balances[i][k] = max(no_stock_balances[i-1][k], with_stock_balances[i-1][k] + prices[i])
239+
// buy
240+
with_stock_balances[i][k] = max(with_stock_balances[i-1][k], no_stock_balances[i-1][k-1] - prices[i])
241+
```
242+
243+
* [121. Best Time to Buy and Sell Stock](src/problem/p0121_best_time_to_buy_and_sell_stock.rs) (k=1)
244+
* Enumerate k dimension with named variables.
245+
* Always replace no_stock_balances[i-1][k-1] with 0 as k=1
246+
* [122. Best Time to Buy and Sell Stock II](src/problem/p0122_best_time_to_buy_and_sell_stock_ii.rs) (k=inf)
247+
* Due inf, no_stock_balances[\*][k]=no_stock_balances[\*][k-1], and similar to with_stock_balances. Hence, the dimension for k can be removed.
248+
* [188. Best Time to Buy and Sell Stock IV](src/problem/p0188_best_time_to_buy_and_sell_stock_iv.rs) (Arbitrary k)
249+
* Minor Optimization: since a txn must span at least two days, one day to buy and one day to sell, when k >n/2, it is equivalent to k = inf.
250+
* [714. Best Time to Buy and Sell Stock with Transaction Fee](src/problem/p0714_best_time_to_buy_and_sell_stock_with_transaction_fee.rs)
251+
* Similar to [P122](src/problem/p0122_best_time_to_buy_and_sell_stock_ii.rs).
252+
* Can pay the fee either during the buy or the sell.
253+
* [309. Best Time to Buy and Sell Stock with Cooldown](src/problem/p0309_best_time_to_buy_and_sell_stock_with_cooldown.rs) (cool down with inf k)
254+
* with_stock_balances[i][k] = max(with_stock_balances[i-1][k], no_stock_balances[**i-2**][k-1] - prices[i])
255+
256+
204257
## Others
258+
* [229. Majority Element II](src/problem/p0229_majority_element_ii.rs)
259+
* (B-M Majority Vote)
205260
* [42. Trapping Rain Water](src/problem/p0042_trapping_rain_water.rs)
206-
### Palindrome Series
207-
NOTE: DP recursion for palindrome
208261
# [Collected Template](src/problem/p0000_template.rs)
209262
* Data structure:
210263
* BtreeMap

src/problem/mod.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,3 +172,10 @@ mod p0212_word_search_ii;
172172
mod p0875_koko_eating_bananas;
173173
mod p0498_diagonal_traverse;
174174
mod p1424_diagonal_traverse_ii;
175+
mod p0191_number_of_1_bits;
176+
mod p0005_longest_palindromic_substring;
177+
mod p0214_shortest_palindrome;
178+
mod p0188_best_time_to_buy_and_sell_stock_iv;
179+
mod p0123_best_time_to_buy_and_sell_stock_iii;
180+
mod p0122_best_time_to_buy_and_sell_stock_ii;
181+
mod p0714_best_time_to_buy_and_sell_stock_with_transaction_fee;

src/problem/p0121_best_time_to_buy_and_sell_stock.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,17 @@ pub struct Solution {}
3434

3535
impl Solution {
3636
pub fn max_profit(prices: Vec<i32>) -> i32 {
37+
let mut last_no_stock_balance = 0i32;
38+
let mut last_with_stock_balance = -2_147_483_648i32;
39+
for &price in prices.iter() {
40+
last_no_stock_balance = std::cmp::max(last_no_stock_balance, last_with_stock_balance + price);
41+
42+
last_with_stock_balance = std::cmp::max(last_with_stock_balance, 0 - price);
43+
}
44+
last_no_stock_balance
45+
}
46+
47+
pub fn max_profit1(prices: Vec<i32>) -> i32 {
3748
// kadane's alg on consecutive diff.
3849
let mut max_so_far = 0;
3950
let mut cur_max = 0;
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
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+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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+
* Example 4:
28+
*
29+
* Input: prices = [1]
30+
* Output: 0
31+
*
32+
*
33+
* Constraints:
34+
*
35+
* 1 <= prices.length <= 10^5
36+
* 0 <= prices[i] <= 10^5
37+
*
38+
*/
39+
pub struct Solution {}
40+
41+
// problem: https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iii/
42+
// discuss: https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iii/discuss/?currentPage=1&orderBy=most_votes&query=
43+
44+
// submission codes start here
45+
46+
impl Solution {
47+
pub fn max_profit(prices: Vec<i32>) -> i32 {
48+
let n = prices.len();
49+
let mut balances : Vec<Vec<i32>> = vec![vec![0;n];3];
50+
for k in 1..=2 {
51+
let mut min = prices[0];
52+
for i in 1..n {
53+
min = std::cmp::min(min, prices[i]-balances[k-1][i-1]);
54+
balances[k][i] = std::cmp::max(balances[k][i-1], prices[i] - min);
55+
}
56+
}
57+
balances[2][n-1]
58+
}
59+
}
60+
61+
// submission codes end
62+
63+
#[cfg(test)]
64+
mod tests {
65+
use super::*;
66+
67+
#[test]
68+
fn test_123() {
69+
assert_eq!(Solution::max_profit(vec![3, 3, 5, 0, 0, 3, 1, 4]), 6);
70+
}
71+
}

0 commit comments

Comments
 (0)