Skip to content

Commit d155572

Browse files
committed
309 codes updated.
1 parent bda2b14 commit d155572

File tree

4 files changed

+91
-30
lines changed

4 files changed

+91
-30
lines changed
Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
cmake_minimum_required(VERSION 3.5)
1+
cmake_minimum_required(VERSION 3.14)
22
project(cpp_0309)
33

4-
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
4+
set(CMAKE_CXX_STANDARD 14)
55

6-
set(SOURCE_FILES main2.cpp)
7-
add_executable(cpp_0309 ${SOURCE_FILES})
6+
add_executable(cpp_0309 main.cpp)

0309-Best-Time-to-Buy-and-Sell-Stock-with-Cooldown/cpp-0309/main.cpp

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,57 @@
11
/// Source : https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-cooldown/description/
22
/// Author : liuyubobobo
3-
/// Time : 2017-10-23
3+
/// Time : 2019-06-27
44

55
#include <iostream>
66
#include <vector>
77

88
using namespace std;
99

10-
/// Using hold and cash to trace the max money in different state
10+
11+
/// Memory Search
1112
/// Time Complexity: O(n)
1213
/// Space Complexity: O(n)
1314
class Solution {
15+
16+
private:
17+
int n;
18+
1419
public:
1520
int maxProfit(vector<int>& prices) {
1621

17-
if(prices.size() <= 1)
18-
return 0;
22+
n = prices.size();
23+
if(n <= 1) return 0;
24+
25+
vector<int> buy(n, -1), sell(n, -1);
26+
return _sell(prices, n - 1, buy, sell);
27+
}
28+
29+
private:
30+
int _sell(const vector<int>& prices, int index, vector<int>& buy, vector<int>& sell){
31+
32+
if(index == 0) return 0;
33+
if(index == 1) return max(0, prices[1] - prices[0]);
1934

20-
vector<int> hold(prices.size(), INT_MIN);
21-
vector<int> cash(prices.size(), 0);
35+
if(sell[index] != -1) return sell[index];
36+
return sell[index] = max(_sell(prices, index - 1, buy, sell),
37+
_buy(prices, index - 1, buy, sell) + prices[index]);
38+
}
2239

23-
hold[0] = -prices[0];
24-
hold[1] = max(hold[0], -prices[1]);
25-
cash[1] = max(cash[0], hold[0] + prices[1]);
26-
for(int i = 2 ; i < prices.size() ; i ++){
27-
cash[i] = max(cash[i-1], hold[i-1] + prices[i]);
28-
hold[i] = max(hold[i-1], cash[i-2] - prices[i]);
29-
}
40+
int _buy(const vector<int>& prices, int index, vector<int>& buy, vector<int>& sell){
3041

31-
return cash.back();
42+
if(index == 0) return -prices[0];
43+
if(index == 1) return max(-prices[0], -prices[1]);
44+
45+
if(buy[index] != -1) return buy[index];
46+
return buy[index] = max(_buy(prices, index - 1, buy, sell),
47+
_sell(prices, index - 2, buy, sell) - prices[index]);
3248
}
3349
};
3450

51+
3552
int main() {
3653

37-
int prices1[] = {1, 2, 3, 0, 2};
38-
vector<int> vec1(prices1, prices1 + sizeof(prices1)/sizeof(int));
54+
vector<int> prices1 = {1, 2, 3, 0, 2};
3955
cout << Solution().maxProfit(vec1) << endl;
4056

4157
return 0;

0309-Best-Time-to-Buy-and-Sell-Stock-with-Cooldown/cpp-0309/main2.cpp

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,42 @@
11
/// Source : https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-cooldown/description/
22
/// Author : liuyubobobo
3-
/// Time : 2017-10-24
3+
/// Time : 2017-10-23
44

55
#include <iostream>
66
#include <vector>
77

88
using namespace std;
99

10-
/// Using hold and cash to trace the max money in different state
10+
11+
/// Dynamic Programming
1112
/// Time Complexity: O(n)
12-
/// Space Complexity: O(1)
13+
/// Space Complexity: O(n)
1314
class Solution {
1415
public:
1516
int maxProfit(vector<int>& prices) {
1617

1718
if(prices.size() <= 1)
1819
return 0;
1920

20-
int hold[3] = {-prices[0], max(hold[0], -prices[1]), INT_MIN};
21-
int cash[3] = {0, max(cash[0], hold[0] + prices[1]), 0};
21+
vector<int> buy(prices.size(), 0);
22+
vector<int> sell(prices.size(), 0);
2223

24+
buy[0] = -prices[0];
25+
buy[1] = max(-prices[0], -prices[1]);
26+
sell[1] = max(0, buy[0] + prices[1]);
2327
for(int i = 2 ; i < prices.size() ; i ++){
24-
cash[i%3] = max(cash[(i-1)%3], hold[(i-1)%3] + prices[i]);
25-
hold[i%3] = max(hold[(i-1)%3], cash[(i-2)%3] - prices[i]);
28+
sell[i] = max(sell[i-1], buy[i-1] + prices[i]);
29+
buy[i] = max(buy[i-1], sell[i-2] - prices[i]);
2630
}
2731

28-
return cash[(prices.size()-1)%3];
32+
return sell.back();
2933
}
3034
};
3135

36+
3237
int main() {
3338

34-
int prices1[] = {1, 2, 3, 0, 2};
35-
vector<int> vec1(prices1, prices1 + sizeof(prices1)/sizeof(int));
39+
vector<int> prices1 = {1, 2, 3, 0, 2};
3640
cout << Solution().maxProfit(vec1) << endl;
3741

3842
return 0;
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/// Source : https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-cooldown/description/
2+
/// Author : liuyubobobo
3+
/// Time : 2017-10-24
4+
5+
#include <iostream>
6+
#include <vector>
7+
8+
using namespace std;
9+
10+
11+
/// Dynamic Programming with Space Optimization
12+
/// Time Complexity: O(n)
13+
/// Space Complexity: O(1)
14+
class Solution {
15+
public:
16+
int maxProfit(vector<int>& prices) {
17+
18+
int n = prices.size();
19+
20+
if(n <= 1)
21+
return 0;
22+
23+
int buy[3] = {-prices[0], max(-prices[0], -prices[1]), 0};
24+
int sell[3] = {0, max(0, prices[1] - prices[0]), 0};
25+
26+
for(int i = 2 ; i < n ; i ++){
27+
sell[i % 3] = max(sell[(i - 1) % 3], buy[(i - 1) % 3] + prices[i]);
28+
buy[i % 3] = max(buy[(i - 1) % 3], sell[(i - 2) % 3] - prices[i]);
29+
}
30+
31+
return cash[(n - 1) % 3];
32+
}
33+
};
34+
35+
36+
int main() {
37+
38+
vector<int> prices1 = {1, 2, 3, 0, 2};
39+
cout << Solution().maxProfit(vec1) << endl;
40+
41+
return 0;
42+
}

0 commit comments

Comments
 (0)