-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMicrosoft.java
More file actions
79 lines (67 loc) · 2.25 KB
/
Microsoft.java
File metadata and controls
79 lines (67 loc) · 2.25 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
public class Microsoft {
public static boolean canCross(int[] stones) {
return canCrossHelper(stones, 0, 0);
}
private static boolean canCrossHelper(int[] stones, int pos, int k) {
for (int i = pos + 1; i < stones.length; i++) {
int gap = stones[i] - stones[pos];
if (gap < k - 1) {
continue;
}
if (gap > k + 1) {
return false;
}
if (canCrossHelper(stones, i, gap)) {
return true;
}
}
return pos == stones.length - 1;
}
public static boolean canCrossBinary(int[] stones, int pos, int k) {
for (int i = pos + 1; i < stones.length; i++) {
int gap = i - pos;
if (gap < k - 1) {
continue;
}
if (gap > k + 1) {
return false;
}
if (canCrossBinary(stones, i, gap)) {
return true;
}
}
return pos == stones.length - 1;
}
public static void main(String[] args) {
int[] stones1 = new int[]{0,1,3,5,6,8,12,17};
int[] stones2 = new int[]{0,1,2,3,4,8,9,11};
int[] river1 = new int[stones1[stones1.length - 1] + 1];
int[] river2 = new int[stones2[stones2.length - 1] + 1];
int stoneIndex = 0;
for (int i = 0; i < river1.length; i++) {
if (i == stones1[stoneIndex]) {
river1[i] = 1;
stoneIndex++;
}
}
stoneIndex = 0;
for (int i = 0; i < river2.length; i++) {
if (i == stones2[stoneIndex]) {
river2[i] = 1;
stoneIndex++;
}
}
System.out.println(canCross(stones1));
System.out.println(canCross(stones2));
for (int i = 0; i < river1.length; i++) {
System.out.print(river1[i] + " ");
}
System.out.println();
for (int i = 0; i < river2.length; i++) {
System.out.print(river2[i] + " ");
}
System.out.println();
System.out.println(canCrossBinary(river1, 0, 0));
System.out.println(canCrossBinary(river2, 0, 0));
}
}