Skip to content

Commit 58fd6fe

Browse files
committed
update: BestTimeToBuyandSell.cpp
1 parent b0cd7d8 commit 58fd6fe

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

array/BestTimeToBuyandSell.cpp

+10-2
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,21 @@ class Solution {
1414
}
1515
return maxSoFar;
1616
}
17+
18+
int maxProfitSecondApproach(vector<int>& prices) {
19+
int max = 0;
20+
for(int i = 1; i < prices.size(); i++) {
21+
if(prices[i] > prices[i - 1]) max += prices[i] - prices[i - 1];
22+
}
23+
return max;
24+
}
1725
};
1826

1927
int main() {
2028
Solution sol;
2129
vector<int> test1 = {7,1,5,3,6,4};
2230
vector<int> test2 = {7,6,4,3,1};
23-
cout << sol.maxProfit(test1) << endl;
24-
cout << sol.maxProfit(test2) << endl;
31+
cout << sol.maxProfitSecondApproach(test1) << endl;
32+
cout << sol.maxProfitSecondApproach(test2) << endl;
2533
return 0;
2634
}

0 commit comments

Comments
 (0)