Add solution 1654、1655

This commit is contained in:
YDZ
2020-11-28 15:24:45 +08:00
parent 0c4b373319
commit 9ac3fdeb96
16 changed files with 844 additions and 232 deletions

View File

@ -5,52 +5,52 @@ import (
"testing" "testing"
) )
type question1649 struct { type question1653 struct {
para1649 para1653
ans1649 ans1653
} }
// para 是参数 // para 是参数
// one 代表第一个参数 // one 代表第一个参数
type para1649 struct { type para1653 struct {
s string s string
} }
// ans 是答案 // ans 是答案
// one 代表第一个答案 // one 代表第一个答案
type ans1649 struct { type ans1653 struct {
one int one int
} }
func Test_Problem1649(t *testing.T) { func Test_Problem1653(t *testing.T) {
qs := []question1649{ qs := []question1653{
{ {
para1649{"aababbab"}, para1653{"aababbab"},
ans1649{2}, ans1653{2},
}, },
{ {
para1649{"bbaaaaabb"}, para1653{"bbaaaaabb"},
ans1649{2}, ans1653{2},
}, },
{ {
para1649{"b"}, para1653{"b"},
ans1649{0}, ans1653{0},
}, },
{ {
para1649{"ababaaaabbbbbaaababbbbbbaaabbaababbabbbbaabbbbaabbabbabaabbbababaa"}, para1653{"ababaaaabbbbbaaababbbbbbaaabbaababbabbbbaabbbbaabbabbabaabbbababaa"},
ans1649{25}, ans1653{25},
}, },
} }
fmt.Printf("------------------------Leetcode Problem 1649------------------------\n") fmt.Printf("------------------------Leetcode Problem 1653------------------------\n")
for _, q := range qs { for _, q := range qs {
_, p := q.ans1649, q.para1649 _, p := q.ans1653, q.para1653
fmt.Printf("【input】:%v 【output】:%v \n", p, minimumDeletions(p.s)) fmt.Printf("【input】:%v 【output】:%v \n", p, minimumDeletions(p.s))
} }
fmt.Printf("\n\n\n") fmt.Printf("\n\n\n")

View File

@ -0,0 +1,29 @@
package leetcode
func minimumJumps(forbidden []int, a int, b int, x int) int {
visited := make([]bool, 6000)
for i := range forbidden {
visited[forbidden[i]] = true
}
queue, res := [][2]int{{0, 0}}, -1
for len(queue) > 0 {
length := len(queue)
res++
for i := 0; i < length; i++ {
cur, isBack := queue[i][0], queue[i][1]
if cur == x {
return res
}
if isBack == 0 && cur-b > 0 && !visited[cur-b] {
visited[cur-b] = true
queue = append(queue, [2]int{cur - b, 1})
}
if cur+a < len(visited) && !visited[cur+a] {
visited[cur+a] = true
queue = append(queue, [2]int{cur + a, 0})
}
}
queue = queue[length:]
}
return -1
}

View File

@ -0,0 +1,60 @@
package leetcode
import (
"fmt"
"testing"
)
type question1654 struct {
para1654
ans1654
}
// para 是参数
// one 代表第一个参数
type para1654 struct {
forbidden []int
a int
b int
x int
}
// ans 是答案
// one 代表第一个答案
type ans1654 struct {
one int
}
func Test_Problem1654(t *testing.T) {
qs := []question1654{
{
para1654{[]int{14, 4, 18, 1, 15}, 3, 15, 9},
ans1654{3},
},
{
para1654{[]int{8, 3, 16, 6, 12, 20}, 15, 13, 11},
ans1654{-1},
},
{
para1654{[]int{1, 6, 2, 14, 5, 17, 4}, 16, 9, 7},
ans1654{2},
},
{
para1654{[]int{1998}, 1999, 2000, 2000},
ans1654{3998},
},
}
fmt.Printf("------------------------Leetcode Problem 1654------------------------\n")
for _, q := range qs {
_, p := q.ans1654, q.para1654
fmt.Printf("【input】:%v 【output】:%v \n", p, minimumJumps(p.forbidden, p.a, p.b, p.x))
}
fmt.Printf("\n\n\n")
}

View File

@ -0,0 +1,101 @@
# [1654. Minimum Jumps to Reach Home](https://leetcode.com/problems/minimum-jumps-to-reach-home/)
## 题目
A certain bug's home is on the x-axis at position `x`. Help them get there from position `0`.
The bug jumps according to the following rules:
- It can jump exactly `a` positions **forward** (to the right).
- It can jump exactly `b` positions **backward** (to the left).
- It cannot jump backward twice in a row.
- It cannot jump to any `forbidden` positions.
The bug may jump forward **beyond** its home, but it **cannot jump** to positions numbered with **negative** integers.
Given an array of integers `forbidden`, where `forbidden[i]` means that the bug cannot jump to the position `forbidden[i]`, and integers `a`, `b`, and `x`, return *the minimum number of jumps needed for the bug to reach its home*. If there is no possible sequence of jumps that lands the bug on position `x`, return `1.`
**Example 1:**
```
Input: forbidden = [14,4,18,1,15], a = 3, b = 15, x = 9
Output: 3
Explanation: 3 jumps forward (0 -> 3 -> 6 -> 9) will get the bug home.
```
**Example 2:**
```
Input: forbidden = [8,3,16,6,12,20], a = 15, b = 13, x = 11
Output: -1
```
**Example 3:**
```
Input: forbidden = [1,6,2,14,5,17,4], a = 16, b = 9, x = 7
Output: 2
Explanation: One jump forward (0 -> 16) then one jump backward (16 -> 7) will get the bug home.
```
**Constraints:**
- `1 <= forbidden.length <= 1000`
- `1 <= a, b, forbidden[i] <= 2000`
- `0 <= x <= 2000`
- All the elements in `forbidden` are distinct.
- Position `x` is not forbidden.
## 题目大意
有一只跳蚤的家在数轴上的位置 x 处。请你帮助它从位置 0 出发到达它的家。
跳蚤跳跃的规则如下:
- 它可以 往前 跳恰好 a 个位置即往右跳
- 它可以 往后 跳恰好 b 个位置即往左跳
- 它不能 连续 往后跳 2 次。
- 它不能跳到任何 forbidden 数组中的位置。
跳蚤可以往前跳 超过 它的家的位置,但是它 不能跳到负整数 的位置。给你一个整数数组 forbidden 其中 forbidden[i] 是跳蚤不能跳到的位置同时给你整数 a b  x 请你返回跳蚤到家的最少跳跃次数。如果没有恰好到达 x 的可行方案请你返回 -1 。
## 解题思路
- 给出坐标 x ,可以往前跳的步长 a往后跳的步长 b。要求输出能跳回家的最少跳跃次数。
- 求最少跳跃次数,思路用 BFS 求解,最先到达坐标 x 的方案即是最少跳跃次数。对`forbidden` 的处理是把记忆化数组里面把他们标记为 true。禁止连续往后跳 2 次的限制,要求我们在 BFS 入队的时候再记录一下跳跃方向,每次往后跳的时候判断前一跳是否是往后跳,如果是往后跳,此次就不能往后跳了。
## 代码
```go
package leetcode
func minimumJumps(forbidden []int, a int, b int, x int) int {
visited := make([]bool, 6000)
for i := range forbidden {
visited[forbidden[i]] = true
}
queue, res := [][2]int{{0, 0}}, -1
for len(queue) > 0 {
length := len(queue)
res++
for i := 0; i < length; i++ {
cur, isBack := queue[i][0], queue[i][1]
if cur == x {
return res
}
if isBack == 0 && cur-b > 0 && !visited[cur-b] {
visited[cur-b] = true
queue = append(queue, [2]int{cur - b, 1})
}
if cur+a < len(visited) && !visited[cur+a] {
visited[cur+a] = true
queue = append(queue, [2]int{cur + a, 0})
}
}
queue = queue[length:]
}
return -1
}
```

View File

@ -0,0 +1,30 @@
package leetcode
func canDistribute(nums []int, quantity []int) bool {
freq := make(map[int]int)
for _, n := range nums {
freq[n]++
}
return dfs(freq, quantity)
}
func dfs(freq map[int]int, quantity []int) bool {
if len(quantity) == 0 {
return true
}
visited := make(map[int]bool)
for i := range freq {
if visited[freq[i]] {
continue
}
visited[freq[i]] = true
if freq[i] >= quantity[0] {
freq[i] -= quantity[0]
if dfs(freq, quantity[1:]) {
return true
}
freq[i] += quantity[0]
}
}
return false
}

View File

@ -0,0 +1,63 @@
package leetcode
import (
"fmt"
"testing"
)
type question1655 struct {
para1655
ans1655
}
// para 是参数
// one 代表第一个参数
type para1655 struct {
nums []int
quantity []int
}
// ans 是答案
// one 代表第一个答案
type ans1655 struct {
one bool
}
func Test_Problem1655(t *testing.T) {
qs := []question1655{
{
para1655{[]int{1, 2, 3, 4}, []int{2}},
ans1655{false},
},
{
para1655{[]int{1, 2, 3, 3}, []int{2}},
ans1655{true},
},
{
para1655{[]int{1, 1, 2, 2}, []int{2, 2}},
ans1655{true},
},
{
para1655{[]int{1, 1, 2, 3}, []int{2, 2}},
ans1655{false},
},
{
para1655{[]int{1, 1, 1, 1, 1}, []int{2, 3}},
ans1655{true},
},
}
fmt.Printf("------------------------Leetcode Problem 1655------------------------\n")
for _, q := range qs {
_, p := q.ans1655, q.para1655
fmt.Printf("【input】:%v 【output】:%v \n", p, canDistribute(p.nums, p.quantity))
}
fmt.Printf("\n\n\n")
}

View File

@ -0,0 +1,112 @@
# [1655. Distribute Repeating Integers](https://leetcode.com/problems/distribute-repeating-integers/)
## 题目
You are given an array of `n` integers, `nums`, where there are at most `50` unique values in the array. You are also given an array of `m` customer order quantities, `quantity`, where `quantity[i]` is the amount of integers the `ith` customer ordered. Determine if it is possible to distribute `nums` such that:
- The `ith` customer gets **exactly** `quantity[i]` integers,
- The integers the `ith` customer gets are **all equal**, and
- Every customer is satisfied.
Return `true` *if it is possible to distribute* `nums` *according to the above conditions*.
**Example 1:**
```
Input: nums = [1,2,3,4], quantity = [2]
Output: false
Explanation: The 0th customer cannot be given two different integers.
```
**Example 2:**
```
Input: nums = [1,2,3,3], quantity = [2]
Output: true
Explanation: The 0th customer is given [3,3]. The integers [1,2] are not used.
```
**Example 3:**
```
Input: nums = [1,1,2,2], quantity = [2,2]
Output: true
Explanation: The 0th customer is given [1,1], and the 1st customer is given [2,2].
```
**Example 4:**
```
Input: nums = [1,1,2,3], quantity = [2,2]
Output: false
Explanation: Although the 0th customer could be given [1,1], the 1st customer cannot be satisfied.
```
**Example 5:**
```
Input: nums = [1,1,1,1,1], quantity = [2,3]
Output: true
Explanation: The 0th customer is given [1,1], and the 1st customer is given [1,1,1].
```
**Constraints:**
- `n == nums.length`
- `1 <= n <= 105`
- `1 <= nums[i] <= 1000`
- `m == quantity.length`
- `1 <= m <= 10`
- `1 <= quantity[i] <= 105`
- There are at most `50` unique values in `nums`.
## 题目大意
给你一个长度为 n 的整数数组 nums 这个数组中至多有 50 个不同的值。同时你有 m 个顾客的订单 quantity 其中整数 quantity[i] 是第 i 位顾客订单的数目。请你判断是否能将 nums 中的整数分配给这些顾客且满足
- i 位顾客 恰好  quantity[i] 个整数。
- i 位顾客拿到的整数都是 相同的 。
- 每位顾客都满足上述两个要求。
如果你可以分配 nums 中的整数满足上面的要求那么请返回 true 否则返回 false 
## 解题思路
- 给定一个数组 nums订单数组 quantity要求按照订单满足顾客的需求。如果能满足输出 true不能满足输出 false。
- 用 DFS 记忆化暴力搜索。代码实现不难。(不知道此题为什么是 Hard
## 代码
```go
package leetcode
func canDistribute(nums []int, quantity []int) bool {
freq := make(map[int]int)
for _, n := range nums {
freq[n]++
}
return dfs(freq, quantity)
}
func dfs(freq map[int]int, quantity []int) bool {
if len(quantity) == 0 {
return true
}
visited := make(map[int]bool)
for i := range freq {
if visited[freq[i]] {
continue
}
visited[freq[i]] = true
if freq[i] >= quantity[0] {
freq[i] -= quantity[0]
if dfs(freq, quantity[1:]) {
return true
}
freq[i] += quantity[0]
}
}
return false
}
```

View File

@ -1,56 +0,0 @@
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
}

View File

@ -1,53 +0,0 @@
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")
}

View File

@ -1,49 +0,0 @@
package leetcode
func minimumDeletions(s string) int {
ai, bi, sum, temp, array := 0, 0, 0, 0, []int{}
for ai = 0; ai < len(s); ai++ {
if s[ai] == 'a' {
break
}
}
if ai != 0 && ai != len(s) {
sum += ai
}
for bi = ai; bi < len(s); bi++ {
if s[bi] == 'b' {
break
}
}
if s[bi-1] == 'a' {
ai = bi - 1
}
if s[bi-1] == 'b' && bi != len(s) {
ai = bi + 1
}
for j := bi; j < len(s); j++ {
if s[j] == 'b' {
temp++
}
if s[j] == 'a' && temp != 0 {
array = append(array, temp)
temp = 0
}
}
if len(array) == 0 {
return sum
}
dp := make([]int, len(array))
dp[0] = min(array[0], len(array))
for i := 1; i < len(array); i++ {
dp[i] = min(dp[i-1]+array[i], dp[i-1]+len(array)-(i+1)+1)
}
return sum + dp[len(array)-1]
}
func min(a int, b int) int {
if a > b {
return b
}
return a
}

View File

@ -1,57 +0,0 @@
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")
}

