Files
LeetCode/Easy/best-time-to-buy-and-sell-stock/solution.cpp
Arkaprabha Chakraborty cef4c4dcc4 Initial commit
2022-07-26 16:15:18 +05:30

15 lines
351 B
C++

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;
}
};