-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathleetcode121_best_time_to_buy_and_sell_stock.cpp
70 lines (59 loc) · 1.52 KB
/
leetcode121_best_time_to_buy_and_sell_stock.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
/**
* @file leetcode121_best_time_to_buy_and_sell_stock.cpp
* @author wangguibao(https://github.com/wangguibao)
* @date 2023/05/31 13:55
* @brief https://leetcode.com/problems/best-time-to-buy-and-sell-stock/
*
**/
#include <iostream>
#include <vector>
#include <climits>
using namespace std;
class Solution {
public:
int maxProfit(vector<int>& prices) {
size_t len = prices.size();
if (len <= 1) {
return 0;
}
int curMin = prices[0];
int curMax = prices[0];
int total = 0;
for (size_t i = 1; i < len; ++i) {
if (prices[i] < curMin) {
int profit = curMax - curMin;
if (profit > total) {
total = profit;
}
curMin = prices[i];
curMax = prices[i];
} else if (prices[i] > curMax) {
curMax = prices[i];
}
}
int profit = curMax - curMin;
if (total < profit) {
total = profit;
}
return total;
}
};
int main() {
while (1) {
std::cout << "Number of elements (control-C to exit): ";
int n = 0;
if (!(cin >> n)) {
return -1;
}
std::vector<int> prices;
int x;
for (size_t i = 0; i < n; ++i) {
cin >> x;
prices.emplace_back(x);
}
Solution solution;
auto ret = solution.maxProfit(prices);
std::cout << ret << std::endl;
}
return 0;
}