mirror of
https://github.com/arkorty/LeetCode.git
synced 2026-03-18 00:57:17 +00:00
Initial commit
This commit is contained in:
14
Easy/best-time-to-buy-and-sell-stock/solution.cpp
Normal file
14
Easy/best-time-to-buy-and-sell-stock/solution.cpp
Normal file
@@ -0,0 +1,14 @@
|
||||
class Solution {
|
||||
public:
|
||||
int maxProfit(std::vector<int> &prices) {
|
||||
int prof = 0;
|
||||
for (int i = prices.size() - 1, max = 0; i >= 0; --i) {
|
||||
if (prices[i] > max)
|
||||
max = prices[i];
|
||||
else if (max - prices[i] > prof)
|
||||
prof = max - prices[i];
|
||||
}
|
||||
|
||||
return prof;
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user