best-time-to-buy-and-sell-stock-ii
1.0.0
Best Time to Buy and Sell Stock II
Loading...
Searching...
No Matches
main.cpp
Go to the documentation of this file.
1
#include <bits/stdc++.h>
2
3
using namespace
std;
4
5
class
Solution
6
{
7
public
:
8
int
maxProfit
(vector<int>& prices)
9
{
10
int
buy_price = prices[0];
11
int
profit = 0;
12
for
(
int
price: prices)
13
{
14
if
(price > buy_price)
15
{
16
profit += price - buy_price;
17
buy_price = price;
18
}
19
if
(price < buy_price)
20
{
21
buy_price = price;
22
}
23
}
24
return
profit;
25
}
26
};
27
28
int
main
()
29
{
30
vector<int> prices = { 7,1,5,3,6,4 };
// expected profit: 7
31
cout <<
Solution
().
maxProfit
(prices) << endl;
32
return
0;
33
}
Solution
Definition
main.cpp:6
Solution::maxProfit
int maxProfit(vector< int > &prices)
Definition
main.cpp:8
main
int main()
Definition
main.cpp:28
main.cpp
Generated by
1.9.8