mirror of
https://github.com/halfrost/LeetCode-Go.git
synced 2025-07-07 09:54:57 +08:00
Add solution 1652、1653
This commit is contained in:
56
leetcode/1652.Defuse-the-Bomb/1652. Defuse the Bomb.go
Normal file
56
leetcode/1652.Defuse-the-Bomb/1652. Defuse the Bomb.go
Normal file
@ -0,0 +1,56 @@
|
||||
package leetcode
|
||||
|
||||
func decrypt(code []int, k int) []int {
|
||||
if k == 0 {
|
||||
for i := 0; i < len(code); i++ {
|
||||
code[i] = 0
|
||||
}
|
||||
return code
|
||||
}
|
||||
count, sum, res := k, 0, make([]int, len(code))
|
||||
if k > 0 {
|
||||
for i := 0; i < len(code); i++ {
|
||||
for j := i + 1; j < len(code); j++ {
|
||||
if count == 0 {
|
||||
break
|
||||
}
|
||||
sum += code[j]
|
||||
count--
|
||||
}
|
||||
if count > 0 {
|
||||
for j := 0; j < len(code); j++ {
|
||||
if count == 0 {
|
||||
break
|
||||
}
|
||||
sum += code[j]
|
||||
count--
|
||||
}
|
||||
}
|
||||
res[i] = sum
|
||||
sum, count = 0, k
|
||||
}
|
||||
}
|
||||
if k < 0 {
|
||||
for i := 0; i < len(code); i++ {
|
||||
for j := i - 1; j >= 0; j-- {
|
||||
if count == 0 {
|
||||
break
|
||||
}
|
||||
sum += code[j]
|
||||
count++
|
||||
}
|
||||
if count < 0 {
|
||||
for j := len(code) - 1; j >= 0; j-- {
|
||||
if count == 0 {
|
||||
break
|
||||
}
|
||||
sum += code[j]
|
||||
count++
|
||||
}
|
||||
}
|
||||
res[i] = sum
|
||||
sum, count = 0, k
|
||||
}
|
||||
}
|
||||
return res
|
||||
}
|
53
leetcode/1652.Defuse-the-Bomb/1652. Defuse the Bomb_test.go
Normal file
53
leetcode/1652.Defuse-the-Bomb/1652. Defuse the Bomb_test.go
Normal file
@ -0,0 +1,53 @@
|
||||
package leetcode
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
)
|
||||
|
||||
type question1652 struct {
|
||||
para1652
|
||||
ans1652
|
||||
}
|
||||
|
||||
// para 是参数
|
||||
// one 代表第一个参数
|
||||
type para1652 struct {
|
||||
code []int
|
||||
k int
|
||||
}
|
||||
|
||||
// ans 是答案
|
||||
// one 代表第一个答案
|
||||
type ans1652 struct {
|
||||
one []int
|
||||
}
|
||||
|
||||
func Test_Problem1652(t *testing.T) {
|
||||
|
||||
qs := []question1652{
|
||||
|
||||
{
|
||||
para1652{[]int{5, 7, 1, 4}, 3},
|
||||
ans1652{[]int{12, 10, 16, 13}},
|
||||
},
|
||||
|
||||
{
|
||||
para1652{[]int{1, 2, 3, 4}, 0},
|
||||
ans1652{[]int{0, 0, 0, 0}},
|
||||
},
|
||||
|
||||
{
|
||||
para1652{[]int{2, 4, 9, 3}, -2},
|
||||
ans1652{[]int{12, 5, 6, 13}},
|
||||
},
|
||||
}
|
||||
|
||||
fmt.Printf("------------------------Leetcode Problem 1652------------------------\n")
|
||||
|
||||
for _, q := range qs {
|
||||
_, p := q.ans1652, q.para1652
|
||||
fmt.Printf("【input】:%v 【output】:%v \n", p, decrypt(p.code, p.k))
|
||||
}
|
||||
fmt.Printf("\n\n\n")
|
||||
}
|
125
leetcode/1652.Defuse-the-Bomb/README.md
Normal file
125
leetcode/1652.Defuse-the-Bomb/README.md
Normal file
@ -0,0 +1,125 @@
|
||||
# [1652. Defuse the Bomb](https://leetcode.com/problems/defuse-the-bomb/)
|
||||
|
||||
|
||||
## 题目
|
||||
|
||||
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: 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:**
|
||||
|
||||
```
|
||||
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.
|
||||
```
|
||||
|
||||
**Constraints:**
|
||||
|
||||
- `n == code.length`
|
||||
- `1 <= n <= 100`
|
||||
- `1 <= code[i] <= 100`
|
||||
- `(n - 1) <= k <= n - 1`
|
||||
|
||||
## 题目大意
|
||||
|
||||
你有一个炸弹需要拆除,时间紧迫!你的情报员会给你一个长度为 n 的 循环 数组 code 以及一个密钥 k 。为了获得正确的密码,你需要替换掉每一个数字。所有数字会 同时 被替换。
|
||||
|
||||
- 如果 k > 0 ,将第 i 个数字用 接下来 k 个数字之和替换。
|
||||
- 如果 k < 0 ,将第 i 个数字用 之前 k 个数字之和替换。
|
||||
- 如果 k == 0 ,将第 i 个数字用 0 替换。
|
||||
|
||||
由于 code 是循环的, code[n-1] 下一个元素是 code[0] ,且 code[0] 前一个元素是 code[n-1] 。
|
||||
|
||||
给你 循环 数组 code 和整数密钥 k ,请你返回解密后的结果来拆除炸弹!
|
||||
|
||||
## 解题思路
|
||||
|
||||
- 给出一个 code 数组,要求按照规则替换每个字母。
|
||||
- 简单题,按照题意描述循环即可。
|
||||
|
||||
## 代码
|
||||
|
||||
```go
|
||||
package leetcode
|
||||
|
||||
func decrypt(code []int, k int) []int {
|
||||
if k == 0 {
|
||||
for i := 0; i < len(code); i++ {
|
||||
code[i] = 0
|
||||
}
|
||||
return code
|
||||
}
|
||||
count, sum, res := k, 0, make([]int, len(code))
|
||||
if k > 0 {
|
||||
for i := 0; i < len(code); i++ {
|
||||
for j := i + 1; j < len(code); j++ {
|
||||
if count == 0 {
|
||||
break
|
||||
}
|
||||
sum += code[j]
|
||||
count--
|
||||
}
|
||||
if count > 0 {
|
||||
for j := 0; j < len(code); j++ {
|
||||
if count == 0 {
|
||||
break
|
||||
}
|
||||
sum += code[j]
|
||||
count--
|
||||
}
|
||||
}
|
||||
res[i] = sum
|
||||
sum, count = 0, k
|
||||
}
|
||||
}
|
||||
if k < 0 {
|
||||
for i := 0; i < len(code); i++ {
|
||||
for j := i - 1; j >= 0; j-- {
|
||||
if count == 0 {
|
||||
break
|
||||
}
|
||||
sum += code[j]
|
||||
count++
|
||||
}
|
||||
if count < 0 {
|
||||
for j := len(code) - 1; j >= 0; j-- {
|
||||
if count == 0 {
|
||||
break
|
||||
}
|
||||
sum += code[j]
|
||||
count++
|
||||
}
|
||||
}
|
||||
res[i] = sum
|
||||
sum, count = 0, k
|
||||
}
|
||||
}
|
||||
return res
|
||||
}
|
||||
```
|
@ -0,0 +1,42 @@
|
||||
package leetcode
|
||||
|
||||
// 解法一 DP
|
||||
func minimumDeletions(s string) int {
|
||||
prev, res, bCount := 0, 0, 0
|
||||
for _, c := range s {
|
||||
if c == 'a' {
|
||||
res = min(prev+1, bCount)
|
||||
prev = res
|
||||
} else {
|
||||
bCount++
|
||||
}
|
||||
}
|
||||
return res
|
||||
}
|
||||
|
||||
func min(a, b int) int {
|
||||
if a < b {
|
||||
return a
|
||||
}
|
||||
return b
|
||||
}
|
||||
|
||||
// 解法二 模拟
|
||||
func minimumDeletions1(s string) int {
|
||||
aCount, bCount, res := 0, 0, 0
|
||||
for i := 0; i < len(s); i++ {
|
||||
if s[i] == 'a' {
|
||||
aCount++
|
||||
}
|
||||
}
|
||||
res = aCount
|
||||
for i := 0; i < len(s); i++ {
|
||||
if s[i] == 'a' {
|
||||
aCount--
|
||||
} else {
|
||||
bCount++
|
||||
}
|
||||
res = min(res, aCount+bCount)
|
||||
}
|
||||
return res
|
||||
}
|
@ -0,0 +1,57 @@
|
||||
package leetcode
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
)
|
||||
|
||||
type question1649 struct {
|
||||
para1649
|
||||
ans1649
|
||||
}
|
||||
|
||||
// para 是参数
|
||||
// one 代表第一个参数
|
||||
type para1649 struct {
|
||||
s string
|
||||
}
|
||||
|
||||
// ans 是答案
|
||||
// one 代表第一个答案
|
||||
type ans1649 struct {
|
||||
one int
|
||||
}
|
||||
|
||||
func Test_Problem1649(t *testing.T) {
|
||||
|
||||
qs := []question1649{
|
||||
|
||||
{
|
||||
para1649{"aababbab"},
|
||||
ans1649{2},
|
||||
},
|
||||
|
||||
{
|
||||
para1649{"bbaaaaabb"},
|
||||
ans1649{2},
|
||||
},
|
||||
|
||||
{
|
||||
para1649{"b"},
|
||||
ans1649{0},
|
||||
},
|
||||
|
||||
{
|
||||
para1649{"ababaaaabbbbbaaababbbbbbaaabbaababbabbbbaabbbbaabbabbabaabbbababaa"},
|
||||
ans1649{25},
|
||||
},
|
||||
}
|
||||
|
||||
fmt.Printf("------------------------Leetcode Problem 1649------------------------\n")
|
||||
|
||||
for _, q := range qs {
|
||||
_, p := q.ans1649, q.para1649
|
||||
fmt.Printf("【input】:%v 【output】:%v \n", p, minimumDeletions(p.s))
|
||||
}
|
||||
fmt.Printf("\n\n\n")
|
||||
}
|
@ -0,0 +1,90 @@
|
||||
# [1653. Minimum Deletions to Make String Balanced](https://leetcode.com/problems/minimum-deletions-to-make-string-balanced/)
|
||||
|
||||
|
||||
## 题目
|
||||
|
||||
You are given a string `s` consisting only of characters `'a'` and `'b'`.
|
||||
|
||||
You can delete any number of characters in `s` to make `s` **balanced**. `s` is **balanced** if there is no pair of indices `(i,j)` such that `i < j` and `s[i] = 'b'` and `s[j]= 'a'`.
|
||||
|
||||
Return *the **minimum** number of deletions needed to make* `s` ***balanced***.
|
||||
|
||||
**Example 1:**
|
||||
|
||||
```
|
||||
Input: s = "aababbab"
|
||||
Output: 2
|
||||
Explanation: You can either:
|
||||
Delete the characters at 0-indexed positions 2 and 6 ("aababbab" -> "aaabbb"), or
|
||||
Delete the characters at 0-indexed positions 3 and 6 ("aababbab" -> "aabbbb").
|
||||
```
|
||||
|
||||
**Example 2:**
|
||||
|
||||
```
|
||||
Input: s = "bbaaaaabb"
|
||||
Output: 2
|
||||
Explanation: The only solution is to delete the first two characters.
|
||||
```
|
||||
|
||||
**Constraints:**
|
||||
|
||||
- `1 <= s.length <= 105`
|
||||
- `s[i]` is `'a'` or `'b'`.
|
||||
|
||||
## 题目大意
|
||||
|
||||
给你一个字符串 s ,它仅包含字符 'a' 和 'b' 。你可以删除 s 中任意数目的字符,使得 s 平衡 。我们称 s 平衡的 当不存在下标对 (i,j) 满足 i < j 且 s[i] = 'b' 同时 s[j]= 'a' 。请你返回使 s 平衡 的 最少 删除次数。
|
||||
|
||||
## 解题思路
|
||||
|
||||
- 给定一个字符串,要求删除最少次数,使得字母 a 都排在字母 b 的前面。
|
||||
- 很容易想到的一个解题思路是 DP。定义 `dp[i]` 为字符串下标 [ 0, i ] 这个区间内使得字符串平衡的最少删除次数。当 `s[i] == 'a'` 的时候,有 2 种情况,一种是 `s[i]` 前面全是 `[aa……aa]` 的情况,这个时候只需要把其中的所有的字母 `b` 删除即可。还有一种情况是 `s[i]` 前面有字母 `a` 也有字母 `b`,即 `[aaa……abb……b]`,这种情况就需要考虑 `dp[i-1]` 了。当前字母是 `a`,那么肯定要删除字母 `a`,来维持前面有一段字母 `b` 的情况。当 `s[i] == 'b'` 的时候,不管是 `[aa……aa]` 这种情况,还是 `[aaa……abb……b]` 这种情况,当前字母 `b` 都可以直接附加在后面,也能保证整个字符串是平衡的。所以状态转移方程为 `dp[i+1] = min(dp[i] + 1, bCount), s[i] == 'a'`,`dp[i+1] = dp[i], s[i] == 'b'`。最终答案存在 `dp[n]` 中。由于前后项的递推关系中只用到一次前一项,所以我们还可以优化一下空间,用一个变量保存前一项的结果。优化以后的代码见解法一。
|
||||
- 这一题还有一个模拟的思路。题目要求找到最小删除字数,那么就是要找到一个“临界点”,在这个临界点的左边删除所有的字母 b,在这个临界点的右边删除所有的字母 a。在所有的“临界点”中找到删除最少的次数。代码实现见解法二。
|
||||
|
||||
## 代码
|
||||
|
||||
```go
|
||||
package leetcode
|
||||
|
||||
// 解法一 DP
|
||||
func minimumDeletions(s string) int {
|
||||
prev, res, bCount := 0, 0, 0
|
||||
for _, c := range s {
|
||||
if c == 'a' {
|
||||
res = min(prev+1, bCount)
|
||||
prev = res
|
||||
} else {
|
||||
bCount++
|
||||
}
|
||||
}
|
||||
return res
|
||||
}
|
||||
|
||||
func min(a, b int) int {
|
||||
if a < b {
|
||||
return a
|
||||
}
|
||||
return b
|
||||
}
|
||||
|
||||
// 解法二 模拟
|
||||
func minimumDeletions1(s string) int {
|
||||
aCount, bCount, res := 0, 0, 0
|
||||
for i := 0; i < len(s); i++ {
|
||||
if s[i] == 'a' {
|
||||
aCount++
|
||||
}
|
||||
}
|
||||
res = aCount
|
||||
for i := 0; i < len(s); i++ {
|
||||
if s[i] == 'a' {
|
||||
aCount--
|
||||
} else {
|
||||
bCount++
|
||||
}
|
||||
res = min(res, aCount+bCount)
|
||||
}
|
||||
return res
|
||||
}
|
||||
```
|
Reference in New Issue
Block a user