-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJumpGame.java
More file actions
25 lines (22 loc) · 730 Bytes
/
Copy pathJumpGame.java
File metadata and controls
25 lines (22 loc) · 730 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import java.util.Arrays;
import java.util.stream.IntStream;
/*
* https://leetcode.com/problems/jump-game/
*/
public class JumpGame {
public boolean canJump(int[] nums) {
return IntStream.range(0, nums.length)
.filter(index -> nums[index] == 0)
.allMatch(index -> existsJump(nums, index));
}
private boolean existsJump(int[] nums, int index) {
return IntStream.range(0, index)
.map(operand -> index - operand - 1)
.anyMatch(value -> nums[value] > index - value);
}
public static void main(String[] args) {
System.out.println(new JumpGame().canJump(
new int[]{2, 3, 1, 1, 4}
)); // true
}
}