-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBest_Time_to_Buy_and_Sell_Stock_II.cpp
105 lines (84 loc) · 2.32 KB
/
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/*
Problem:
Say you have an array for which the ith element is the price of a given stock on day i.
Design an algorithm to find the maximum profit. You may complete as many transactions as you like (ie, buy one and sell one share of the stock multiple times). However, you may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).
*/
#include<iostream>
#include<vector>
using namespace std;
int maxProfit(vector<int> &prices)
{
if (prices.size() <= 1) return 0;
prices.push_back(0);
int a = 0;
int b = 0;
int c = 0;
bool buy = false;
bool sell = false;
int min = 0;
if (prices[0] < prices[1])
{
min = prices[0];
buy = true;
}
int max = 0;
int profit = 0;
size_t i = 0;
while (i < prices.size()-2)
{
//cout << "i'm" << endl;
a = prices[i];
b = prices[i+1];
c = prices[i+2];
if (a >= b && b <= c && sell == false)
{
//cout << "buy" << endl;
min = b;
buy = true;
}
else if (b >= a && b >= c && buy == true)
{
//cout << "sell" << endl;
max = b;
sell = true;
}
if (buy == true && sell == true)
{
profit += max - min;
//cout << profit << endl;
sell = false;
buy = false;
}
i++;
}
if (c >= b) profit += c-min;
return profit;
}
int main()
{
vector<int> result;
result.push_back(2);
result.push_back(2);
result.push_back(5);
//result.push_back(1);
int test;
test = maxProfit(result);
cout << test << endl;
return 0;
}
//Another Method
class Solution {
public:
int maxProfit(vector<int> &prices) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
int p = 0;
for(int i = 1; i < prices.size() ; ++i) {
int delta = prices[i] - prices[i-1];
if(delta > 0 ) {
p += delta;
}
}
return p;
}
};