Skip to content

Commit d948bb6

Browse files
add 1652
1 parent 2394b41 commit d948bb6

File tree

3 files changed

+82
-0
lines changed

3 files changed

+82
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ _If you like this project, please leave me a star._ ★
88

99
| # | Title | Solutions | Video | Difficulty | Tag
1010
|-----|----------------|---------------|--------|-------------|-------------
11+
|1652|[Defuse the Bomb](https://leetcode.com/problems/defuse-the-bomb/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1652.java) ||Easy|Array|
1112
|1640|[Check Array Formation Through Concatenation](https://leetcode.com/problems/check-array-formation-through-concatenation/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1640.java) ||Easy|Array, Sort|
1213
|1637|[Widest Vertical Area Between Two Points Containing No Points](https://leetcode.com/problems/widest-vertical-area-between-two-points-containing-no-points/)|[Javascript](./javascript/_1637.js)| | Medium | Sort |
1314
|1636|[Sort Array by Increasing Frequency](https://leetcode.com/problems/sort-array-by-increasing-frequency/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1636.java) ||Easy|Array, Sort|
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.fishercoder.solutions;
2+
3+
public class _1652 {
4+
public static class Solution1 {
5+
public int[] decrypt(int[] code, int k) {
6+
int[] result = new int[code.length];
7+
int len = code.length;
8+
if (k == 0) {
9+
for (int i = 0; i < code.length; i++) {
10+
result[i] = 0;
11+
}
12+
} else if (k > 0) {
13+
int kSum = 0;
14+
for (int i = 1; i <= k; i++) {
15+
kSum += code[i];
16+
}
17+
result[0] = kSum;
18+
for (int i = 1; i < len; i++) {
19+
kSum -= code[i];
20+
kSum += code[(i + k) % len];
21+
result[i] = kSum;
22+
}
23+
} else {
24+
int kSum = 0;
25+
int kVal = Math.abs(k);
26+
for (int i = len - 1; i >= len - kVal; i--) {
27+
kSum += code[i];
28+
}
29+
result[0] = kSum;
30+
for (int i = 1; i < len; i++) {
31+
kSum -= code[(len - kVal + i - 1) % len];
32+
kSum += code[i - 1];
33+
result[i] = kSum;
34+
}
35+
}
36+
return result;
37+
}
38+
}
39+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._1652;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertArrayEquals;
8+
9+
public class _1652Test {
10+
private static _1652.Solution1 solution1;
11+
private static int[] code;
12+
13+
@BeforeClass
14+
public static void setup() {
15+
solution1 = new _1652.Solution1();
16+
}
17+
18+
@Test
19+
public void test1() {
20+
code = new int[]{5, 7, 1, 4};
21+
assertArrayEquals(new int[]{12, 10, 16, 13}, solution1.decrypt(code, 3));
22+
}
23+
24+
@Test
25+
public void test2() {
26+
code = new int[]{1, 2, 3, 4};
27+
assertArrayEquals(new int[]{0, 0, 0, 0}, solution1.decrypt(code, 0));
28+
}
29+
30+
@Test
31+
public void test3() {
32+
code = new int[]{2, 4, 9, 3};
33+
assertArrayEquals(new int[]{12, 5, 6, 13}, solution1.decrypt(code, -2));
34+
}
35+
36+
@Test
37+
public void test4() {
38+
code = new int[]{10, 5, 7, 7, 3, 2, 10, 3, 6, 9, 1, 6};
39+
assertArrayEquals(new int[]{22, 26, 22, 28, 29, 22, 19, 22, 18, 21, 28, 19}, solution1.decrypt(code, -4));
40+
}
41+
42+
}

0 commit comments

Comments
 (0)