Skip to content

Commit cd4e1f6

Browse files
solves defuse the bomb
1 parent 5a11efe commit cd4e1f6

File tree

2 files changed

+57
-1
lines changed

2 files changed

+57
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -408,7 +408,7 @@
408408
| 1636 | [Sort Array by Increasing Frequency](https://leetcode.com/problems/sort-array-by-increasing-frequency) | [![Java](assets/java.png)](src/SortArrayByIncreasingFrequency.java) | |
409409
| 1640 | [Check Array Formation Through Concatenation](https://leetcode.com/problems/check-array-formation-through-concatenation) | [![Java](assets/java.png)](src/CheckArrayFormationThroughConcatenation.java) | |
410410
| 1646 | [Get Maximum in Generated Array](https://leetcode.com/problems/get-maximum-in-generated-array) | [![Java](assets/java.png)](src/GetMaximumInGeneratedArray.java) | |
411-
| 1652 | [Defuse the Bomb](https://leetcode.com/problems/defuse-the-bomb) | | |
411+
| 1652 | [Defuse the Bomb](https://leetcode.com/problems/defuse-the-bomb) | [![Java](assets/java.png)](src/DefuseTheBomb.java) | |
412412
| 1656 | [Design an Ordered Stream](https://leetcode.com/problems/design-an-ordered-stream) | | |
413413
| 1662 | [Check If Two String Arrays are Equivalent](https://leetcode.com/problems/check-if-two-string-arrays-are-equivalent) | | |
414414
| 1668 | [Maximum Repeating Substring](https://leetcode.com/problems/maximum-repeating-substring) | | |

src/DefuseTheBomb.java

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
public class DefuseTheBomb {
2+
public int[] decrypt(int[] code, int k) {
3+
if (k == 0) return new int[code.length];
4+
final Code codeData = new Code(code);
5+
final int[] result = new int[code.length];
6+
if (k > 0) {
7+
for (int index = 0 ; index < result.length ; index++) {
8+
result[index] = codeData.getSlice(index + 1, index + k);
9+
}
10+
} else {
11+
for (int index = 0 ; index < result.length ; index++) {
12+
result[index] = codeData.getSlice(index + k, index - 1);
13+
}
14+
}
15+
return result;
16+
}
17+
18+
private static final class Code {
19+
private final int[] sum;
20+
private final int codeSum;
21+
22+
Code(int[] code) {
23+
this.sum = computeSum(code);
24+
this.codeSum = sum[sum.length - 1];
25+
}
26+
27+
private int[] computeSum(int[] array) {
28+
final int[] sum = new int[array.length];
29+
sum[0] = array[0];
30+
for (int index = 1 ; index < array.length ; index++) {
31+
sum[index] = sum[index - 1] + array[index];
32+
}
33+
return sum;
34+
}
35+
36+
private int getIndex(int index) {
37+
return (index + sum.length) % sum.length;
38+
}
39+
40+
private int getSlice(int start, int end) {
41+
final int startIndex = getIndex(start), endIndex = getIndex(end);
42+
if (startIndex <= endIndex) {
43+
return sum[endIndex] - (startIndex == 0 ? 0: sum[startIndex - 1]);
44+
}
45+
return codeSum - sum[startIndex - 1] + sum[endIndex];
46+
}
47+
48+
private int[] getFullSumArray(int length) {
49+
final int[] array = new int[length];
50+
for (int index = 0 ; index < length ; index++) {
51+
array[index] = codeSum;
52+
}
53+
return array;
54+
}
55+
}
56+
}

0 commit comments

Comments
 (0)