-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDefuseTheBomb.java
38 lines (33 loc) · 989 Bytes
/
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
package com.smlnskgmail.jaman.leetcodejava.easy;
// https://leetcode.com/problems/defuse-the-bomb/
public class DefuseTheBomb {
private final int[] code;
private final int k;
public DefuseTheBomb(int[] code, int k) {
this.code = code;
this.k = k;
}
public int[] solution() {
int[] result = new int[code.length];
if (k == 0) {
return result;
}
int limit = k > 0 ? k : -k;
int breakValue = k > 0 ? code.length : -1;
int nextIndex = k > 0 ? 0 : code.length - 1;
for (int i = 0; i < code.length; i++) {
int count = 0;
int j = k > 0 ? i + 1 : i - 1;
int sum = 0;
while (count != limit) {
if (j == breakValue) {
j = nextIndex;
}
sum += code[k > 0 ? j++ : j--];
count++;
}
result[i] = sum;
}
return result;
}
}