We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 0c0804b commit 080e8a7Copy full SHA for 080e8a7
Cpp/maxProfit.cpp
@@ -36,6 +36,10 @@ class Solution {
36
{
37
int diff = prices[i+1] - prices[i];
38
if(diff == 0) continue;
39
+ // if previous profit is less than the current one (negative profit = loss)
40
+ // then it should be discarded
41
+ // that is why maximum of (diff+currentMax) and diff
42
+ // is checked here
43
currentMax = std::max(diff+currentMax, diff);
44
max = std::max(currentMax, max);
45
}
0 commit comments