Skip to content

Commit 2b9849e

Browse files
authored
feat: best-time-to-buy-and-sell-stock
1 parent 3bfd185 commit 2b9849e

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/**
2+
* @param {number[]} prices
3+
* @return {number}
4+
*/
5+
var maxProfit = function(prices) {
6+
let maxGap = 0;
7+
8+
for(let i =0; i < prices.length; i++){
9+
const price = prices[i];
10+
const restList = prices.slice(i);
11+
12+
const max = Math.max(...restList);
13+
14+
maxGap = Math.max(max-price, maxGap)
15+
}
16+
17+
return maxGap
18+
};
19+

0 commit comments

Comments
 (0)