File tree Expand file tree Collapse file tree 1 file changed +35
-0
lines changed Expand file tree Collapse file tree 1 file changed +35
-0
lines changed Original file line number Diff line number Diff line change @@ -55,3 +55,38 @@ const maxJumps = function (arr, d) {
55
55
for ( let i = 0 ; i < arr . length ; i ++ ) dfs ( i )
56
56
return Math . max ( ...cache )
57
57
}
58
+
59
+ // another
60
+
61
+ /**
62
+ * @param {number[] } arr
63
+ * @param {number } d
64
+ * @return {number }
65
+ */
66
+ const maxJumps = function ( arr , d , res = 0 ) {
67
+ const n = arr . length
68
+ const stack = [ ] , stack2 = [ ]
69
+ const dp = Array ( n + 1 ) . fill ( 1 )
70
+ arr . push ( Infinity )
71
+ for ( let i = 0 ; i <= n ; i ++ ) {
72
+ while ( stack . length && arr [ stack [ stack . length - 1 ] ] < arr [ i ] ) {
73
+ const pre = arr [ stack [ stack . length - 1 ] ]
74
+ while ( stack . length && pre === arr [ stack [ stack . length - 1 ] ] ) {
75
+ const j = stack [ stack . length - 1 ]
76
+ stack . pop ( )
77
+ if ( i - j <= d ) dp [ i ] = Math . max ( dp [ i ] , dp [ j ] + 1 )
78
+ stack2 . push ( j )
79
+ }
80
+ while ( stack2 . length ) {
81
+ const j = stack2 [ stack2 . length - 1 ]
82
+ stack2 . pop ( )
83
+ if ( stack . length && j - stack [ stack . length - 1 ] <= d ) {
84
+ dp [ stack [ stack . length - 1 ] ] = Math . max ( dp [ stack [ stack . length - 1 ] ] , dp [ j ] + 1 )
85
+ }
86
+ }
87
+ }
88
+ stack . push ( i )
89
+ }
90
+ for ( let i = 0 ; i < n ; i ++ ) res = Math . max ( res , dp [ i ] )
91
+ return res
92
+ }
You can’t perform that action at this time.
0 commit comments