Skip to content

Commit 519dc5b

Browse files
committed
Runtime: 183 ms (Top 40.91%) | Memory: 47.7 MB (Top 22.73%)
1 parent 8b84b22 commit 519dc5b

File tree

1 file changed

+10
-9
lines changed

1 file changed

+10
-9
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,36 @@
1+
// Runtime: 183 ms (Top 40.91%) | Memory: 47.7 MB (Top 22.73%)
12
var maxJumps = function(arr, d) {
2-
3+
34
let n = arr.length;
45
let dp = new Array(n).fill(0);
56
let result = 0;
6-
7+
78
// Time complexity:
89
// O(nlogn) + O(n*d) => O(ologn)
9-
10+
1011
// First we sort the arr (small -> large) , then calculating DP by sortedArr.
1112
// O(nlogn)
1213
let sortedArr = arr.map((v,i)=>([v,i])).sort((a,b)=>a[0]-b[0]);
13-
14+
1415
// Shifting with single way
1516
// mid -> left ; mid -> right
1617
// O(n*d)
1718
for(let index=1 ; index< n; index++){
1819
let [v,i] = sortedArr[index];
19-
20+
2021
for(let shift =1 ; shift <=d ; shift++){
2122
if(i+shift >= n || arr[i+shift] >= arr[i]) break;
2223
dp[i] = Math.max(dp[i],dp[i+shift]+1);
2324
if(dp[i] > result) result = dp[i];
2425
};
25-
26+
2627
for(let shift=-1 ; shift >= -d; shift--){
2728
if(i+shift < 0 || arr[i+shift] >= arr[i]) break;
2829
dp[i] = Math.max(dp[i],dp[i+shift]+1);
2930
if(dp[i] > result) result = dp[i];
3031
};
3132
};
32-
33+
3334
return result+1;
34-
35-
};
35+
36+
};

0 commit comments

Comments
 (0)