File tree Expand file tree Collapse file tree 1 file changed +33
-0
lines changed Expand file tree Collapse file tree 1 file changed +33
-0
lines changed Original file line number Diff line number Diff line change
1
+ /**
2
+ * @param {number[] } arr
3
+ * @param {number } d
4
+ * @return {number }
5
+ */
6
+ const maxJumps = function ( arr , d ) {
7
+ const map = { }
8
+ let max = 1
9
+ for ( let i = 0 ; i < arr . length ; i ++ ) {
10
+ max = Math . max ( max , calc ( i ) )
11
+ }
12
+ return max
13
+
14
+ function calc ( i ) {
15
+ if ( map [ i ] ) return map [ i ]
16
+ let max = 1
17
+ const left = Math . max ( 0 , i - d )
18
+ for ( let j = i - 1 ; j >= left ; j -- ) {
19
+ if ( arr [ j ] >= arr [ i ] ) break
20
+ if ( arr [ j ] < arr [ i ] ) max = Math . max ( max , calc ( j ) + 1 )
21
+ }
22
+ const right = Math . min ( arr . length - 1 , i + d )
23
+ for ( let j = i + 1 ; j <= right ; j ++ ) {
24
+ if ( arr [ j ] >= arr [ i ] ) break
25
+ if ( arr [ j ] < arr [ i ] ) max = Math . max ( max , calc ( j ) + 1 )
26
+ }
27
+ map [ i ] = max
28
+ return map [ i ]
29
+ }
30
+ }
31
+
32
+ // another
33
+
1
34
/**
2
35
* @param {number[] } arr
3
36
* @param {number } d
You can’t perform that action at this time.
0 commit comments