Best Time to Buy and Sell Stock II LeetCode solution in JavaScript

1
/**
2
* @param {number[]} prices
3
* @return {number}
4
*/
5
var maxProfit = function (prices) {
6
let lowestNum = prices[0];
7
let highestNum = prices[0];
8
let profit = highestNum - lowestNum;
9

10
for (var indexI = 1; indexI < prices.length; indexI++) {
11
if (prices[indexI] < prices[indexI - 1]) {
12
lowestNum = prices[indexI];
13
}
14
if (prices[indexI] > lowestNum) {
15
lowestNum = prices[indexI - 1];
16
profit = profit + prices[indexI] - lowestNum;
17
highestNum = prices[indexI];
18
}
19
}
20

21
return profit;
22
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0