forked from liuyubobobo/Play-Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bda2b14
commit d155572
Showing
4 changed files
with
91 additions
and
30 deletions.
There are no files selected for viewing
7 changes: 3 additions & 4 deletions
7
0309-Best-Time-to-Buy-and-Sell-Stock-with-Cooldown/cpp-0309/CMakeLists.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,6 @@ | ||
cmake_minimum_required(VERSION 3.5) | ||
cmake_minimum_required(VERSION 3.14) | ||
project(cpp_0309) | ||
|
||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") | ||
set(CMAKE_CXX_STANDARD 14) | ||
|
||
set(SOURCE_FILES main2.cpp) | ||
add_executable(cpp_0309 ${SOURCE_FILES}) | ||
add_executable(cpp_0309 main.cpp) |
48 changes: 32 additions & 16 deletions
48
0309-Best-Time-to-Buy-and-Sell-Stock-with-Cooldown/cpp-0309/main.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 14 additions & 10 deletions
24
0309-Best-Time-to-Buy-and-Sell-Stock-with-Cooldown/cpp-0309/main2.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
0309-Best-Time-to-Buy-and-Sell-Stock-with-Cooldown/cpp-0309/main3.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/// Source : https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-cooldown/description/ | ||
/// Author : liuyubobobo | ||
/// Time : 2017-10-24 | ||
|
||
#include <iostream> | ||
#include <vector> | ||
|
||
using namespace std; | ||
|
||
|
||
/// Dynamic Programming with Space Optimization | ||
/// Time Complexity: O(n) | ||
/// Space Complexity: O(1) | ||
class Solution { | ||
public: | ||
int maxProfit(vector<int>& prices) { | ||
|
||
int n = prices.size(); | ||
|
||
if(n <= 1) | ||
return 0; | ||
|
||
int buy[3] = {-prices[0], max(-prices[0], -prices[1]), 0}; | ||
int sell[3] = {0, max(0, prices[1] - prices[0]), 0}; | ||
|
||
for(int i = 2 ; i < n ; i ++){ | ||
sell[i % 3] = max(sell[(i - 1) % 3], buy[(i - 1) % 3] + prices[i]); | ||
buy[i % 3] = max(buy[(i - 1) % 3], sell[(i - 2) % 3] - prices[i]); | ||
} | ||
|
||
return cash[(n - 1) % 3]; | ||
} | ||
}; | ||
|
||
|
||
int main() { | ||
|
||
vector<int> prices1 = {1, 2, 3, 0, 2}; | ||
cout << Solution().maxProfit(vec1) << endl; | ||
|
||
return 0; | ||
} |