Skip to content

Commit b272dfc

Browse files
committed
feat: solve new question
1 parent 6d34e1a commit b272dfc

File tree

1 file changed

+21
-0
lines changed
  • best-time-to-buy-and-sell-stock

1 file changed

+21
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* @param {number[]} prices
3+
* @return {number}
4+
*/
5+
var maxProfit = function(prices) {
6+
let left = 0;
7+
let right = 1;
8+
let maxProfit = 0;
9+
10+
while (right < (prices.length)) {
11+
if (prices[left] < prices[right]) {
12+
let profit = prices[right] - prices[left];
13+
maxProfit = Math.max(profit, maxProfit);
14+
} else {
15+
left = right;
16+
}
17+
right += 1;
18+
}
19+
20+
return maxProfit;
21+
};

0 commit comments

Comments
 (0)