-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path122_Best_Time_to_Buy_and_Sell_Stock_II.cpp
56 lines (56 loc) · 1.41 KB
/
122_Best_Time_to_Buy_and_Sell_Stock_II.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include<iostream>
#include<vector>
#include<map>
using namespace std;
static const auto x=[](){
std::ios::sync_with_stdio(false);
std:cin.tie(nullptr);
return nullptr;
}();
class Solution
{
public:
int maxProfit(vector<int>& prices)
{
int i = 0;
int p_size=prices.size();
int profit=0;
while(i < p_size)
{
for(; i < p_size-1;i++)
{
if(prices[i] >= prices[i+1])
continue;
else
break;
}
if( i == (p_size-1) and prices[i] >= prices[i+1] )
{
break;
}
else//i < p_size-1 or (i == p_size-1 and ascending)
// then there must exist one transaction
{
int buy_price = prices[i++];
for(; i < p_size;i++)
{
if(prices[i] > prices[i-1])
continue;
else
break;
}
profit += (prices[i-1] - buy_price);
}
}
return profit;
}
};
int main(int argc, char const *argv[])
{
Solution sol;
vector<int> prices={7,1,5,3,6,4};
vector<int> prices2={1,2,3,4,5};
cout<<sol.maxProfit(prices)<<endl;
cout<<sol.maxProfit(prices2)<<endl;
return 0;
}