mirror of
https://github.com/halfrost/LeetCode-Go.git
synced 2025-07-06 09:23:19 +08:00
Add solution 1603、1608、1700、1710、1716、1720、1725、1732、1736、1742、1748、1752、1758
This commit is contained in:
@ -0,0 +1,51 @@
|
||||
package leetcode
|
||||
|
||||
type ParkingSystem struct {
|
||||
Big int
|
||||
Medium int
|
||||
Small int
|
||||
}
|
||||
|
||||
func Constructor(big int, medium int, small int) ParkingSystem {
|
||||
return ParkingSystem{
|
||||
Big: big,
|
||||
Medium: medium,
|
||||
Small: small,
|
||||
}
|
||||
}
|
||||
|
||||
func (this *ParkingSystem) AddCar(carType int) bool {
|
||||
switch carType {
|
||||
case 1:
|
||||
{
|
||||
if this.Big > 0 {
|
||||
this.Big--
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
case 2:
|
||||
{
|
||||
if this.Medium > 0 {
|
||||
this.Medium--
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
case 3:
|
||||
{
|
||||
if this.Small > 0 {
|
||||
this.Small--
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
/**
|
||||
* Your ParkingSystem object will be instantiated and called as such:
|
||||
* obj := Constructor(big, medium, small);
|
||||
* param_1 := obj.AddCar(carType);
|
||||
*/
|
@ -0,0 +1,15 @@
|
||||
package leetcode
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func Test_Problem1603(t *testing.T) {
|
||||
obj := Constructor(1, 1, 0)
|
||||
fmt.Printf("obj = %v\n", obj)
|
||||
fmt.Printf("obj = %v\n", obj.AddCar(1))
|
||||
fmt.Printf("obj = %v\n", obj.AddCar(2))
|
||||
fmt.Printf("obj = %v\n", obj.AddCar(3))
|
||||
fmt.Printf("obj = %v\n", obj.AddCar(1))
|
||||
}
|
103
leetcode/1603.Design-Parking-System/README.md
Normal file
103
leetcode/1603.Design-Parking-System/README.md
Normal file
@ -0,0 +1,103 @@
|
||||
# [1603. Design Parking System](https://leetcode.com/problems/design-parking-system/)
|
||||
|
||||
|
||||
## 题目
|
||||
|
||||
Design a parking system for a parking lot. The parking lot has three kinds of parking spaces: big, medium, and small, with a fixed number of slots for each size.
|
||||
|
||||
Implement the `ParkingSystem` class:
|
||||
|
||||
- `ParkingSystem(int big, int medium, int small)` Initializes object of the `ParkingSystem` class. The number of slots for each parking space are given as part of the constructor.
|
||||
- `bool addCar(int carType)` Checks whether there is a parking space of `carType` for the car that wants to get into the parking lot. `carType` can be of three kinds: big, medium, or small, which are represented by `1`, `2`, and `3` respectively. **A car can only park in a parking space of its** `carType`. If there is no space available, return `false`, else park the car in that size space and return `true`.
|
||||
|
||||
**Example 1:**
|
||||
|
||||
```
|
||||
Input
|
||||
["ParkingSystem", "addCar", "addCar", "addCar", "addCar"]
|
||||
[[1, 1, 0], [1], [2], [3], [1]]
|
||||
Output
|
||||
[null, true, true, false, false]
|
||||
|
||||
Explanation
|
||||
ParkingSystem parkingSystem = new ParkingSystem(1, 1, 0);
|
||||
parkingSystem.addCar(1); // return true because there is 1 available slot for a big car
|
||||
parkingSystem.addCar(2); // return true because there is 1 available slot for a medium car
|
||||
parkingSystem.addCar(3); // return false because there is no available slot for a small car
|
||||
parkingSystem.addCar(1); // return false because there is no available slot for a big car. It is already occupied.
|
||||
```
|
||||
|
||||
**Constraints:**
|
||||
|
||||
- `0 <= big, medium, small <= 1000`
|
||||
- `carType` is `1`, `2`, or `3`
|
||||
- At most `1000` calls will be made to `addCar`
|
||||
|
||||
## 题目大意
|
||||
|
||||
请你给一个停车场设计一个停车系统。停车场总共有三种不同大小的车位:大,中和小,每种尺寸分别有固定数目的车位。
|
||||
|
||||
请你实现 ParkingSystem 类:
|
||||
|
||||
- ParkingSystem(int big, int medium, int small) 初始化 ParkingSystem 类,三个参数分别对应每种停车位的数目。
|
||||
- bool addCar(int carType) 检查是否有 carType 对应的停车位。 carType 有三种类型:大,中,小,分别用数字 1, 2 和 3 表示。一辆车只能停在 carType 对应尺寸的停车位中。如果没有空车位,请返回 false ,否则将该车停入车位并返回 true 。
|
||||
|
||||
## 解题思路
|
||||
|
||||
- 简单题。分别用 3 个变量表示大,中和小车位。`addCar()` 判断这 3 个变量是否还有空车位即可。
|
||||
|
||||
## 代码
|
||||
|
||||
```go
|
||||
package leetcode
|
||||
|
||||
type ParkingSystem struct {
|
||||
Big int
|
||||
Medium int
|
||||
Small int
|
||||
}
|
||||
|
||||
func Constructor(big int, medium int, small int) ParkingSystem {
|
||||
return ParkingSystem{
|
||||
Big: big,
|
||||
Medium: medium,
|
||||
Small: small,
|
||||
}
|
||||
}
|
||||
|
||||
func (this *ParkingSystem) AddCar(carType int) bool {
|
||||
switch carType {
|
||||
case 1:
|
||||
{
|
||||
if this.Big > 0 {
|
||||
this.Big--
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
case 2:
|
||||
{
|
||||
if this.Medium > 0 {
|
||||
this.Medium--
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
case 3:
|
||||
{
|
||||
if this.Small > 0 {
|
||||
this.Small--
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
/**
|
||||
* Your ParkingSystem object will be instantiated and called as such:
|
||||
* obj := Constructor(big, medium, small);
|
||||
* param_1 := obj.AddCar(carType);
|
||||
*/
|
||||
```
|
@ -0,0 +1,18 @@
|
||||
package leetcode
|
||||
|
||||
import "sort"
|
||||
|
||||
func specialArray(nums []int) int {
|
||||
sort.Ints(nums)
|
||||
x := len(nums)
|
||||
for _, num := range nums {
|
||||
if num >= x {
|
||||
return x
|
||||
}
|
||||
x--
|
||||
if num >= x {
|
||||
return -1
|
||||
}
|
||||
}
|
||||
return -1
|
||||
}
|
@ -0,0 +1,57 @@
|
||||
package leetcode
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
)
|
||||
|
||||
type question1608 struct {
|
||||
para1608
|
||||
ans1608
|
||||
}
|
||||
|
||||
// para 是参数
|
||||
// one 代表第一个参数
|
||||
type para1608 struct {
|
||||
nums []int
|
||||
}
|
||||
|
||||
// ans 是答案
|
||||
// one 代表第一个答案
|
||||
type ans1608 struct {
|
||||
one int
|
||||
}
|
||||
|
||||
func Test_Problem1608(t *testing.T) {
|
||||
|
||||
qs := []question1608{
|
||||
|
||||
{
|
||||
para1608{[]int{3, 5}},
|
||||
ans1608{2},
|
||||
},
|
||||
|
||||
{
|
||||
para1608{[]int{0, 0}},
|
||||
ans1608{-1},
|
||||
},
|
||||
|
||||
{
|
||||
para1608{[]int{0, 4, 3, 0, 4}},
|
||||
ans1608{3},
|
||||
},
|
||||
|
||||
{
|
||||
para1608{[]int{3, 6, 7, 7, 0}},
|
||||
ans1608{-1},
|
||||
},
|
||||
}
|
||||
|
||||
fmt.Printf("------------------------Leetcode Problem 1608------------------------\n")
|
||||
|
||||
for _, q := range qs {
|
||||
_, p := q.ans1608, q.para1608
|
||||
fmt.Printf("【input】:%v 【output】:%v \n", p, specialArray(p.nums))
|
||||
}
|
||||
fmt.Printf("\n\n\n")
|
||||
}
|
@ -0,0 +1,80 @@
|
||||
# [1608. Special Array With X Elements Greater Than or Equal X](https://leetcode.com/problems/special-array-with-x-elements-greater-than-or-equal-x/)
|
||||
|
||||
## 题目
|
||||
|
||||
You are given an array `nums` of non-negative integers. `nums` is considered **special** if there exists a number `x` such that there are **exactly** `x` numbers in `nums` that are **greater than or equal to** `x`.
|
||||
|
||||
Notice that `x` **does not** have to be an element in `nums`.
|
||||
|
||||
Return `x` *if the array is **special**, otherwise, return* `-1`. It can be proven that if `nums` is special, the value for `x` is **unique**.
|
||||
|
||||
**Example 1:**
|
||||
|
||||
```
|
||||
Input: nums = [3,5]
|
||||
Output: 2
|
||||
Explanation: There are 2 values (3 and 5) that are greater than or equal to 2.
|
||||
```
|
||||
|
||||
**Example 2:**
|
||||
|
||||
```
|
||||
Input: nums = [0,0]
|
||||
Output: -1
|
||||
Explanation: No numbers fit the criteria for x.
|
||||
If x = 0, there should be 0 numbers >= x, but there are 2.
|
||||
If x = 1, there should be 1 number >= x, but there are 0.
|
||||
If x = 2, there should be 2 numbers >= x, but there are 0.
|
||||
x cannot be greater since there are only 2 numbers in nums.
|
||||
```
|
||||
|
||||
**Example 3:**
|
||||
|
||||
```
|
||||
Input: nums = [0,4,3,0,4]
|
||||
Output: 3
|
||||
Explanation: There are 3 values that are greater than or equal to 3.
|
||||
```
|
||||
|
||||
**Example 4:**
|
||||
|
||||
```
|
||||
Input: nums = [3,6,7,7,0]
|
||||
Output: -1
|
||||
```
|
||||
|
||||
**Constraints:**
|
||||
|
||||
- `1 <= nums.length <= 100`
|
||||
- `0 <= nums[i] <= 1000`
|
||||
|
||||
## 题目大意
|
||||
|
||||
给你一个非负整数数组 nums 。如果存在一个数 x ,使得 nums 中恰好有 x 个元素 大于或者等于 x ,那么就称 nums 是一个 特殊数组 ,而 x 是该数组的 特征值 。(注意: x 不必 是 nums 的中的元素。)如果数组 nums 是一个 特殊数组 ,请返回它的特征值 x 。否则,返回 -1 。可以证明的是,如果 nums 是特殊数组,那么其特征值 x 是 唯一的 。
|
||||
|
||||
## 解题思路
|
||||
|
||||
- 简单题。抓住题干中给的证明,特征值是唯一的。先将数组从小到大排序,下标的含义与特征值就等价了。下标 `i` 代表大于等于 `nums[i]` 的元素有 `len(nums) - i` 个,那么从第 0 个下标的元素开始遍历,如果这个元素都大于 `len(nums)`,那么后面 `len(nums)` 个元素也都大于等于它,特征值就找到了。如果特征值减一以后,仍然满足 `nums[i] >= x`,说明满足条件的值有多个,这一点不满足特征值唯一性,可以直接返回 -1 了。下标继续右移,特征值继续减一。如果最终循环结束依旧找不到特征值,返回 -1 。
|
||||
|
||||
## 代码
|
||||
|
||||
```go
|
||||
package leetcode
|
||||
|
||||
import "sort"
|
||||
|
||||
func specialArray(nums []int) int {
|
||||
sort.Ints(nums)
|
||||
x := len(nums)
|
||||
for _, num := range nums {
|
||||
if num >= x {
|
||||
return x
|
||||
}
|
||||
x--
|
||||
if num >= x {
|
||||
return -1
|
||||
}
|
||||
}
|
||||
return -1
|
||||
}
|
||||
```
|
@ -0,0 +1,13 @@
|
||||
package leetcode
|
||||
|
||||
func countStudents(students []int, sandwiches []int) int {
|
||||
tmp, n, i := [2]int{}, len(students), 0
|
||||
for _, v := range students {
|
||||
tmp[v]++
|
||||
}
|
||||
for i < n && tmp[sandwiches[i]] > 0 {
|
||||
tmp[sandwiches[i]]--
|
||||
i++
|
||||
}
|
||||
return n - i
|
||||
}
|
@ -0,0 +1,48 @@
|
||||
package leetcode
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
)
|
||||
|
||||
type question1700 struct {
|
||||
para1700
|
||||
ans1700
|
||||
}
|
||||
|
||||
// para 是参数
|
||||
// one 代表第一个参数
|
||||
type para1700 struct {
|
||||
students []int
|
||||
sandwiches []int
|
||||
}
|
||||
|
||||
// ans 是答案
|
||||
// one 代表第一个答案
|
||||
type ans1700 struct {
|
||||
one int
|
||||
}
|
||||
|
||||
func Test_Problem1700(t *testing.T) {
|
||||
|
||||
qs := []question1700{
|
||||
|
||||
{
|
||||
para1700{[]int{1, 1, 0, 0}, []int{0, 1, 0, 1}},
|
||||
ans1700{0},
|
||||
},
|
||||
|
||||
{
|
||||
para1700{[]int{1, 1, 1, 0, 0, 1}, []int{1, 0, 0, 0, 1, 1}},
|
||||
ans1700{3},
|
||||
},
|
||||
}
|
||||
|
||||
fmt.Printf("------------------------Leetcode Problem 1700------------------------\n")
|
||||
|
||||
for _, q := range qs {
|
||||
_, p := q.ans1700, q.para1700
|
||||
fmt.Printf("【input】:%v 【output】:%v\n", p, countStudents(p.students, p.sandwiches))
|
||||
}
|
||||
fmt.Printf("\n\n\n")
|
||||
}
|
@ -0,0 +1,79 @@
|
||||
# [1700. Number of Students Unable to Eat Lunch](https://leetcode.com/problems/number-of-students-unable-to-eat-lunch/)
|
||||
|
||||
|
||||
## 题目
|
||||
|
||||
The school cafeteria offers circular and square sandwiches at lunch break, referred to by numbers `0` and `1` respectively. All students stand in a queue. Each student either prefers square or circular sandwiches.
|
||||
|
||||
The number of sandwiches in the cafeteria is equal to the number of students. The sandwiches are placed in a **stack**. At each step:
|
||||
|
||||
- If the student at the front of the queue **prefers** the sandwich on the top of the stack, they will **take it** and leave the queue.
|
||||
- Otherwise, they will **leave it** and go to the queue's end.
|
||||
|
||||
This continues until none of the queue students want to take the top sandwich and are thus unable to eat.
|
||||
|
||||
You are given two integer arrays `students` and `sandwiches` where `sandwiches[i]` is the type of the `ith` sandwich in the stack (`i = 0` is the top of the stack) and `students[j]` is the preference of the `jth` student in the initial queue (`j = 0` is the front of the queue). Return *the number of students that are unable to eat.*
|
||||
|
||||
**Example 1:**
|
||||
|
||||
```
|
||||
Input: students = [1,1,0,0], sandwiches = [0,1,0,1]
|
||||
Output: 0
|
||||
Explanation:
|
||||
- Front student leaves the top sandwich and returns to the end of the line making students = [1,0,0,1].
|
||||
- Front student leaves the top sandwich and returns to the end of the line making students = [0,0,1,1].
|
||||
- Front student takes the top sandwich and leaves the line making students = [0,1,1] and sandwiches = [1,0,1].
|
||||
- Front student leaves the top sandwich and returns to the end of the line making students = [1,1,0].
|
||||
- Front student takes the top sandwich and leaves the line making students = [1,0] and sandwiches = [0,1].
|
||||
- Front student leaves the top sandwich and returns to the end of the line making students = [0,1].
|
||||
- Front student takes the top sandwich and leaves the line making students = [1] and sandwiches = [1].
|
||||
- Front student takes the top sandwich and leaves the line making students = [] and sandwiches = [].
|
||||
Hence all students are able to eat.
|
||||
```
|
||||
|
||||
**Example 2:**
|
||||
|
||||
```
|
||||
Input: students = [1,1,1,0,0,1], sandwiches = [1,0,0,0,1,1]
|
||||
Output: 3
|
||||
```
|
||||
|
||||
**Constraints:**
|
||||
|
||||
- `1 <= students.length, sandwiches.length <= 100`
|
||||
- `students.length == sandwiches.length`
|
||||
- `sandwiches[i]` is `0` or `1`.
|
||||
- `students[i]` is `0` or `1`.
|
||||
|
||||
## 题目大意
|
||||
|
||||
学校的自助午餐提供圆形和方形的三明治,分别用数字 0 和 1 表示。所有学生站在一个队列里,每个学生要么喜欢圆形的要么喜欢方形的。
|
||||
餐厅里三明治的数量与学生的数量相同。所有三明治都放在一个 栈 里,每一轮:
|
||||
|
||||
- 如果队列最前面的学生 喜欢 栈顶的三明治,那么会 拿走它 并离开队列。
|
||||
- 否则,这名学生会 放弃这个三明治 并回到队列的尾部。
|
||||
这个过程会一直持续到队列里所有学生都不喜欢栈顶的三明治为止。
|
||||
|
||||
给你两个整数数组 students 和 sandwiches ,其中 sandwiches[i] 是栈里面第 i 个三明治的类型(i = 0 是栈的顶部), students[j] 是初始队列里第 j 名学生对三明治的喜好(j = 0 是队列的最开始位置)。请你返回无法吃午餐的学生数量。
|
||||
|
||||
## 解题思路
|
||||
|
||||
- 简单题。按照题意,学生不管怎么轮流领三明治,如果数量够,经过多轮循环,总能领到。问题可以等价为,学生依次到队列前面领取三明治。2 个种类的三明治都摆好放在那里了。最终领不到三明治的学生都是因为喜欢的三明治不够发放了。按照这个思路,先统计 2 种三明治的总个数,然后减去学生对三明治的需求总数,剩下的学生即都是无法满足的。
|
||||
|
||||
## 代码
|
||||
|
||||
```go
|
||||
package leetcode
|
||||
|
||||
func countStudents(students []int, sandwiches []int) int {
|
||||
tmp, n, i := [2]int{}, len(students), 0
|
||||
for _, v := range students {
|
||||
tmp[v]++
|
||||
}
|
||||
for i < n && tmp[sandwiches[i]] > 0 {
|
||||
tmp[sandwiches[i]]--
|
||||
i++
|
||||
}
|
||||
return n - i
|
||||
}
|
||||
```
|
@ -0,0 +1,20 @@
|
||||
package leetcode
|
||||
|
||||
import "sort"
|
||||
|
||||
func maximumUnits(boxTypes [][]int, truckSize int) int {
|
||||
sort.Slice(boxTypes, func(i, j int) bool {
|
||||
return boxTypes[i][1] > boxTypes[j][1]
|
||||
})
|
||||
res := 0
|
||||
for i := 0; truckSize > 0 && i < len(boxTypes); i++ {
|
||||
if truckSize >= boxTypes[i][0] {
|
||||
truckSize -= boxTypes[i][0]
|
||||
res += (boxTypes[i][1] * boxTypes[i][0])
|
||||
} else {
|
||||
res += (truckSize * boxTypes[i][1])
|
||||
truckSize = 0
|
||||
}
|
||||
}
|
||||
return res
|
||||
}
|
@ -0,0 +1,48 @@
|
||||
package leetcode
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
)
|
||||
|
||||
type question1710 struct {
|
||||
para1710
|
||||
ans1710
|
||||
}
|
||||
|
||||
// para 是参数
|
||||
// one 代表第一个参数
|
||||
type para1710 struct {
|
||||
boxTypes [][]int
|
||||
truckSize int
|
||||
}
|
||||
|
||||
// ans 是答案
|
||||
// one 代表第一个答案
|
||||
type ans1710 struct {
|
||||
one int
|
||||
}
|
||||
|
||||
func Test_Problem1710(t *testing.T) {
|
||||
|
||||
qs := []question1710{
|
||||
|
||||
{
|
||||
para1710{[][]int{{1, 3}, {2, 2}, {3, 1}}, 4},
|
||||
ans1710{8},
|
||||
},
|
||||
|
||||
{
|
||||
para1710{[][]int{{5, 10}, {2, 5}, {4, 7}, {3, 9}}, 10},
|
||||
ans1710{91},
|
||||
},
|
||||
}
|
||||
|
||||
fmt.Printf("------------------------Leetcode Problem 1710------------------------\n")
|
||||
|
||||
for _, q := range qs {
|
||||
_, p := q.ans1710, q.para1710
|
||||
fmt.Printf("【input】:%v 【output】:%v\n", p, maximumUnits(p.boxTypes, p.truckSize))
|
||||
}
|
||||
fmt.Printf("\n\n\n")
|
||||
}
|
77
leetcode/1710.Maximum-Units-on-a-Truck/README.md
Normal file
77
leetcode/1710.Maximum-Units-on-a-Truck/README.md
Normal file
@ -0,0 +1,77 @@
|
||||
# [1710. Maximum Units on a Truck](https://leetcode.com/problems/maximum-units-on-a-truck/)
|
||||
|
||||
|
||||
## 题目
|
||||
|
||||
You are assigned to put some amount of boxes onto **one truck**. You are given a 2D array `boxTypes`, where `boxTypes[i] = [numberOfBoxesi, numberOfUnitsPerBoxi]`:
|
||||
|
||||
- `numberOfBoxesi` is the number of boxes of type `i`.
|
||||
- `numberOfUnitsPerBoxi`is the number of units in each box of the type `i`.
|
||||
|
||||
You are also given an integer `truckSize`, which is the **maximum** number of **boxes** that can be put on the truck. You can choose any boxes to put on the truck as long as the number of boxes does not exceed `truckSize`.
|
||||
|
||||
Return *the **maximum** total number of **units** that can be put on the truck.*
|
||||
|
||||
**Example 1:**
|
||||
|
||||
```
|
||||
Input: boxTypes = [[1,3],[2,2],[3,1]], truckSize = 4
|
||||
Output: 8
|
||||
Explanation: There are:
|
||||
- 1 box of the first type that contains 3 units.
|
||||
- 2 boxes of the second type that contain 2 units each.
|
||||
- 3 boxes of the third type that contain 1 unit each.
|
||||
You can take all the boxes of the first and second types, and one box of the third type.
|
||||
The total number of units will be = (1 * 3) + (2 * 2) + (1 * 1) = 8.
|
||||
```
|
||||
|
||||
**Example 2:**
|
||||
|
||||
```
|
||||
Input: boxTypes = [[5,10],[2,5],[4,7],[3,9]], truckSize = 10
|
||||
Output: 91
|
||||
```
|
||||
|
||||
**Constraints:**
|
||||
|
||||
- `1 <= boxTypes.length <= 1000`
|
||||
- `1 <= numberOfBoxesi, numberOfUnitsPerBoxi <= 1000`
|
||||
- `1 <= truckSize <= 106`
|
||||
|
||||
## 题目大意
|
||||
|
||||
请你将一些箱子装在 一辆卡车 上。给你一个二维数组 boxTypes ,其中 boxTypes[i] = [numberOfBoxesi, numberOfUnitsPerBoxi] :
|
||||
|
||||
- numberOfBoxesi 是类型 i 的箱子的数量。-
|
||||
- numberOfUnitsPerBoxi 是类型 i 每个箱子可以装载的单元数量。
|
||||
|
||||
整数 truckSize 表示卡车上可以装载 箱子 的 最大数量 。只要箱子数量不超过 truckSize ,你就可以选择任意箱子装到卡车上。返回卡车可以装载 单元 的 最大 总数。
|
||||
|
||||
## 解题思路
|
||||
|
||||
- 简单题。先将箱子按照单元数量从大到小排序。要想卡车装载单元数最大,那么需要尽量装单元数多的箱子。所以排序以后从单元数量多的箱子开始取。一直取至 truckSize 没有空间。累积的单元数即最大总数。
|
||||
|
||||
## 代码
|
||||
|
||||
```go
|
||||
package leetcode
|
||||
|
||||
import "sort"
|
||||
|
||||
func maximumUnits(boxTypes [][]int, truckSize int) int {
|
||||
sort.Slice(boxTypes, func(i, j int) bool {
|
||||
return boxTypes[i][1] > boxTypes[j][1]
|
||||
})
|
||||
res := 0
|
||||
for i := 0; truckSize > 0 && i < len(boxTypes); i++ {
|
||||
if truckSize >= boxTypes[i][0] {
|
||||
truckSize -= boxTypes[i][0]
|
||||
res += (boxTypes[i][1] * boxTypes[i][0])
|
||||
} else {
|
||||
res += (truckSize * boxTypes[i][1])
|
||||
truckSize = 0
|
||||
}
|
||||
}
|
||||
return res
|
||||
}
|
||||
```
|
@ -0,0 +1,13 @@
|
||||
package leetcode
|
||||
|
||||
func totalMoney(n int) int {
|
||||
res := 0
|
||||
for tmp, count := 1, 7; n > 0; tmp, count = tmp+1, 7 {
|
||||
for m := tmp; n > 0 && count > 0; m++ {
|
||||
res += m
|
||||
n--
|
||||
count--
|
||||
}
|
||||
}
|
||||
return res
|
||||
}
|
@ -0,0 +1,52 @@
|
||||
package leetcode
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
)
|
||||
|
||||
type question1716 struct {
|
||||
para1716
|
||||
ans1716
|
||||
}
|
||||
|
||||
// para 是参数
|
||||
// one 代表第一个参数
|
||||
type para1716 struct {
|
||||
n int
|
||||
}
|
||||
|
||||
// ans 是答案
|
||||
// one 代表第一个答案
|
||||
type ans1716 struct {
|
||||
one int
|
||||
}
|
||||
|
||||
func Test_Problem1716(t *testing.T) {
|
||||
|
||||
qs := []question1716{
|
||||
|
||||
{
|
||||
para1716{4},
|
||||
ans1716{10},
|
||||
},
|
||||
|
||||
{
|
||||
para1716{10},
|
||||
ans1716{37},
|
||||
},
|
||||
|
||||
{
|
||||
para1716{20},
|
||||
ans1716{96},
|
||||
},
|
||||
}
|
||||
|
||||
fmt.Printf("------------------------Leetcode Problem 1716------------------------\n")
|
||||
|
||||
for _, q := range qs {
|
||||
_, p := q.ans1716, q.para1716
|
||||
fmt.Printf("【input】:%v 【output】:%v\n", p, totalMoney(p.n))
|
||||
}
|
||||
fmt.Printf("\n\n\n")
|
||||
}
|
64
leetcode/1716.Calculate-Money-in-Leetcode-Bank/README.md
Normal file
64
leetcode/1716.Calculate-Money-in-Leetcode-Bank/README.md
Normal file
@ -0,0 +1,64 @@
|
||||
# [1716. Calculate Money in Leetcode Bank](https://leetcode.com/problems/calculate-money-in-leetcode-bank/)
|
||||
|
||||
|
||||
## 题目
|
||||
|
||||
Hercy wants to save money for his first car. He puts money in the Leetcode bank **every day**.
|
||||
|
||||
He starts by putting in `$1` on Monday, the first day. Every day from Tuesday to Sunday, he will put in `$1` more than the day before. On every subsequent Monday, he will put in `$1` more than the **previous Monday**.
|
||||
|
||||
Given `n`, return *the total amount of money he will have in the Leetcode bank at the end of the* `nth` *day.*
|
||||
|
||||
**Example 1:**
|
||||
|
||||
```
|
||||
Input: n = 4
|
||||
Output: 10
|
||||
Explanation: After the 4th day, the total is 1 + 2 + 3 + 4 = 10.
|
||||
```
|
||||
|
||||
**Example 2:**
|
||||
|
||||
```
|
||||
Input: n = 10
|
||||
Output: 37
|
||||
Explanation: After the 10th day, the total is (1 + 2 + 3 + 4 + 5 + 6 + 7) + (2 + 3 + 4) = 37. Notice that on the 2nd Monday, Hercy only puts in $2.
|
||||
```
|
||||
|
||||
**Example 3:**
|
||||
|
||||
```
|
||||
Input: n = 20
|
||||
Output: 96
|
||||
Explanation: After the 20th day, the total is (1 + 2 + 3 + 4 + 5 + 6 + 7) + (2 + 3 + 4 + 5 + 6 + 7 + 8) + (3 + 4 + 5 + 6 + 7 + 8) = 96.
|
||||
```
|
||||
|
||||
**Constraints:**
|
||||
|
||||
- `1 <= n <= 1000`
|
||||
|
||||
## 题目大意
|
||||
|
||||
Hercy 想要为购买第一辆车存钱。他 每天 都往力扣银行里存钱。最开始,他在周一的时候存入 1 块钱。从周二到周日,他每天都比前一天多存入 1 块钱。在接下来每一个周一,他都会比 前一个周一 多存入 1 块钱。给你 n ,请你返回在第 n 天结束的时候他在力扣银行总共存了多少块钱。
|
||||
|
||||
## 解题思路
|
||||
|
||||
- 简单题。按照题意写 2 层循环即可。
|
||||
|
||||
## 代码
|
||||
|
||||
```go
|
||||
package leetcode
|
||||
|
||||
func totalMoney(n int) int {
|
||||
res := 0
|
||||
for tmp, count := 1, 7; n > 0; tmp, count = tmp+1, 7 {
|
||||
for m := tmp; n > 0 && count > 0; m++ {
|
||||
res += m
|
||||
n--
|
||||
count--
|
||||
}
|
||||
}
|
||||
return res
|
||||
}
|
||||
```
|
10
leetcode/1720.Decode-XORed-Array/1720. Decode XORed Array.go
Normal file
10
leetcode/1720.Decode-XORed-Array/1720. Decode XORed Array.go
Normal file
@ -0,0 +1,10 @@
|
||||
package leetcode
|
||||
|
||||
func decode(encoded []int, first int) []int {
|
||||
arr := make([]int, len(encoded)+1)
|
||||
arr[0] = first
|
||||
for i, val := range encoded {
|
||||
arr[i+1] = arr[i] ^ val
|
||||
}
|
||||
return arr
|
||||
}
|
@ -0,0 +1,48 @@
|
||||
package leetcode
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
)
|
||||
|
||||
type question1720 struct {
|
||||
para1720
|
||||
ans1720
|
||||
}
|
||||
|
||||
// para 是参数
|
||||
// one 代表第一个参数
|
||||
type para1720 struct {
|
||||
encoded []int
|
||||
first int
|
||||
}
|
||||
|
||||
// ans 是答案
|
||||
// one 代表第一个答案
|
||||
type ans1720 struct {
|
||||
one []int
|
||||
}
|
||||
|
||||
func Test_Problem1720(t *testing.T) {
|
||||
|
||||
qs := []question1720{
|
||||
|
||||
{
|
||||
para1720{[]int{1, 2, 3}, 1},
|
||||
ans1720{[]int{1, 0, 2, 1}},
|
||||
},
|
||||
|
||||
{
|
||||
para1720{[]int{6, 2, 7, 3}, 4},
|
||||
ans1720{[]int{4, 2, 0, 7, 4}},
|
||||
},
|
||||
}
|
||||
|
||||
fmt.Printf("------------------------Leetcode Problem 1720------------------------\n")
|
||||
|
||||
for _, q := range qs {
|
||||
_, p := q.ans1720, q.para1720
|
||||
fmt.Printf("【input】:%v 【output】:%v\n", p, decode(p.encoded, p.first))
|
||||
}
|
||||
fmt.Printf("\n\n\n")
|
||||
}
|
59
leetcode/1720.Decode-XORed-Array/README.md
Normal file
59
leetcode/1720.Decode-XORed-Array/README.md
Normal file
@ -0,0 +1,59 @@
|
||||
# [1720. Decode XORed Array](https://leetcode.com/problems/decode-xored-array/)
|
||||
|
||||
|
||||
## 题目
|
||||
|
||||
There is a **hidden** integer array `arr` that consists of `n` non-negative integers.
|
||||
|
||||
It was encoded into another integer array `encoded` of length `n - 1`, such that `encoded[i] = arr[i] XOR arr[i + 1]`. For example, if `arr = [1,0,2,1]`, then `encoded = [1,2,3]`.
|
||||
|
||||
You are given the `encoded` array. You are also given an integer `first`, that is the first element of `arr`, i.e. `arr[0]`.
|
||||
|
||||
Return *the original array* `arr`. It can be proved that the answer exists and is unique.
|
||||
|
||||
**Example 1:**
|
||||
|
||||
```
|
||||
Input: encoded = [1,2,3], first = 1
|
||||
Output: [1,0,2,1]
|
||||
Explanation: If arr = [1,0,2,1], then first = 1 and encoded = [1 XOR 0, 0 XOR 2, 2 XOR 1] = [1,2,3]
|
||||
|
||||
```
|
||||
|
||||
**Example 2:**
|
||||
|
||||
```
|
||||
Input: encoded = [6,2,7,3], first = 4
|
||||
Output: [4,2,0,7,4]
|
||||
|
||||
```
|
||||
|
||||
**Constraints:**
|
||||
|
||||
- `2 <= n <= 104`
|
||||
- `encoded.length == n - 1`
|
||||
- `0 <= encoded[i] <= 105`
|
||||
- `0 <= first <= 10^5`
|
||||
|
||||
## 题目大意
|
||||
|
||||
未知 整数数组 arr 由 n 个非负整数组成。经编码后变为长度为 n - 1 的另一个整数数组 encoded ,其中 encoded[i] = arr[i] XOR arr[i + 1] 。例如,arr = [1,0,2,1] 经编码后得到 encoded = [1,2,3] 。给你编码后的数组 encoded 和原数组 arr 的第一个元素 first(arr[0])。请解码返回原数组 arr 。可以证明答案存在并且是唯一的。
|
||||
|
||||
## 解题思路
|
||||
|
||||
- 简单题。按照题意,求返回解码以后的原数组,即将编码后的数组前后两两元素依次做异或 `XOR` 运算。
|
||||
|
||||
## 代码
|
||||
|
||||
```go
|
||||
package leetcode
|
||||
|
||||
func decode(encoded []int, first int) []int {
|
||||
arr := make([]int, len(encoded)+1)
|
||||
arr[0] = first
|
||||
for i, val := range encoded {
|
||||
arr[i+1] = arr[i] ^ val
|
||||
}
|
||||
return arr
|
||||
}
|
||||
```
|
@ -0,0 +1,20 @@
|
||||
package leetcode
|
||||
|
||||
func countGoodRectangles(rectangles [][]int) int {
|
||||
minLength, count := 0, 0
|
||||
for i, _ := range rectangles {
|
||||
minSide := 0
|
||||
if rectangles[i][0] <= rectangles[i][1] {
|
||||
minSide = rectangles[i][0]
|
||||
} else {
|
||||
minSide = rectangles[i][1]
|
||||
}
|
||||
if minSide > minLength {
|
||||
minLength = minSide
|
||||
count = 1
|
||||
} else if minSide == minLength {
|
||||
count++
|
||||
}
|
||||
}
|
||||
return count
|
||||
}
|
@ -0,0 +1,47 @@
|
||||
package leetcode
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
)
|
||||
|
||||
type question1725 struct {
|
||||
para1725
|
||||
ans1725
|
||||
}
|
||||
|
||||
// para 是参数
|
||||
// one 代表第一个参数
|
||||
type para1725 struct {
|
||||
rectangles [][]int
|
||||
}
|
||||
|
||||
// ans 是答案
|
||||
// one 代表第一个答案
|
||||
type ans1725 struct {
|
||||
one int
|
||||
}
|
||||
|
||||
func Test_Problem1725(t *testing.T) {
|
||||
|
||||
qs := []question1725{
|
||||
|
||||
{
|
||||
para1725{[][]int{{5, 8}, {3, 9}, {5, 12}, {16, 5}}},
|
||||
ans1725{3},
|
||||
},
|
||||
|
||||
{
|
||||
para1725{[][]int{{2, 3}, {3, 7}, {4, 3}, {3, 7}}},
|
||||
ans1725{3},
|
||||
},
|
||||
}
|
||||
|
||||
fmt.Printf("------------------------Leetcode Problem 1725------------------------\n")
|
||||
|
||||
for _, q := range qs {
|
||||
_, p := q.ans1725, q.para1725
|
||||
fmt.Printf("【input】:%v 【output】:%v\n", p, countGoodRectangles(p.rectangles))
|
||||
}
|
||||
fmt.Printf("\n\n\n")
|
||||
}
|
@ -0,0 +1,68 @@
|
||||
# [1725. Number Of Rectangles That Can Form The Largest Square](https://leetcode.com/problems/number-of-rectangles-that-can-form-the-largest-square/)
|
||||
|
||||
|
||||
## 题目
|
||||
|
||||
You are given an array `rectangles` where `rectangles[i] = [li, wi]` represents the `ith` rectangle of length `li` and width `wi`.
|
||||
|
||||
You can cut the `ith` rectangle to form a square with a side length of `k` if both `k <= li` and `k <= wi`. For example, if you have a rectangle `[4,6]`, you can cut it to get a square with a side length of at most `4`.
|
||||
|
||||
Let `maxLen` be the side length of the **largest** square you can obtain from any of the given rectangles.
|
||||
|
||||
Return *the **number** of rectangles that can make a square with a side length of* `maxLen`.
|
||||
|
||||
**Example 1:**
|
||||
|
||||
```
|
||||
Input: rectangles = [[5,8],[3,9],[5,12],[16,5]]
|
||||
Output: 3
|
||||
Explanation: The largest squares you can get from each rectangle are of lengths [5,3,5,5].
|
||||
The largest possible square is of length 5, and you can get it out of 3 rectangles.
|
||||
```
|
||||
|
||||
**Example 2:**
|
||||
|
||||
```
|
||||
Input: rectangles = [[2,3],[3,7],[4,3],[3,7]]
|
||||
Output: 3
|
||||
```
|
||||
|
||||
**Constraints:**
|
||||
|
||||
- `1 <= rectangles.length <= 1000`
|
||||
- `rectangles[i].length == 2`
|
||||
- `1 <= li, wi <= 10^9`
|
||||
- `li != wi`
|
||||
|
||||
## 题目大意
|
||||
|
||||
给你一个数组 rectangles ,其中 rectangles[i] = [li, wi] 表示第 i 个矩形的长度为 li 、宽度为 wi 。如果存在 k 同时满足 k <= li 和 k <= wi ,就可以将第 i 个矩形切成边长为 k 的正方形。例如,矩形 [4,6] 可以切成边长最大为 4 的正方形。设 maxLen 为可以从矩形数组 rectangles 切分得到的 最大正方形 的边长。返回可以切出边长为 maxLen 的正方形的矩形 数目 。
|
||||
|
||||
## 解题思路
|
||||
|
||||
- 简单题。扫描数组中的每一个矩形,先找到边长较小的那条边,作为正方形的边长。扫描过程中动态更新最大的正方形边长,并累加计数。循环一遍结束,输出最终计数值即可。
|
||||
|
||||
## 代码
|
||||
|
||||
```go
|
||||
package leetcode
|
||||
|
||||
func countGoodRectangles(rectangles [][]int) int {
|
||||
minLength, count := 0, 0
|
||||
for i, _ := range rectangles {
|
||||
minSide := 0
|
||||
if rectangles[i][0] <= rectangles[i][1] {
|
||||
minSide = rectangles[i][0]
|
||||
} else {
|
||||
minSide = rectangles[i][1]
|
||||
}
|
||||
if minSide > minLength {
|
||||
minLength = minSide
|
||||
count = 1
|
||||
} else if minSide == minLength {
|
||||
count++
|
||||
}
|
||||
}
|
||||
return count
|
||||
}
|
||||
```
|
@ -0,0 +1,12 @@
|
||||
package leetcode
|
||||
|
||||
func largestAltitude(gain []int) int {
|
||||
max, height := 0, 0
|
||||
for _, g := range gain {
|
||||
height += g
|
||||
if height > max {
|
||||
max = height
|
||||
}
|
||||
}
|
||||
return max
|
||||
}
|
@ -0,0 +1,47 @@
|
||||
package leetcode
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
)
|
||||
|
||||
type question1732 struct {
|
||||
para1732
|
||||
ans1732
|
||||
}
|
||||
|
||||
// para 是参数
|
||||
// one 代表第一个参数
|
||||
type para1732 struct {
|
||||
gain []int
|
||||
}
|
||||
|
||||
// ans 是答案
|
||||
// one 代表第一个答案
|
||||
type ans1732 struct {
|
||||
one int
|
||||
}
|
||||
|
||||
func Test_Problem1732(t *testing.T) {
|
||||
|
||||
qs := []question1732{
|
||||
|
||||
{
|
||||
para1732{[]int{-5, 1, 5, 0, -7}},
|
||||
ans1732{1},
|
||||
},
|
||||
|
||||
{
|
||||
para1732{[]int{-4, -3, -2, -1, 4, 3, 2}},
|
||||
ans1732{0},
|
||||
},
|
||||
}
|
||||
|
||||
fmt.Printf("------------------------Leetcode Problem 1732------------------------\n")
|
||||
|
||||
for _, q := range qs {
|
||||
_, p := q.ans1732, q.para1732
|
||||
fmt.Printf("【input】:%v 【output】:%v\n", p, largestAltitude(p.gain))
|
||||
}
|
||||
fmt.Printf("\n\n\n")
|
||||
}
|
55
leetcode/1732.Find-the-Highest-Altitude/README.md
Normal file
55
leetcode/1732.Find-the-Highest-Altitude/README.md
Normal file
@ -0,0 +1,55 @@
|
||||
# [1732. Find the Highest Altitude](https://leetcode.com/problems/find-the-highest-altitude/)
|
||||
|
||||
|
||||
## 题目
|
||||
|
||||
There is a biker going on a road trip. The road trip consists of `n + 1` points at different altitudes. The biker starts his trip on point `0` with altitude equal `0`.
|
||||
|
||||
You are given an integer array `gain` of length `n` where `gain[i]` is the **net gain in altitude** between points `i` and `i + 1` for all (`0 <= i < n)`. Return *the **highest altitude** of a point.*
|
||||
|
||||
**Example 1:**
|
||||
|
||||
```
|
||||
Input: gain = [-5,1,5,0,-7]
|
||||
Output: 1
|
||||
Explanation: The altitudes are [0,-5,-4,1,1,-6]. The highest is 1.
|
||||
```
|
||||
|
||||
**Example 2:**
|
||||
|
||||
```
|
||||
Input: gain = [-4,-3,-2,-1,4,3,2]
|
||||
Output: 0
|
||||
Explanation: The altitudes are [0,-4,-7,-9,-10,-6,-3,-1]. The highest is 0.
|
||||
```
|
||||
|
||||
**Constraints:**
|
||||
|
||||
- `n == gain.length`
|
||||
- `1 <= n <= 100`
|
||||
- `100 <= gain[i] <= 100`
|
||||
|
||||
## 题目大意
|
||||
|
||||
有一个自行车手打算进行一场公路骑行,这条路线总共由 n + 1 个不同海拔的点组成。自行车手从海拔为 0 的点 0 开始骑行。给你一个长度为 n 的整数数组 gain ,其中 gain[i] 是点 i 和点 i + 1 的 净海拔高度差(0 <= i < n)。请你返回 最高点的海拔 。
|
||||
|
||||
## 解题思路
|
||||
|
||||
- 简单题。循环数组依次从第一个海拔点开始还原每个海拔点,动态记录最大高度。循环结束输出最大高度即可。
|
||||
|
||||
## 代码
|
||||
|
||||
```go
|
||||
package leetcode
|
||||
|
||||
func largestAltitude(gain []int) int {
|
||||
max, height := 0, 0
|
||||
for _, g := range gain {
|
||||
height += g
|
||||
if height > max {
|
||||
max = height
|
||||
}
|
||||
}
|
||||
return max
|
||||
}
|
||||
```
|
@ -0,0 +1,25 @@
|
||||
package leetcode
|
||||
|
||||
func maximumTime(time string) string {
|
||||
timeb := []byte(time)
|
||||
if timeb[3] == '?' {
|
||||
timeb[3] = '5'
|
||||
}
|
||||
if timeb[4] == '?' {
|
||||
timeb[4] = '9'
|
||||
}
|
||||
if timeb[0] == '?' {
|
||||
if int(timeb[1]-'0') > 3 && int(timeb[1]-'0') < 10 {
|
||||
timeb[0] = '1'
|
||||
} else {
|
||||
timeb[0] = '2'
|
||||
}
|
||||
}
|
||||
if timeb[1] == '?' {
|
||||
timeb[1] = '9'
|
||||
}
|
||||
if timeb[0] == '2' && timeb[1] == '9' {
|
||||
timeb[1] = '3'
|
||||
}
|
||||
return string(timeb)
|
||||
}
|
@ -0,0 +1,52 @@
|
||||
package leetcode
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
)
|
||||
|
||||
type question1736 struct {
|
||||
para1736
|
||||
ans1736
|
||||
}
|
||||
|
||||
// para 是参数
|
||||
// one 代表第一个参数
|
||||
type para1736 struct {
|
||||
time string
|
||||
}
|
||||
|
||||
// ans 是答案
|
||||
// one 代表第一个答案
|
||||
type ans1736 struct {
|
||||
one string
|
||||
}
|
||||
|
||||
func Test_Problem1736(t *testing.T) {
|
||||
|
||||
qs := []question1736{
|
||||
|
||||
{
|
||||
para1736{"2?:?0"},
|
||||
ans1736{"23:50"},
|
||||
},
|
||||
|
||||
{
|
||||
para1736{"0?:3?"},
|
||||
ans1736{"09:39"},
|
||||
},
|
||||
|
||||
{
|
||||
para1736{"1?:22"},
|
||||
ans1736{"19:22"},
|
||||
},
|
||||
}
|
||||
|
||||
fmt.Printf("------------------------Leetcode Problem 1736------------------------\n")
|
||||
|
||||
for _, q := range qs {
|
||||
_, p := q.ans1736, q.para1736
|
||||
fmt.Printf("【input】:%v 【output】:%v\n", p, maximumTime(p.time))
|
||||
}
|
||||
fmt.Printf("\n\n\n")
|
||||
}
|
@ -0,0 +1,75 @@
|
||||
# [1736. Latest Time by Replacing Hidden Digits](https://leetcode.com/problems/latest-time-by-replacing-hidden-digits/)
|
||||
|
||||
|
||||
## 题目
|
||||
|
||||
You are given a string `time` in the form of `hh:mm`, where some of the digits in the string are hidden (represented by `?`).
|
||||
|
||||
The valid times are those inclusively between `00:00` and `23:59`.
|
||||
|
||||
Return *the latest valid time you can get from* `time` *by replacing the hidden* *digits*.
|
||||
|
||||
**Example 1:**
|
||||
|
||||
```
|
||||
Input: time = "2?:?0"
|
||||
Output: "23:50"
|
||||
Explanation: The latest hour beginning with the digit '2' is 23 and the latest minute ending with the digit '0' is 50.
|
||||
```
|
||||
|
||||
**Example 2:**
|
||||
|
||||
```
|
||||
Input: time = "0?:3?"
|
||||
Output: "09:39"
|
||||
```
|
||||
|
||||
**Example 3:**
|
||||
|
||||
```
|
||||
Input: time = "1?:22"
|
||||
Output: "19:22"
|
||||
```
|
||||
|
||||
**Constraints:**
|
||||
|
||||
- `time` is in the format `hh:mm`.
|
||||
- It is guaranteed that you can produce a valid time from the given string.
|
||||
|
||||
## 题目大意
|
||||
|
||||
给你一个字符串 time ,格式为 hh:mm(小时:分钟),其中某几位数字被隐藏(用 ? 表示)。有效的时间为 00:00 到 23:59 之间的所有时间,包括 00:00 和 23:59 。替换 time 中隐藏的数字,返回你可以得到的最晚有效时间。
|
||||
|
||||
## 解题思路
|
||||
|
||||
- 简单题。根据题意,需要找到最晚的有效时间。枚举时间 4 个位置即可。如果第 3 个位置是 ?,那么它最晚时间是 5;如果第 4 个位置是 ?,那么它最晚时间是 9;如果第 2 个位置是 ?,那么它最晚时间是 9;如果第 1 个位置是 ?,根据第 2 个位置判断,如果第 2 个位置是大于 3 的数,那么第一个位置最晚时间是 1,如果第 2 个位置是小于 3 的数那么第一个位置最晚时间是 2 。按照上述规则即可还原最晚时间。
|
||||
|
||||
## 代码
|
||||
|
||||
```go
|
||||
package leetcode
|
||||
|
||||
func maximumTime(time string) string {
|
||||
timeb := []byte(time)
|
||||
if timeb[3] == '?' {
|
||||
timeb[3] = '5'
|
||||
}
|
||||
if timeb[4] == '?' {
|
||||
timeb[4] = '9'
|
||||
}
|
||||
if timeb[0] == '?' {
|
||||
if int(timeb[1]-'0') > 3 && int(timeb[1]-'0') < 10 {
|
||||
timeb[0] = '1'
|
||||
} else {
|
||||
timeb[0] = '2'
|
||||
}
|
||||
}
|
||||
if timeb[1] == '?' {
|
||||
timeb[1] = '9'
|
||||
}
|
||||
if timeb[0] == '2' && timeb[1] == '9' {
|
||||
timeb[1] = '3'
|
||||
}
|
||||
return string(timeb)
|
||||
}
|
||||
```
|
@ -0,0 +1,17 @@
|
||||
package leetcode
|
||||
|
||||
func countBalls(lowLimit int, highLimit int) int {
|
||||
buckets, maxBall := [46]int{}, 0
|
||||
for i := lowLimit; i <= highLimit; i++ {
|
||||
t := 0
|
||||
for j := i; j > 0; {
|
||||
t += j % 10
|
||||
j = j / 10
|
||||
}
|
||||
buckets[t]++
|
||||
if buckets[t] > maxBall {
|
||||
maxBall = buckets[t]
|
||||
}
|
||||
}
|
||||
return maxBall
|
||||
}
|
@ -0,0 +1,53 @@
|
||||
package leetcode
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
)
|
||||
|
||||
type question1742 struct {
|
||||
para1742
|
||||
ans1742
|
||||
}
|
||||
|
||||
// para 是参数
|
||||
// one 代表第一个参数
|
||||
type para1742 struct {
|
||||
lowLimit int
|
||||
highLimit int
|
||||
}
|
||||
|
||||
// ans 是答案
|
||||
// one 代表第一个答案
|
||||
type ans1742 struct {
|
||||
one int
|
||||
}
|
||||
|
||||
func Test_Problem1742(t *testing.T) {
|
||||
|
||||
qs := []question1742{
|
||||
|
||||
{
|
||||
para1742{1, 10},
|
||||
ans1742{2},
|
||||
},
|
||||
|
||||
{
|
||||
para1742{5, 15},
|
||||
ans1742{2},
|
||||
},
|
||||
|
||||
{
|
||||
para1742{19, 28},
|
||||
ans1742{2},
|
||||
},
|
||||
}
|
||||
|
||||
fmt.Printf("------------------------Leetcode Problem 1742------------------------\n")
|
||||
|
||||
for _, q := range qs {
|
||||
_, p := q.ans1742, q.para1742
|
||||
fmt.Printf("【input】:%v 【output】:%v\n", p, countBalls(p.lowLimit, p.highLimit))
|
||||
}
|
||||
fmt.Printf("\n\n\n")
|
||||
}
|
81
leetcode/1742.Maximum-Number-of-Balls-in-a-Box/README.md
Normal file
81
leetcode/1742.Maximum-Number-of-Balls-in-a-Box/README.md
Normal file
@ -0,0 +1,81 @@
|
||||
# [1742. Maximum Number of Balls in a Box](https://leetcode.com/problems/maximum-number-of-balls-in-a-box/)
|
||||
|
||||
|
||||
## 题目
|
||||
|
||||
You are working in a ball factory where you have `n` balls numbered from `lowLimit` up to `highLimit` **inclusive** (i.e., `n == highLimit - lowLimit + 1`), and an infinite number of boxes numbered from `1` to `infinity`.
|
||||
|
||||
Your job at this factory is to put each ball in the box with a number equal to the sum of digits of the ball's number. For example, the ball number `321` will be put in the box number `3 + 2 + 1 = 6` and the ball number `10` will be put in the box number `1 + 0 = 1`.
|
||||
|
||||
Given two integers `lowLimit` and `highLimit`, return *the number of balls in the box with the most balls.*
|
||||
|
||||
**Example 1:**
|
||||
|
||||
```
|
||||
Input: lowLimit = 1, highLimit = 10
|
||||
Output: 2
|
||||
Explanation:
|
||||
Box Number: 1 2 3 4 5 6 7 8 9 10 11 ...
|
||||
Ball Count: 2 1 1 1 1 1 1 1 1 0 0 ...
|
||||
Box 1 has the most number of balls with 2 balls.
|
||||
```
|
||||
|
||||
**Example 2:**
|
||||
|
||||
```
|
||||
Input: lowLimit = 5, highLimit = 15
|
||||
Output: 2
|
||||
Explanation:
|
||||
Box Number: 1 2 3 4 5 6 7 8 9 10 11 ...
|
||||
Ball Count: 1 1 1 1 2 2 1 1 1 0 0 ...
|
||||
Boxes 5 and 6 have the most number of balls with 2 balls in each.
|
||||
|
||||
```
|
||||
|
||||
**Example 3:**
|
||||
|
||||
```
|
||||
Input: lowLimit = 19, highLimit = 28
|
||||
Output: 2
|
||||
Explanation:
|
||||
Box Number: 1 2 3 4 5 6 7 8 9 10 11 12 ...
|
||||
Ball Count: 0 1 1 1 1 1 1 1 1 2 0 0 ...
|
||||
Box 10 has the most number of balls with 2 balls.
|
||||
|
||||
```
|
||||
|
||||
**Constraints:**
|
||||
|
||||
- `1 <= lowLimit <= highLimit <= 10^5`
|
||||
|
||||
## 题目大意
|
||||
|
||||
你在一家生产小球的玩具厂工作,有 n 个小球,编号从 lowLimit 开始,到 highLimit 结束(包括 lowLimit 和 highLimit ,即 n == highLimit - lowLimit + 1)。另有无限数量的盒子,编号从 1 到 infinity 。你的工作是将每个小球放入盒子中,其中盒子的编号应当等于小球编号上每位数字的和。例如,编号 321 的小球应当放入编号 3 + 2 + 1 = 6 的盒子,而编号 10 的小球应当放入编号 1 + 0 = 1 的盒子。
|
||||
|
||||
给你两个整数 lowLimit 和 highLimit ,返回放有最多小球的盒子中的小球数量。如果有多个盒子都满足放有最多小球,只需返回其中任一盒子的小球数量。
|
||||
|
||||
## 解题思路
|
||||
|
||||
- 简单题。循环遍历一遍数组,依次计算出所有小球的编号各位数字累加和,并且动态维护放有小球最多的数目。循环结束,输出最多小球个数即可。
|
||||
|
||||
## 代码
|
||||
|
||||
```go
|
||||
package leetcode
|
||||
|
||||
func countBalls(lowLimit int, highLimit int) int {
|
||||
buckets, maxBall := [46]int{}, 0
|
||||
for i := lowLimit; i <= highLimit; i++ {
|
||||
t := 0
|
||||
for j := i; j > 0; {
|
||||
t += j % 10
|
||||
j = j / 10
|
||||
}
|
||||
buckets[t]++
|
||||
if buckets[t] > maxBall {
|
||||
maxBall = buckets[t]
|
||||
}
|
||||
}
|
||||
return maxBall
|
||||
}
|
||||
```
|
@ -0,0 +1,17 @@
|
||||
package leetcode
|
||||
|
||||
func sumOfUnique(nums []int) int {
|
||||
freq, res := make(map[int]int), 0
|
||||
for _, v := range nums {
|
||||
if _, ok := freq[v]; !ok {
|
||||
freq[v] = 0
|
||||
}
|
||||
freq[v]++
|
||||
}
|
||||
for k, v := range freq {
|
||||
if v == 1 {
|
||||
res += k
|
||||
}
|
||||
}
|
||||
return res
|
||||
}
|
@ -0,0 +1,52 @@
|
||||
package leetcode
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
)
|
||||
|
||||
type question1748 struct {
|
||||
para1748
|
||||
ans1748
|
||||
}
|
||||
|
||||
// para 是参数
|
||||
// one 代表第一个参数
|
||||
type para1748 struct {
|
||||
nums []int
|
||||
}
|
||||
|
||||
// ans 是答案
|
||||
// one 代表第一个答案
|
||||
type ans1748 struct {
|
||||
one int
|
||||
}
|
||||
|
||||
func Test_Problem1748(t *testing.T) {
|
||||
|
||||
qs := []question1748{
|
||||
|
||||
{
|
||||
para1748{[]int{1, 2, 3, 2}},
|
||||
ans1748{4},
|
||||
},
|
||||
|
||||
{
|
||||
para1748{[]int{1, 1, 1, 1, 1}},
|
||||
ans1748{0},
|
||||
},
|
||||
|
||||
{
|
||||
para1748{[]int{1, 2, 3, 4, 5}},
|
||||
ans1748{15},
|
||||
},
|
||||
}
|
||||
|
||||
fmt.Printf("------------------------Leetcode Problem 1748------------------------\n")
|
||||
|
||||
for _, q := range qs {
|
||||
_, p := q.ans1748, q.para1748
|
||||
fmt.Printf("【input】:%v 【output】:%v\n", p, sumOfUnique(p.nums))
|
||||
}
|
||||
fmt.Printf("\n\n\n")
|
||||
}
|
67
leetcode/1748.Sum-of-Unique-Elements/README.md
Normal file
67
leetcode/1748.Sum-of-Unique-Elements/README.md
Normal file
@ -0,0 +1,67 @@
|
||||
# [1748. Sum of Unique Elements](https://leetcode.com/problems/sum-of-unique-elements/)
|
||||
|
||||
|
||||
## 题目
|
||||
|
||||
You are given an integer array `nums`. The unique elements of an array are the elements that appear **exactly once** in the array.
|
||||
|
||||
Return *the **sum** of all the unique elements of* `nums`.
|
||||
|
||||
**Example 1:**
|
||||
|
||||
```
|
||||
Input: nums = [1,2,3,2]
|
||||
Output: 4
|
||||
Explanation: The unique elements are [1,3], and the sum is 4.
|
||||
```
|
||||
|
||||
**Example 2:**
|
||||
|
||||
```
|
||||
Input: nums = [1,1,1,1,1]
|
||||
Output: 0
|
||||
Explanation: There are no unique elements, and the sum is 0.
|
||||
```
|
||||
|
||||
**Example 3:**
|
||||
|
||||
```
|
||||
Input: nums = [1,2,3,4,5]
|
||||
Output: 15
|
||||
Explanation: The unique elements are [1,2,3,4,5], and the sum is 15.
|
||||
```
|
||||
|
||||
**Constraints:**
|
||||
|
||||
- `1 <= nums.length <= 100`
|
||||
- `1 <= nums[i] <= 100`
|
||||
|
||||
## 题目大意
|
||||
|
||||
给你一个整数数组 `nums` 。数组中唯一元素是那些只出现 **恰好一次** 的元素。请你返回 `nums` 中唯一元素的 **和** 。
|
||||
|
||||
## 解题思路
|
||||
|
||||
- 简单题。利用 map 统计出每个元素出现的频次。再累加所有频次为 1 的元素,最后输出累加和即可。
|
||||
|
||||
## 代码
|
||||
|
||||
```go
|
||||
package leetcode
|
||||
|
||||
func sumOfUnique(nums []int) int {
|
||||
freq, res := make(map[int]int), 0
|
||||
for _, v := range nums {
|
||||
if _, ok := freq[v]; !ok {
|
||||
freq[v] = 0
|
||||
}
|
||||
freq[v]++
|
||||
}
|
||||
for k, v := range freq {
|
||||
if v == 1 {
|
||||
res += k
|
||||
}
|
||||
}
|
||||
return res
|
||||
}
|
||||
```
|
@ -0,0 +1,14 @@
|
||||
package leetcode
|
||||
|
||||
func check(nums []int) bool {
|
||||
count := 0
|
||||
for i := 0; i < len(nums)-1; i++ {
|
||||
if nums[i] > nums[i+1] {
|
||||
count++
|
||||
if count > 1 || nums[0] < nums[len(nums)-1] {
|
||||
return false
|
||||
}
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
@ -0,0 +1,67 @@
|
||||
package leetcode
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
)
|
||||
|
||||
type question1752 struct {
|
||||
para1752
|
||||
ans1752
|
||||
}
|
||||
|
||||
// para 是参数
|
||||
// one 代表第一个参数
|
||||
type para1752 struct {
|
||||
nums []int
|
||||
}
|
||||
|
||||
// ans 是答案
|
||||
// one 代表第一个答案
|
||||
type ans1752 struct {
|
||||
one bool
|
||||
}
|
||||
|
||||
func Test_Problem1752(t *testing.T) {
|
||||
|
||||
qs := []question1752{
|
||||
|
||||
{
|
||||
para1752{[]int{3, 4, 5, 1, 2}},
|
||||
ans1752{true},
|
||||
},
|
||||
|
||||
{
|
||||
para1752{[]int{2, 1, 3, 4}},
|
||||
ans1752{false},
|
||||
},
|
||||
|
||||
{
|
||||
para1752{[]int{1, 2, 3}},
|
||||
ans1752{true},
|
||||
},
|
||||
|
||||
{
|
||||
para1752{[]int{1, 1, 1}},
|
||||
ans1752{true},
|
||||
},
|
||||
|
||||
{
|
||||
para1752{[]int{2, 1}},
|
||||
ans1752{true},
|
||||
},
|
||||
|
||||
{
|
||||
para1752{[]int{1, 3, 2, 4}},
|
||||
ans1752{false},
|
||||
},
|
||||
}
|
||||
|
||||
fmt.Printf("------------------------Leetcode Problem 1752------------------------\n")
|
||||
|
||||
for _, q := range qs {
|
||||
_, p := q.ans1752, q.para1752
|
||||
fmt.Printf("【input】:%v 【output】:%v\n", p, check(p.nums))
|
||||
}
|
||||
fmt.Printf("\n\n\n")
|
||||
}
|
86
leetcode/1752.Check-if-Array-Is-Sorted-and-Rotated/README.md
Normal file
86
leetcode/1752.Check-if-Array-Is-Sorted-and-Rotated/README.md
Normal file
@ -0,0 +1,86 @@
|
||||
# [1752. Check if Array Is Sorted and Rotated](https://leetcode.com/problems/check-if-array-is-sorted-and-rotated/)
|
||||
|
||||
|
||||
## 题目
|
||||
|
||||
Given an array `nums`, return `true` *if the array was originally sorted in non-decreasing order, then rotated **some** number of positions (including zero)*. Otherwise, return `false`.
|
||||
|
||||
There may be **duplicates** in the original array.
|
||||
|
||||
**Note:** An array `A` rotated by `x` positions results in an array `B` of the same length such that `A[i] == B[(i+x) % A.length]`, where `%` is the modulo operation.
|
||||
|
||||
**Example 1:**
|
||||
|
||||
```
|
||||
Input: nums = [3,4,5,1,2]
|
||||
Output: true
|
||||
Explanation: [1,2,3,4,5] is the original sorted array.
|
||||
You can rotate the array by x = 3 positions to begin on the the element of value 3: [3,4,5,1,2].
|
||||
```
|
||||
|
||||
**Example 2:**
|
||||
|
||||
```
|
||||
Input: nums = [2,1,3,4]
|
||||
Output: false
|
||||
Explanation: There is no sorted array once rotated that can make nums.
|
||||
```
|
||||
|
||||
**Example 3:**
|
||||
|
||||
```
|
||||
Input: nums = [1,2,3]
|
||||
Output: true
|
||||
Explanation: [1,2,3] is the original sorted array.
|
||||
You can rotate the array by x = 0 positions (i.e. no rotation) to make nums.
|
||||
```
|
||||
|
||||
**Example 4:**
|
||||
|
||||
```
|
||||
Input: nums = [1,1,1]
|
||||
Output: true
|
||||
Explanation: [1,1,1] is the original sorted array.
|
||||
You can rotate any number of positions to make nums.
|
||||
```
|
||||
|
||||
**Example 5:**
|
||||
|
||||
```
|
||||
Input: nums = [2,1]
|
||||
Output: true
|
||||
Explanation: [1,2] is the original sorted array.
|
||||
You can rotate the array by x = 5 positions to begin on the element of value 2: [2,1].
|
||||
```
|
||||
|
||||
**Constraints:**
|
||||
|
||||
- `1 <= nums.length <= 100`
|
||||
- `1 <= nums[i] <= 100`
|
||||
|
||||
## 题目大意
|
||||
|
||||
给你一个数组 nums 。nums 的源数组中,所有元素与 nums 相同,但按非递减顺序排列。如果 nums 能够由源数组轮转若干位置(包括 0 个位置)得到,则返回 true ;否则,返回 false 。源数组中可能存在 重复项 。
|
||||
|
||||
## 解题思路
|
||||
|
||||
- 简单题。从头扫描一遍数组,找出相邻两个元素递减的数对。如果递减的数对只有 1 个,则有可能是轮转得来的,超过 1 个,则返回 false。题干里面还提到可能有多个重复元素,针对这一情况还需要判断一下 `nums[0]` 和 `nums[len(nums)-1]` 。如果是相同元素,`nums[0] < nums[len(nums)-1]`,并且数组中间还存在一对递减的数对,这时候也是 false。判断好上述这 2 种情况,本题得解。
|
||||
|
||||
## 代码
|
||||
|
||||
```go
|
||||
package leetcode
|
||||
|
||||
func check(nums []int) bool {
|
||||
count := 0
|
||||
for i := 0; i < len(nums)-1; i++ {
|
||||
if nums[i] > nums[i+1] {
|
||||
count++
|
||||
if count > 1 || nums[0] < nums[len(nums)-1] {
|
||||
return false
|
||||
}
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
```
|
@ -0,0 +1,18 @@
|
||||
package leetcode
|
||||
|
||||
func minOperations(s string) int {
|
||||
res := 0
|
||||
for i := 0; i < len(s); i++ {
|
||||
if int(s[i]-'0') != i%2 {
|
||||
res++
|
||||
}
|
||||
}
|
||||
return min(res, len(s)-res)
|
||||
}
|
||||
|
||||
func min(a, b int) int {
|
||||
if a > b {
|
||||
return b
|
||||
}
|
||||
return a
|
||||
}
|
@ -0,0 +1,52 @@
|
||||
package leetcode
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
)
|
||||
|
||||
type question1758 struct {
|
||||
para1758
|
||||
ans1758
|
||||
}
|
||||
|
||||
// para 是参数
|
||||
// one 代表第一个参数
|
||||
type para1758 struct {
|
||||
s string
|
||||
}
|
||||
|
||||
// ans 是答案
|
||||
// one 代表第一个答案
|
||||
type ans1758 struct {
|
||||
one int
|
||||
}
|
||||
|
||||
func Test_Problem1758(t *testing.T) {
|
||||
|
||||
qs := []question1758{
|
||||
|
||||
{
|
||||
para1758{"0100"},
|
||||
ans1758{1},
|
||||
},
|
||||
|
||||
{
|
||||
para1758{"10"},
|
||||
ans1758{0},
|
||||
},
|
||||
|
||||
{
|
||||
para1758{"1111"},
|
||||
ans1758{2},
|
||||
},
|
||||
}
|
||||
|
||||
fmt.Printf("------------------------Leetcode Problem 1758------------------------\n")
|
||||
|
||||
for _, q := range qs {
|
||||
_, p := q.ans1758, q.para1758
|
||||
fmt.Printf("【input】:%v 【output】:%v\n", p, minOperations(p.s))
|
||||
}
|
||||
fmt.Printf("\n\n\n")
|
||||
}
|
@ -0,0 +1,70 @@
|
||||
# [1758. Minimum Changes To Make Alternating Binary String](https://leetcode.com/problems/minimum-changes-to-make-alternating-binary-string/)
|
||||
|
||||
|
||||
## 题目
|
||||
|
||||
You are given a string `s` consisting only of the characters `'0'` and `'1'`. In one operation, you can change any `'0'` to `'1'` or vice versa.
|
||||
|
||||
The string is called alternating if no two adjacent characters are equal. For example, the string `"010"` is alternating, while the string `"0100"` is not.
|
||||
|
||||
Return *the **minimum** number of operations needed to make* `s` *alternating*.
|
||||
|
||||
**Example 1:**
|
||||
|
||||
```
|
||||
Input: s = "0100"
|
||||
Output: 1
|
||||
Explanation: If you change the last character to '1', s will be "0101", which is alternating.
|
||||
```
|
||||
|
||||
**Example 2:**
|
||||
|
||||
```
|
||||
Input: s = "10"
|
||||
Output: 0
|
||||
Explanation: s is already alternating.
|
||||
```
|
||||
|
||||
**Example 3:**
|
||||
|
||||
```
|
||||
Input: s = "1111"
|
||||
Output: 2
|
||||
Explanation: You need two operations to reach "0101" or "1010".
|
||||
```
|
||||
|
||||
**Constraints:**
|
||||
|
||||
- `1 <= s.length <= 104`
|
||||
- `s[i]` is either `'0'` or `'1'`.
|
||||
|
||||
## 题目大意
|
||||
|
||||
你将得到一个仅包含字符“ 0”和“ 1”的字符串 `s`。 在一项操作中,你可以将任何 `'0'` 更改为 `'1'`,反之亦然。 如果两个相邻字符都不相等,则该字符串称为交替字符串。 例如,字符串“ 010”是交替的,而字符串“ 0100”则不是。 返回使 `s` 交替所需的最小操作数。
|
||||
|
||||
## 解题思路
|
||||
|
||||
- 简单题。利用数组下标奇偶交替性来判断交替字符串。交替字符串有 2 种,一个是 `'01010101……'` 还有一个是 `'1010101010……'`,这两个只需要计算出一个即可,另外一个利用 `len(s) - res` 就是答案。
|
||||
|
||||
## 代码
|
||||
|
||||
```go
|
||||
package leetcode
|
||||
|
||||
func minOperations(s string) int {
|
||||
res := 0
|
||||
for i := 0; i < len(s); i++ {
|
||||
if int(s[i]-'0') != i%2 {
|
||||
res++
|
||||
}
|
||||
}
|
||||
return min(res, len(s)-res)
|
||||
}
|
||||
|
||||
func min(a, b int) int {
|
||||
if a > b {
|
||||
return b
|
||||
}
|
||||
return a
|
||||
}
|
||||
```
|
Reference in New Issue
Block a user