Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit28b86f8

Browse files
authored
Update 0045-jump-game-ii.kt
1 parent7fa5229 commit28b86f8

File tree

1 file changed

+33
-1
lines changed

1 file changed

+33
-1
lines changed

‎kotlin/0045-jump-game-ii.kt‎

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
/*
2+
* O(n) BFS
3+
*/
14
classSolution {
25
funjump(nums:IntArray):Int {
36
var left=0
@@ -15,4 +18,33 @@ class Solution {
1518
}
1619
return res
1720
}
18-
}
21+
}
22+
23+
/*
24+
* O(N^2) DP + memoization
25+
*/
26+
classSolution {
27+
funjump(nums:IntArray):Int {
28+
val dp=IntArray(nums.size) {10001 }
29+
30+
funjump(i:Int):Int {
31+
if (i== nums.lastIndex)
32+
return0
33+
if (dp[i]!=10001)
34+
return dp[i]
35+
36+
for (stepsin1..nums[i]) {
37+
if (i+ steps<= nums.lastIndex) {
38+
dp[i]= minOf(
39+
dp[i],
40+
1+ jump(i+ steps)
41+
)
42+
}
43+
}
44+
45+
return dp[i]
46+
}
47+
48+
return jump(0)
49+
}
50+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp