-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path403_Frog Jump.java
More file actions
29 lines (29 loc) · 891 Bytes
/
403_Frog Jump.java
File metadata and controls
29 lines (29 loc) · 891 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
26
27
28
29
class Solution {
public boolean canCross(int[] stones) {
int target = stones[stones.length - 1];
Map<Integer, Set<Integer>> map = new HashMap<>();
for (int stone:stones) {
map.put(stone, new HashSet<>());
}
map.get(0).add(1);
for (int stone: stones) {
if (map.get(stone).isEmpty()) {
return false;
}
for (int unit: map.get(stone)) {
if (stone + unit == target) {
return true;
}
Set<Integer> next = map.get(stone + unit);
if (next != null) {
next.add(unit);
next.add(unit + 1);
if (unit > 1) {
next.add(unit - 1);
}
}
}
}
return false;
}
}