diff --git a/leetcode/1601-1700/1652.Defuse-the-Bomb/README.md b/leetcode/1601-1700/1652.Defuse-the-Bomb/README.md index 1e868c0bc..dcdef162b 100755 --- a/leetcode/1601-1700/1652.Defuse-the-Bomb/README.md +++ b/leetcode/1601-1700/1652.Defuse-the-Bomb/README.md @@ -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. +``` ## 结语 diff --git a/leetcode/1601-1700/1652.Defuse-the-Bomb/Solution.go b/leetcode/1601-1700/1652.Defuse-the-Bomb/Solution.go index d115ccf5e..e1133167a 100644 --- a/leetcode/1601-1700/1652.Defuse-the-Bomb/Solution.go +++ b/leetcode/1601-1700/1652.Defuse-the-Bomb/Solution.go @@ -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 } diff --git a/leetcode/1601-1700/1652.Defuse-the-Bomb/Solution_test.go b/leetcode/1601-1700/1652.Defuse-the-Bomb/Solution_test.go index 14ff50eb4..4f7b6b2f8 100644 --- a/leetcode/1601-1700/1652.Defuse-the-Bomb/Solution_test.go +++ b/leetcode/1601-1700/1652.Defuse-the-Bomb/Solution_test.go @@ -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() { }