View 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
}
```

View File

@ -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
}
```

View File

@ -0,0 +1,101 @@
# [1654. Minimum Jumps to Reach Home](https://leetcode.com/problems/minimum-jumps-to-reach-home/)
## 题目
A certain bug's home is on the x-axis at position `x`. Help them get there from position `0`.
The bug jumps according to the following rules:
- It can jump exactly `a` positions **forward** (to the right).
- It can jump exactly `b` positions **backward** (to the left).
- It cannot jump backward twice in a row.
- It cannot jump to any `forbidden` positions.
The bug may jump forward **beyond** its home, but it **cannot jump** to positions numbered with **negative** integers.
Given an array of integers `forbidden`, where `forbidden[i]` means that the bug cannot jump to the position `forbidden[i]`, and integers `a`, `b`, and `x`, return *the minimum number of jumps needed for the bug to reach its home*. If there is no possible sequence of jumps that lands the bug on position `x`, return `1.`
**Example 1:**
```
Input: forbidden = [14,4,18,1,15], a = 3, b = 15, x = 9
Output: 3
Explanation: 3 jumps forward (0 -> 3 -> 6 -> 9) will get the bug home.
```
**Example 2:**
```
Input: forbidden = [8,3,16,6,12,20], a = 15, b = 13, x = 11
Output: -1
```
**Example 3:**
```
Input: forbidden = [1,6,2,14,5,17,4], a = 16, b = 9, x = 7
Output: 2
Explanation: One jump forward (0 -> 16) then one jump backward (16 -> 7) will get the bug home.
```
**Constraints:**
- `1 <= forbidden.length <= 1000`
- `1 <= a, b, forbidden[i] <= 2000`
- `0 <= x <= 2000`
- All the elements in `forbidden` are distinct.
- Position `x` is not forbidden.
## 题目大意
有一只跳蚤的家在数轴上的位置 x 处。请你帮助它从位置 0 出发到达它的家。
跳蚤跳跃的规则如下:
- 它可以 往前 跳恰好 a 个位置即往右跳
- 它可以 往后 跳恰好 b 个位置即往左跳
- 它不能 连续 往后跳 2 次。
- 它不能跳到任何 forbidden 数组中的位置。
跳蚤可以往前跳 超过 它的家的位置,但是它 不能跳到负整数 的位置。给你一个整数数组 forbidden 其中 forbidden[i] 是跳蚤不能跳到的位置同时给你整数 a b  x 请你返回跳蚤到家的最少跳跃次数。如果没有恰好到达 x 的可行方案请你返回 -1 。
## 解题思路
- 给出坐标 x ,可以往前跳的步长 a往后跳的步长 b。要求输出能跳回家的最少跳跃次数。
- 求最少跳跃次数,思路用 BFS 求解,最先到达坐标 x 的方案即是最少跳跃次数。对`forbidden` 的处理是把记忆化数组里面把他们标记为 true。禁止连续往后跳 2 次的限制,要求我们在 BFS 入队的时候再记录一下跳跃方向,每次往后跳的时候判断前一跳是否是往后跳,如果是往后跳,此次就不能往后跳了。
## 代码
```go
package leetcode
func minimumJumps(forbidden []int, a int, b int, x int) int {
visited := make([]bool, 6000)
for i := range forbidden {
visited[forbidden[i]] = true
}
queue, res := [][2]int{{0, 0}}, -1
for len(queue) > 0 {
length := len(queue)
res++
for i := 0; i < length; i++ {
cur, isBack := queue[i][0], queue[i][1]
if cur == x {
return res
}
if isBack == 0 && cur-b > 0 && !visited[cur-b] {
visited[cur-b] = true
queue = append(queue, [2]int{cur - b, 1})
}
if cur+a < len(visited) && !visited[cur+a] {
visited[cur+a] = true
queue = append(queue, [2]int{cur + a, 0})
}
}
queue = queue[length:]
}
return -1
}
```

