-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathHops.java
More file actions
55 lines (52 loc) · 2.15 KB
/
Hops.java
File metadata and controls
55 lines (52 loc) · 2.15 KB
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import java.util.List;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Map;
import java.util.HashMap;
public class Hops {
public List<List<Integer>> find(int[] steps) {
if (steps == null || steps.length == 0)
return new ArrayList<>();
Map<Integer, List<List<Integer>>> cache = new HashMap<>();
find(steps, 0, cache);
return cache.get(0);
}
List<List<Integer>> find(int[] steps, int currentIndex, Map<Integer, List<List<Integer>>> cache) {
if (cache.containsKey(currentIndex))
return cache.get(currentIndex);
if (currentIndex > steps.length - 1)
return new ArrayList<>();
if (currentIndex == steps.length - 1) {
cache.put(currentIndex, new ArrayList<>(Arrays.asList(new ArrayList<>(Arrays.asList(0)))));
return cache.get(currentIndex);
}
if (currentIndex < steps.length - 1 && steps[currentIndex] == 0)
return new ArrayList<>();
List<List<Integer>> options = new ArrayList<>();
for (int i = 1; i <= steps[currentIndex]; i++ ) {
List<List<Integer>> subOptions = find(steps, currentIndex + i, cache);
if (subOptions != null && subOptions.size() != 0) {
for (List<Integer> subOption : subOptions) {
List<Integer> option = new ArrayList(Arrays.asList(i));
option.addAll(subOption);
options.add(option);
cache.put(currentIndex, options);
}
}
}
return cache.get(currentIndex);
}
public static void main(String[] args) {
//int[] steps = new int [] {2, 0, 1, 0};
//int[] steps = new int [] {1, 1, 0, 1};
int[] steps = new int [] {2, 0, 1, 3, 0, 2, 3, 1, 0, 4, 2, 0, 1, 1, 4, 2, 0};
Hops hops = new Hops();
System.out.println("INPUT: ");
for (int i = 0; i < steps.length; i++)
System.out.print(steps[i] + " ");
System.out.println();
List<List<Integer>> sequence = hops.find(steps);
System.out.println("OUTPUT: ");
System.out.println(sequence);
}
}