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 e2a8908 commit 4a54264Copy full SHA for 4a54264
121-best-time-to-buy-and-sell-stock.js
@@ -21,12 +21,11 @@ const maxProfit = function(prices) {
21
* @param {number[]} prices
22
* @return {number}
23
*/
24
-const maxProfit = function (prices) {
25
- let maxCur = 0,
26
- maxSoFar = 0
27
- for (let i = 1; i < prices.length; i++) {
28
- maxCur = Math.max(0, (maxCur += prices[i] - prices[i - 1]))
29
- maxSoFar = Math.max(maxCur, maxSoFar)
+const maxProfit = function(prices) {
+ let res = 0, maxCur = 0
+ for(let i = 1; i < prices.length; i++) {
+ maxCur = Math.max(0, maxCur + (prices[i] - prices[i - 1]))
+ res = Math.max(res, maxCur)
30
}
31
- return maxSoFar
32
-}
+ return res
+};
0 commit comments