View File

@ -0,0 +1,112 @@
# [1655. Distribute Repeating Integers](https://leetcode.com/problems/distribute-repeating-integers/)
## 题目
You are given an array of `n` integers, `nums`, where there are at most `50` unique values in the array. You are also given an array of `m` customer order quantities, `quantity`, where `quantity[i]` is the amount of integers the `ith` customer ordered. Determine if it is possible to distribute `nums` such that:
- The `ith` customer gets **exactly** `quantity[i]` integers,
- The integers the `ith` customer gets are **all equal**, and
- Every customer is satisfied.
Return `true` *if it is possible to distribute* `nums` *according to the above conditions*.
**Example 1:**
```
Input: nums = [1,2,3,4], quantity = [2]
Output: false
Explanation: The 0th customer cannot be given two different integers.
```
**Example 2:**
```
Input: nums = [1,2,3,3], quantity = [2]
Output: true
Explanation: The 0th customer is given [3,3]. The integers [1,2] are not used.
```
**Example 3:**
```
Input: nums = [1,1,2,2], quantity = [2,2]
Output: true
Explanation: The 0th customer is given [1,1], and the 1st customer is given [2,2].
```
**Example 4:**
```
Input: nums = [1,1,2,3], quantity = [2,2]
Output: false
Explanation: Although the 0th customer could be given [1,1], the 1st customer cannot be satisfied.
```
**Example 5:**
```
Input: nums = [1,1,1,1,1], quantity = [2,3]
Output: true
Explanation: The 0th customer is given [1,1], and the 1st customer is given [1,1,1].
```
**Constraints:**
- `n == nums.length`
- `1 <= n <= 105`
- `1 <= nums[i] <= 1000`
- `m == quantity.length`
- `1 <= m <= 10`
- `1 <= quantity[i] <= 105`
- There are at most `50` unique values in `nums`.
## 题目大意
给你一个长度为 n 的整数数组 nums 这个数组中至多有 50 个不同的值。同时你有 m 个顾客的订单 quantity 其中整数 quantity[i] 是第 i 位顾客订单的数目。请你判断是否能将 nums 中的整数分配给这些顾客且满足
- i 位顾客 恰好  quantity[i] 个整数。
- i 位顾客拿到的整数都是 相同的 。
- 每位顾客都满足上述两个要求。
如果你可以分配 nums 中的整数满足上面的要求那么请返回 true 否则返回 false 
## 解题思路
- 给定一个数组 nums订单数组 quantity要求按照订单满足顾客的需求。如果能满足输出 true不能满足输出 false。
- 用 DFS 记忆化暴力搜索。代码实现不难。(不知道此题为什么是 Hard
## 代码
```go
package leetcode
func canDistribute(nums []int, quantity []int) bool {
freq := make(map[int]int)
for _, n := range nums {
freq[n]++
}
return dfs(freq, quantity)
}
func dfs(freq map[int]int, quantity []int) bool {
if len(quantity) == 0 {
return true
}
visited := make(map[int]bool)
for i := range freq {
if visited[freq[i]] {
continue
}
visited[freq[i]] = true
if freq[i] >= quantity[0] {
freq[i] -= quantity[0]
if dfs(freq, quantity[1:]) {
return true
}
freq[i] += quantity[0]
}
}
return false
}
```

View File

@ -552,6 +552,10 @@ headless: true
- [1647.Minimum-Deletions-to-Make-Character-Frequencies-Unique]({{< relref "/ChapterFour/1647.Minimum-Deletions-to-Make-Character-Frequencies-Unique.md" >}}) - [1647.Minimum-Deletions-to-Make-Character-Frequencies-Unique]({{< relref "/ChapterFour/1647.Minimum-Deletions-to-Make-Character-Frequencies-Unique.md" >}})
- [1648.Sell-Diminishing-Valued-Colored-Balls]({{< relref "/ChapterFour/1648.Sell-Diminishing-Valued-Colored-Balls.md" >}}) - [1648.Sell-Diminishing-Valued-Colored-Balls]({{< relref "/ChapterFour/1648.Sell-Diminishing-Valued-Colored-Balls.md" >}})
- [1649.Create-Sorted-Array-through-Instructions]({{< relref "/ChapterFour/1649.Create-Sorted-Array-through-Instructions.md" >}}) - [1649.Create-Sorted-Array-through-Instructions]({{< relref "/ChapterFour/1649.Create-Sorted-Array-through-Instructions.md" >}})
- [1652.Defuse-the-Bomb]({{< relref "/ChapterFour/1652.Defuse-the-Bomb.md" >}})
- [1653.Minimum-Deletions-to-Make-String-Balanced]({{< relref "/ChapterFour/1653.Minimum-Deletions-to-Make-String-Balanced.md" >}})
- [1654.Minimum-Jumps-to-Reach-Home]({{< relref "/ChapterFour/1654.Minimum-Jumps-to-Reach-Home.md" >}})
- [1655.Distribute-Repeating-Integers]({{< relref "/ChapterFour/1655.Distribute-Repeating-Integers.md" >}})
- [1656.Design-an-Ordered-Stream]({{< relref "/ChapterFour/1656.Design-an-Ordered-Stream.md" >}}) - [1656.Design-an-Ordered-Stream]({{< relref "/ChapterFour/1656.Design-an-Ordered-Stream.md" >}})
- [1657.Determine-if-Two-Strings-Are-Close]({{< relref "/ChapterFour/1657.Determine-if-Two-Strings-Are-Close.md" >}}) - [1657.Determine-if-Two-Strings-Are-Close]({{< relref "/ChapterFour/1657.Determine-if-Two-Strings-Are-Close.md" >}})
- [1658.Minimum-Operations-to-Reduce-X-to-Zero]({{< relref "/ChapterFour/1658.Minimum-Operations-to-Reduce-X-to-Zero.md" >}}) - [1658.Minimum-Operations-to-Reduce-X-to-Zero]({{< relref "/ChapterFour/1658.Minimum-Operations-to-Reduce-X-to-Zero.md" >}})