Skip to content

Add solution and test-cases for problem 1652 #1036

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 26 additions & 13 deletions leetcode/1601-1700/1652.Defuse-the-Bomb/README.md
Original file line number Diff line number Diff line change
@@ -1,28 +1,41 @@
# [1652.Defuse the Bomb][title]

> [!WARNING|style:flat]
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)

## Description
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`.

To decrypt the code, you must replace every number. All the numbers are replaced **simultaneously**.

- If `k > 0`, replace the `ith` number with the sum of the **next** `k` numbers.
- If `k < 0`, replace the `ith` number with the sum of the **previous** `k` numbers.
- If `k == 0`, replace the `ith` number with `0`.

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]`.

Given the **circular** array `code` and an integer key `k`, return the decrypted code to defuse the bomb!

**Example 1:**

```
Input: a = "11", b = "1"
Output: "100"
Input: code = [5,7,1,4], k = 3
Output: [12,10,16,13]
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.
```

## 题意
> ...
**Example 2:**

## 题解

### 思路1
> ...
Defuse the Bomb
```go
```
Input: code = [1,2,3,4], k = 0
Output: [0,0,0,0]
Explanation: When k is zero, the numbers are replaced by 0.
```

**Example 3:**

```
Input: code = [2,4,9,3], k = -2
Output: [12,5,6,13]
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.
```

## 结语

Expand Down
44 changes: 42 additions & 2 deletions leetcode/1601-1700/1652.Defuse-the-Bomb/Solution.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,45 @@
package Solution

func Solution(x bool) bool {
return x
func Solution(code []int, k int) []int {
l := len(code)
sum := make([]int, l)
if k == 0 {
return sum
}
sum[0] = code[0]
for i := 1; i < l; i++ {
sum[i] = sum[i-1] + code[i]
}
ans := make([]int, l)
if k < 0 {
k = -k
for i := range l {
diff := i - k
if diff > 0 {
start, end := diff, i-1
ans[i] = sum[end]
if start > 0 {
ans[i] -= sum[start-1]
}
} else {
if i > 0 {
ans[i] += sum[i-1]
}
ans[i] += sum[l-1] - sum[l+diff-1]
}
}
} else {
for i := range l {
diff := i + k
if diff < l {
ans[i] = sum[diff] - sum[i]
} else {
ans[i] += sum[l-1] - sum[i]
ans[i] += sum[diff-l]
}
}

}

return ans
}
21 changes: 11 additions & 10 deletions leetcode/1601-1700/1652.Defuse-the-Bomb/Solution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,30 +10,31 @@ func TestSolution(t *testing.T) {
// 测试用例
cases := []struct {
name string
inputs bool
expect bool
code []int
k int
expect []int
}{
{"TestCase", true, true},
{"TestCase", true, true},
{"TestCase", false, false},
{"TestCase1", []int{5, 7, 1, 4}, 3, []int{12, 10, 16, 13}},
{"TestCase2", []int{1, 2, 3, 4}, 0, []int{0, 0, 0, 0}},
{"TestCase3", []int{2, 4, 9, 3}, -2, []int{12, 5, 6, 13}},
}

// 开始测试
for i, c := range cases {
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
got := Solution(c.inputs)
got := Solution(c.code, c.k)
if !reflect.DeepEqual(got, c.expect) {
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
c.expect, got, c.inputs)
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v",
c.expect, got, c.code, c.k)
}
})
}
}

// 压力测试
// 压力测试
func BenchmarkSolution(b *testing.B) {
}

// 使用案列
// 使用案列
func ExampleSolution() {
}
Loading