File tree 1 file changed +8
-7
lines changed
scripts/algorithms/B/Best Time to Buy and Sell Stock II 1 file changed +8
-7
lines changed Original file line number Diff line number Diff line change
1
+ // Runtime: 102 ms (Top 35.16%) | Memory: 42.5 MB (Top 44.08%)
1
2
/**
2
3
* @param {number[] } prices
3
4
* @return {number }
@@ -6,17 +7,17 @@ var maxProfit = function(prices) {
6
7
let lowestNum = prices [ 0 ] ;
7
8
let highestNum = prices [ 0 ] ;
8
9
let profit = highestNum - lowestNum ;
9
-
10
+
10
11
for ( var indexI = 1 ; indexI < prices . length ; indexI ++ ) {
11
- if ( prices [ indexI ] < prices [ indexI - 1 ] ) {
12
- lowestNum = prices [ indexI ] ;
12
+ if ( prices [ indexI ] < prices [ indexI - 1 ] ) {
13
+ lowestNum = prices [ indexI ] ;
13
14
}
14
- if ( prices [ indexI ] > lowestNum ) {
15
+ if ( prices [ indexI ] > lowestNum ) {
15
16
lowestNum = prices [ indexI - 1 ] ;
16
17
profit = profit + prices [ indexI ] - lowestNum ;
17
- highestNum = prices [ indexI ] ;
18
+ highestNum = prices [ indexI ] ;
18
19
}
19
20
}
20
-
21
+
21
22
return profit ;
22
- } ;
23
+ } ;
You can’t perform that action at this time.
0 commit comments