-
Notifications
You must be signed in to change notification settings - Fork 16
/
DefuseTheBomb.java
56 lines (50 loc) · 1.82 KB
/
DefuseTheBomb.java
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
public class DefuseTheBomb {
public int[] decrypt(int[] code, int k) {
if (k == 0) return new int[code.length];
final Code codeData = new Code(code);
final int[] result = new int[code.length];
if (k > 0) {
for (int index = 0 ; index < result.length ; index++) {
result[index] = codeData.getSlice(index + 1, index + k);
}
} else {
for (int index = 0 ; index < result.length ; index++) {
result[index] = codeData.getSlice(index + k, index - 1);
}
}
return result;
}
private static final class Code {
private final int[] sum;
private final int codeSum;
Code(int[] code) {
this.sum = computeSum(code);
this.codeSum = sum[sum.length - 1];
}
private int[] computeSum(int[] array) {
final int[] sum = new int[array.length];
sum[0] = array[0];
for (int index = 1 ; index < array.length ; index++) {
sum[index] = sum[index - 1] + array[index];
}
return sum;
}
private int getIndex(int index) {
return (index + sum.length) % sum.length;
}
private int getSlice(int start, int end) {
final int startIndex = getIndex(start), endIndex = getIndex(end);
if (startIndex <= endIndex) {
return sum[endIndex] - (startIndex == 0 ? 0: sum[startIndex - 1]);
}
return codeSum - sum[startIndex - 1] + sum[endIndex];
}
private int[] getFullSumArray(int length) {
final int[] array = new int[length];
for (int index = 0 ; index < length ; index++) {
array[index] = codeSum;
}
return array;
}
}
}