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"
)
type question1649 struct {
para1649
ans1649
type question1653 struct {
para1653
ans1653
}
// para 是参数
// one 代表第一个参数
type para1649 struct {
type para1653 struct {
s string
}
// ans 是答案
// one 代表第一个答案
type ans1649 struct {
type ans1653 struct {
one int
}
func Test_Problem1649(t *testing.T) {
func Test_Problem1653(t *testing.T) {
qs := []question1649{
qs := []question1653{
{
para1649{"aababbab"},
ans1649{2},
para1653{"aababbab"},
ans1653{2},
},
{
para1649{"bbaaaaabb"},
ans1649{2},
para1653{"bbaaaaabb"},
ans1653{2},
},
{
para1649{"b"},
ans1649{0},
para1653{"b"},
ans1653{0},
},
{
para1649{"ababaaaabbbbbaaababbbbbbaaabbaababbabbbbaabbbbaabbabbabaabbbababaa"},
ans1649{25},
para1653{"ababaaaabbbbbaaababbbbbbaaabbaababbabbbbaabbbbaabbabbabaabbbababaa"},
ans1653{25},
},
}
fmt.Printf("------------------------Leetcode Problem 1649------------------------\n")
fmt.Printf("------------------------Leetcode Problem 1653------------------------\n")
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("\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")
}