File tree Expand file tree Collapse file tree 1 file changed +7
-2
lines changed Expand file tree Collapse file tree 1 file changed +7
-2
lines changed Original file line number Diff line number Diff line change 21
21
var maxProfit = function ( prices ) {
22
22
if ( prices == null || prices . length == 0 )
23
23
return 0 ;
24
+
24
25
var max = 0 ;
26
+
25
27
var left = new Array ( prices . length ) ;
26
28
left . fill ( 0 ) ;
27
- var right = new Array ( prices . length ) ;
28
- right . fill ( 0 ) ;
29
29
var minPrice = prices [ 0 ] ;
30
+
30
31
for ( var i = 1 ; i < prices . length ; i ++ ) {
31
32
if ( minPrice < prices [ i ] ) {
32
33
left [ i ] = Math . max ( left [ i - 1 ] , prices [ i ] - minPrice ) ;
@@ -35,6 +36,9 @@ var maxProfit = function (prices) {
35
36
minPrice = prices [ i ] ;
36
37
}
37
38
}
39
+
40
+ var right = new Array ( prices . length ) ;
41
+ right . fill ( 0 ) ;
38
42
var maxPrice = prices [ prices . length - 1 ] ;
39
43
for ( var i = prices . length - 2 ; i >= 0 ; i -- ) {
40
44
if ( prices [ i ] < maxPrice ) {
@@ -44,6 +48,7 @@ var maxProfit = function (prices) {
44
48
maxPrice = prices [ i ] ;
45
49
}
46
50
}
51
+
47
52
for ( var i = 0 ; i < prices . length ; i ++ ) {
48
53
max = Math . max ( max , left [ i ] + right [ i ] ) ;
49
54
}
You can’t perform that action at this time.
0 commit comments