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