Skip to content

Commit 568d467

Browse files
authored
Update 0121-best-time-to-buy-and-sell-stock.cpp
1 parent d314414 commit 568d467

File tree

1 file changed

+8
-8
lines changed

1 file changed

+8
-8
lines changed

Diff for: cpp/0121-best-time-to-buy-and-sell-stock.cpp

+8-8
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,14 @@
1111
class Solution {
1212
public:
1313
int maxProfit(vector<int>& prices) {
14-
int minValue = prices[0];
15-
int maxDiff = 0;
16-
17-
for (int i = 1; i < prices.size(); i++) {
18-
minValue = min(minValue, prices[i]);
19-
maxDiff = max(maxDiff, prices[i] - minValue);
14+
int maxP = 0, l = 0, r = 0;
15+
while (r < prices.size()){
16+
if (prices[r] > prices[l])
17+
maxP = max(maxP, prices[r] - prices[l]);
18+
else
19+
l = r;
20+
++r;
2021
}
21-
22-
return maxDiff;
22+
return maxP;
2323
}
2424
};

0 commit comments

Comments
 (0)