Skip to content

Commit 3e79463

Browse files
authored
Merge pull request #1036 from 0xff-dev/1652
Add solution and test-cases for problem 1652
2 parents 7fca019 + 2177923 commit 3e79463

File tree

3 files changed

+79
-25
lines changed

3 files changed

+79
-25
lines changed

leetcode/1601-1700/1652.Defuse-the-Bomb/README.md

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,41 @@
11
# [1652.Defuse the Bomb][title]
22

3-
> [!WARNING|style:flat]
4-
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
5-
63
## Description
4+
You have a bomb to defuse, and your time is running out! Your informer will provide you with a **circular** array `code` of length of `n` and a key `k`.
5+
6+
To decrypt the code, you must replace every number. All the numbers are replaced **simultaneously**.
7+
8+
- If `k > 0`, replace the `ith` number with the sum of the **next** `k` numbers.
9+
- If `k < 0`, replace the `ith` number with the sum of the **previous** `k` numbers.
10+
- If `k == 0`, replace the `ith` number with `0`.
11+
12+
As `code` is circular, the next element of `code[n-1]` is `code[0]`, and the previous element of `code[0]` is `code[n-1]`.
13+
14+
Given the **circular** array `code` and an integer key `k`, return the decrypted code to defuse the bomb!
715

816
**Example 1:**
917

1018
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
19+
Input: code = [5,7,1,4], k = 3
20+
Output: [12,10,16,13]
21+
Explanation: Each number is replaced by the sum of the next 3 numbers. The decrypted code is [7+1+4, 1+4+5, 4+5+7, 5+7+1]. Notice that the numbers wrap around.
1322
```
1423

15-
## 题意
16-
> ...
24+
**Example 2:**
1725

18-
## 题解
19-
20-
### 思路1
21-
> ...
22-
Defuse the Bomb
23-
```go
2426
```
27+
Input: code = [1,2,3,4], k = 0
28+
Output: [0,0,0,0]
29+
Explanation: When k is zero, the numbers are replaced by 0.
30+
```
31+
32+
**Example 3:**
2533

34+
```
35+
Input: code = [2,4,9,3], k = -2
36+
Output: [12,5,6,13]
37+
Explanation: The decrypted code is [3+9, 2+3, 4+2, 9+4]. Notice that the numbers wrap around again. If k is negative, the sum is of the previous numbers.
38+
```
2639

2740
## 结语
2841

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,45 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(code []int, k int) []int {
4+
l := len(code)
5+
sum := make([]int, l)
6+
if k == 0 {
7+
return sum
8+
}
9+
sum[0] = code[0]
10+
for i := 1; i < l; i++ {
11+
sum[i] = sum[i-1] + code[i]
12+
}
13+
ans := make([]int, l)
14+
if k < 0 {
15+
k = -k
16+
for i := range l {
17+
diff := i - k
18+
if diff > 0 {
19+
start, end := diff, i-1
20+
ans[i] = sum[end]
21+
if start > 0 {
22+
ans[i] -= sum[start-1]
23+
}
24+
} else {
25+
if i > 0 {
26+
ans[i] += sum[i-1]
27+
}
28+
ans[i] += sum[l-1] - sum[l+diff-1]
29+
}
30+
}
31+
} else {
32+
for i := range l {
33+
diff := i + k
34+
if diff < l {
35+
ans[i] = sum[diff] - sum[i]
36+
} else {
37+
ans[i] += sum[l-1] - sum[i]
38+
ans[i] += sum[diff-l]
39+
}
40+
}
41+
42+
}
43+
44+
return ans
545
}

leetcode/1601-1700/1652.Defuse-the-Bomb/Solution_test.go

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,30 +10,31 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
code []int
14+
k int
15+
expect []int
1516
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
17+
{"TestCase1", []int{5, 7, 1, 4}, 3, []int{12, 10, 16, 13}},
18+
{"TestCase2", []int{1, 2, 3, 4}, 0, []int{0, 0, 0, 0}},
19+
{"TestCase3", []int{2, 4, 9, 3}, -2, []int{12, 5, 6, 13}},
1920
}
2021

2122
// 开始测试
2223
for i, c := range cases {
2324
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
24-
got := Solution(c.inputs)
25+
got := Solution(c.code, c.k)
2526
if !reflect.DeepEqual(got, c.expect) {
26-
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
27-
c.expect, got, c.inputs)
27+
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v",
28+
c.expect, got, c.code, c.k)
2829
}
2930
})
3031
}
3132
}
3233

33-
// 压力测试
34+
// 压力测试
3435
func BenchmarkSolution(b *testing.B) {
3536
}
3637

37-
// 使用案列
38+
// 使用案列
3839
func ExampleSolution() {
3940
}

0 commit comments

Comments
 (0)