Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 502f8e7

Browse files
authoredJun 15, 2020
Update 121-best-time-to-buy-and-sell-stock.js
1 parent b0321a3 commit 502f8e7

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed
 

‎121-best-time-to-buy-and-sell-stock.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,19 @@ const maxProfit = function(prices) {
1414
}
1515
return maxP;
1616
};
17+
18+
// another
19+
20+
/**
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)
30+
}
31+
return maxSoFar
32+
}

0 commit comments

Comments
 (0)
Please sign in to comment.