-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
https://leetcode.com/problems/jump-game/
- Loading branch information
Showing
4 changed files
with
76 additions
and
37 deletions.
There are no files selected for viewing
31 changes: 31 additions & 0 deletions
31
interview_prep/algorithm/java/src/main/java/hoa/can/code/gg/JumpingDP.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package hoa.can.code.gg; | ||
|
||
public class JumpingDP { | ||
public static boolean canJump(int[] nums) { | ||
return minJumps(nums) != Integer.MAX_VALUE; | ||
} | ||
|
||
public static int minJumps(int[] arr) { | ||
int n = arr.length; | ||
int minCostToIdx[] = new int[n]; | ||
// result | ||
int i, j; | ||
|
||
// if first element is 0, | ||
if (n == 0 || arr[0] == 0) | ||
return Integer.MAX_VALUE; | ||
|
||
minCostToIdx[0] = 0; | ||
|
||
for (i = 1; i < n; i++) { | ||
minCostToIdx[i] = Integer.MAX_VALUE; | ||
for (j = 0; j < i; j++) { | ||
if (i <= j + arr[j] && minCostToIdx[j] != Integer.MAX_VALUE) { | ||
minCostToIdx[i] = Math.min(minCostToIdx[i], minCostToIdx[j] + 1); | ||
break; | ||
} | ||
} | ||
} | ||
return minCostToIdx[n - 1]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters