Skip to content

Commit 0c4b373

Browse files
committed
Add solution 1652、1653
1 parent 5770d11 commit 0c4b373

File tree

6 files changed

+423
-0
lines changed

6 files changed

+423
-0
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package leetcode
2+
3+
func decrypt(code []int, k int) []int {
4+
if k == 0 {
5+
for i := 0; i < len(code); i++ {
6+
code[i] = 0
7+
}
8+
return code
9+
}
10+
count, sum, res := k, 0, make([]int, len(code))
11+
if k > 0 {
12+
for i := 0; i < len(code); i++ {
13+
for j := i + 1; j < len(code); j++ {
14+
if count == 0 {
15+
break
16+
}
17+
sum += code[j]
18+
count--
19+
}
20+
if count > 0 {
21+
for j := 0; j < len(code); j++ {
22+
if count == 0 {
23+
break
24+
}
25+
sum += code[j]
26+
count--
27+
}
28+
}
29+
res[i] = sum
30+
sum, count = 0, k
31+
}
32+
}
33+
if k < 0 {
34+
for i := 0; i < len(code); i++ {
35+
for j := i - 1; j >= 0; j-- {
36+
if count == 0 {
37+
break
38+
}
39+
sum += code[j]
40+
count++
41+
}
42+
if count < 0 {
43+
for j := len(code) - 1; j >= 0; j-- {
44+
if count == 0 {
45+
break
46+
}
47+
sum += code[j]
48+
count++
49+
}
50+
}
51+
res[i] = sum
52+
sum, count = 0, k
53+
}
54+
}
55+
return res
56+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package leetcode
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
)
7+
8+
type question1652 struct {
9+
para1652
10+
ans1652
11+
}
12+
13+
// para 是参数
14+
// one 代表第一个参数
15+
type para1652 struct {
16+
code []int
17+
k int
18+
}
19+
20+
// ans 是答案
21+
// one 代表第一个答案
22+
type ans1652 struct {
23+
one []int
24+
}
25+
26+
func Test_Problem1652(t *testing.T) {
27+
28+
qs := []question1652{
29+
30+
{
31+
para1652{[]int{5, 7, 1, 4}, 3},
32+
ans1652{[]int{12, 10, 16, 13}},
33+
},
34+
35+
{
36+
para1652{[]int{1, 2, 3, 4}, 0},
37+
ans1652{[]int{0, 0, 0, 0}},
38+
},
39+
40+
{
41+
para1652{[]int{2, 4, 9, 3}, -2},
42+
ans1652{[]int{12, 5, 6, 13}},
43+
},
44+
}
45+
46+
fmt.Printf("------------------------Leetcode Problem 1652------------------------\n")
47+
48+
for _, q := range qs {
49+
_, p := q.ans1652, q.para1652
50+
fmt.Printf("【input】:%v 【output】:%v \n", p, decrypt(p.code, p.k))
51+
}
52+
fmt.Printf("\n\n\n")
53+
}
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
# [1652. Defuse the Bomb](https://leetcode.com/problems/defuse-the-bomb/)
2+
3+
4+
## 题目
5+
6+
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`.
7+
8+
To decrypt the code, you must replace every number. All the numbers are replaced **simultaneously**.
9+
10+
- If `k > 0`, replace the `ith` number with the sum of the **next** `k` numbers.
11+
- If `k < 0`, replace the `ith` number with the sum of the **previous** `k` numbers.
12+
- If `k == 0`, replace the `ith` number with `0`.
13+
14+
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]`.
15+
16+
Given the **circular** array `code` and an integer key `k`, return *the decrypted code to defuse the bomb*!
17+
18+
**Example 1:**
19+
20+
```
21+
Input: code = [5,7,1,4], k = 3
22+
Output: [12,10,16,13]
23+
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.
24+
```
25+
26+
**Example 2:**
27+
28+
```
29+
Input: code = [1,2,3,4], k = 0
30+
Output: [0,0,0,0]
31+
Explanation: When k is zero, the numbers are replaced by 0.
32+
```
33+
34+
**Example 3:**
35+
36+
```
37+
Input: code = [2,4,9,3], k = -2
38+
Output: [12,5,6,13]
39+
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.
40+
```
41+
42+
**Constraints:**
43+
44+
- `n == code.length`
45+
- `1 <= n <= 100`
46+
- `1 <= code[i] <= 100`
47+
- `(n - 1) <= k <= n - 1`
48+
49+
## 题目大意
50+
51+
你有一个炸弹需要拆除,时间紧迫!你的情报员会给你一个长度为 n 的 循环 数组 code 以及一个密钥 k 。为了获得正确的密码,你需要替换掉每一个数字。所有数字会 同时 被替换。
52+
53+
- 如果 k > 0 ,将第 i 个数字用 接下来 k 个数字之和替换。
54+
- 如果 k < 0 ,将第 i 个数字用 之前 k 个数字之和替换。
55+
- 如果 k == 0 ,将第 i 个数字用 0 替换。
56+
57+
由于 code 是循环的, code[n-1] 下一个元素是 code[0] ,且 code[0] 前一个元素是 code[n-1] 。
58+
59+
给你 循环 数组 code 和整数密钥 k ,请你返回解密后的结果来拆除炸弹!
60+
61+
## 解题思路
62+
63+
- 给出一个 code 数组,要求按照规则替换每个字母。
64+
- 简单题,按照题意描述循环即可。
65+
66+
## 代码
67+
68+
```go
69+
package leetcode
70+
71+
func decrypt(code []int, k int) []int {
72+
if k == 0 {
73+
for i := 0; i < len(code); i++ {
74+
code[i] = 0
75+
}
76+
return code
77+
}
78+
count, sum, res := k, 0, make([]int, len(code))
79+
if k > 0 {
80+
for i := 0; i < len(code); i++ {
81+
for j := i + 1; j < len(code); j++ {
82+
if count == 0 {
83+
break
84+
}
85+
sum += code[j]
86+
count--
87+
}
88+
if count > 0 {
89+
for j := 0; j < len(code); j++ {
90+
if count == 0 {
91+
break
92+
}
93+
sum += code[j]
94+
count--
95+
}
96+
}
97+
res[i] = sum
98+
sum, count = 0, k
99+
}
100+
}
101+
if k < 0 {
102+
for i := 0; i < len(code); i++ {
103+
for j := i - 1; j >= 0; j-- {
104+
if count == 0 {
105+
break
106+
}
107+
sum += code[j]
108+
count++
109+
}
110+
if count < 0 {
111+
for j := len(code) - 1; j >= 0; j-- {
112+
if count == 0 {
113+
break
114+
}
115+
sum += code[j]
116+
count++
117+
}
118+
}
119+
res[i] = sum
120+
sum, count = 0, k
121+
}
122+
}
123+
return res
124+
}
125+
```
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package leetcode
2+
3+
// 解法一 DP
4+
func minimumDeletions(s string) int {
5+
prev, res, bCount := 0, 0, 0
6+
for _, c := range s {
7+
if c == 'a' {
8+
res = min(prev+1, bCount)
9+
prev = res
10+
} else {
11+
bCount++
12+
}
13+
}
14+
return res
15+
}
16+
17+
func min(a, b int) int {
18+
if a < b {
19+
return a
20+
}
21+
return b
22+
}
23+
24+
// 解法二 模拟
25+
func minimumDeletions1(s string) int {
26+
aCount, bCount, res := 0, 0, 0
27+
for i := 0; i < len(s); i++ {
28+
if s[i] == 'a' {
29+
aCount++
30+
}
31+
}
32+
res = aCount
33+
for i := 0; i < len(s); i++ {
34+
if s[i] == 'a' {
35+
aCount--
36+
} else {
37+
bCount++
38+
}
39+
res = min(res, aCount+bCount)
40+
}
41+
return res
42+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package leetcode
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
)
7+
8+
type question1649 struct {
9+
para1649
10+
ans1649
11+
}
12+
13+
// para 是参数
14+
// one 代表第一个参数
15+
type para1649 struct {
16+
s string
17+
}
18+
19+
// ans 是答案
20+
// one 代表第一个答案
21+
type ans1649 struct {
22+
one int
23+
}
24+
25+
func Test_Problem1649(t *testing.T) {
26+
27+
qs := []question1649{
28+
29+
{
30+
para1649{"aababbab"},
31+
ans1649{2},
32+
},
33+
34+
{
35+
para1649{"bbaaaaabb"},
36+
ans1649{2},
37+
},
38+
39+
{
40+
para1649{"b"},
41+
ans1649{0},
42+
},
43+
44+
{
45+
para1649{"ababaaaabbbbbaaababbbbbbaaabbaababbabbbbaabbbbaabbabbabaabbbababaa"},
46+
ans1649{25},
47+
},
48+
}
49+
50+
fmt.Printf("------------------------Leetcode Problem 1649------------------------\n")
51+
52+
for _, q := range qs {
53+
_, p := q.ans1649, q.para1649
54+
fmt.Printf("【input】:%v 【output】:%v \n", p, minimumDeletions(p.s))
55+
}
56+
fmt.Printf("\n\n\n")
57+
}

0 commit comments

Comments
 (0)