mirror of
https://github.com/halfrost/LeetCode-Go.git
synced 2026-03-13 10:02:05 +08:00
添加 problem 853、1030、1054
This commit is contained in:
36
Algorithms/0853. Car Fleet/853. Car Fleet.go
Normal file
36
Algorithms/0853. Car Fleet/853. Car Fleet.go
Normal file
@@ -0,0 +1,36 @@
|
||||
package leetcode
|
||||
|
||||
import (
|
||||
"sort"
|
||||
)
|
||||
|
||||
type car struct {
|
||||
time float64
|
||||
position int
|
||||
}
|
||||
|
||||
type ByPosition []car
|
||||
|
||||
func (a ByPosition) Len() int { return len(a) }
|
||||
func (a ByPosition) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
|
||||
func (a ByPosition) Less(i, j int) bool { return a[i].position > a[j].position }
|
||||
|
||||
func carFleet(target int, position []int, speed []int) int {
|
||||
n := len(position)
|
||||
if n <= 1 {
|
||||
return n
|
||||
}
|
||||
cars := make([]car, n)
|
||||
for i := 0; i < n; i++ {
|
||||
cars[i] = car{float64(target-position[i]) / float64(speed[i]), position[i]}
|
||||
}
|
||||
sort.Sort(ByPosition(cars))
|
||||
fleet, lastTime := 0, 0.0
|
||||
for i := 0; i < len(cars); i++ {
|
||||
if cars[i].time > lastTime {
|
||||
lastTime = cars[i].time
|
||||
fleet++
|
||||
}
|
||||
}
|
||||
return fleet
|
||||
}
|
||||
44
Algorithms/0853. Car Fleet/853. Car Fleet_test.go
Normal file
44
Algorithms/0853. Car Fleet/853. Car Fleet_test.go
Normal file
@@ -0,0 +1,44 @@
|
||||
package leetcode
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
)
|
||||
|
||||
type question853 struct {
|
||||
para853
|
||||
ans853
|
||||
}
|
||||
|
||||
// para 是参数
|
||||
// one 代表第一个参数
|
||||
type para853 struct {
|
||||
target int
|
||||
position []int
|
||||
speed []int
|
||||
}
|
||||
|
||||
// ans 是答案
|
||||
// one 代表第一个答案
|
||||
type ans853 struct {
|
||||
one int
|
||||
}
|
||||
|
||||
func Test_Problem853(t *testing.T) {
|
||||
|
||||
qs := []question853{
|
||||
|
||||
question853{
|
||||
para853{12, []int{10, 8, 0, 5, 3}, []int{2, 4, 1, 1, 3}},
|
||||
ans853{3},
|
||||
},
|
||||
}
|
||||
|
||||
fmt.Printf("------------------------Leetcode Problem 853------------------------\n")
|
||||
|
||||
for _, q := range qs {
|
||||
_, p := q.ans853, q.para853
|
||||
fmt.Printf("【input】:%v 【output】:%v\n", p, carFleet(p.target, p.position, p.speed))
|
||||
}
|
||||
fmt.Printf("\n\n\n")
|
||||
}
|
||||
53
Algorithms/0853. Car Fleet/README.md
Normal file
53
Algorithms/0853. Car Fleet/README.md
Normal file
@@ -0,0 +1,53 @@
|
||||
# [853. Car Fleet](https://leetcode.com/problems/car-fleet/)
|
||||
|
||||
## 题目
|
||||
|
||||
N cars are going to the same destination along a one lane road. The destination is target miles away.
|
||||
|
||||
Each car i has a constant speed speed[i] (in miles per hour), and initial position position[i] miles towards the target along the road.
|
||||
|
||||
A car can never pass another car ahead of it, but it can catch up to it, and drive bumper to bumper at the same speed.
|
||||
|
||||
The distance between these two cars is ignored - they are assumed to have the same position.
|
||||
|
||||
A car fleet is some non-empty set of cars driving at the same position and same speed. Note that a single car is also a car fleet.
|
||||
|
||||
If a car catches up to a car fleet right at the destination point, it will still be considered as one car fleet.
|
||||
|
||||
|
||||
How many car fleets will arrive at the destination?
|
||||
|
||||
|
||||
|
||||
Example 1:
|
||||
|
||||
```c
|
||||
Input: target = 12, position = [10,8,0,5,3], speed = [2,4,1,1,3]
|
||||
Output: 3
|
||||
Explanation:
|
||||
The cars starting at 10 and 8 become a fleet, meeting each other at 12.
|
||||
The car starting at 0 doesn't catch up to any other car, so it is a fleet by itself.
|
||||
The cars starting at 5 and 3 become a fleet, meeting each other at 6.
|
||||
Note that no other cars meet these fleets before the destination, so the answer is 3.
|
||||
```
|
||||
|
||||
Note:
|
||||
|
||||
1. 0 <= N <= 10 ^ 4
|
||||
2. 0 < target <= 10 ^ 6
|
||||
3. 0 < speed[i] <= 10 ^ 6
|
||||
4. 0 <= position[i] < target
|
||||
5. All initial positions are different.
|
||||
|
||||
|
||||
## 题目大意
|
||||
|
||||
汽车舰队。有 N 个车辆,每个车辆都想行驶到终点,但是每个车辆距离终点的距离不同,行驶速度也不同。当一辆汽车追上前一辆汽车以后,会形成“汽车舰队”,即形成舰队的车辆并以第一辆车的速度前进。形成舰队以后,多辆车的长度可以忽略,可以认为这些车辆都叠在一起了。一辆车也是一个舰队,当多辆车一起到达终点,也算是一起形成了舰队。问,最后有多少个“汽车舰队”到达终点了?
|
||||
|
||||
|
||||
## 解题思路
|
||||
|
||||
- 根据每辆车距离终点和速度,计算每辆车到达终点的时间,并按照距离从大到小排序(position 越大代表距离终点越近)
|
||||
- 从头往后扫描排序以后的数组,时间一旦大于前一个 car 的时间,就会生成一个新的 car fleet,最终计数加一即可。
|
||||
|
||||
|
||||
@@ -0,0 +1,23 @@
|
||||
package leetcode
|
||||
|
||||
func allCellsDistOrder(R int, C int, r0 int, c0 int) [][]int {
|
||||
longRow, longCol, result := max(abs(r0-0), abs(R-r0)), max(abs(c0-0), abs(C-c0)), make([][]int, 0)
|
||||
maxDistance := longRow + longCol
|
||||
bucket := make([][][]int, maxDistance+1)
|
||||
for i := 0; i <= maxDistance; i++ {
|
||||
bucket[i] = make([][]int, 0)
|
||||
}
|
||||
for r := 0; r < R; r++ {
|
||||
for c := 0; c < C; c++ {
|
||||
distance := abs(r-r0) + abs(c-c0)
|
||||
tmp := []int{r, c}
|
||||
bucket[distance] = append(bucket[distance], tmp)
|
||||
}
|
||||
}
|
||||
for i := 0; i <= maxDistance; i++ {
|
||||
for _, buk := range bucket[i] {
|
||||
result = append(result, buk)
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
package leetcode
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
)
|
||||
|
||||
type question1030 struct {
|
||||
para1030
|
||||
ans1030
|
||||
}
|
||||
|
||||
// para 是参数
|
||||
// one 代表第一个参数
|
||||
type para1030 struct {
|
||||
R int
|
||||
C int
|
||||
r0 int
|
||||
c0 int
|
||||
}
|
||||
|
||||
// ans 是答案
|
||||
// one 代表第一个答案
|
||||
type ans1030 struct {
|
||||
one [][]int
|
||||
}
|
||||
|
||||
func Test_Problem1030(t *testing.T) {
|
||||
|
||||
qs := []question1030{
|
||||
|
||||
question1030{
|
||||
para1030{1, 2, 0, 0},
|
||||
ans1030{[][]int{[]int{0, 0}, []int{0, 1}}},
|
||||
},
|
||||
|
||||
question1030{
|
||||
para1030{2, 2, 0, 1},
|
||||
ans1030{[][]int{[]int{0, 1}, []int{0, 0}, []int{1, 1}, []int{1, 0}}},
|
||||
},
|
||||
|
||||
question1030{
|
||||
para1030{2, 3, 1, 2},
|
||||
ans1030{[][]int{[]int{1, 2}, []int{0, 2}, []int{1, 1}, []int{0, 1}, []int{1, 0}, []int{0, 0}}},
|
||||
},
|
||||
}
|
||||
|
||||
fmt.Printf("------------------------Leetcode Problem 1030------------------------\n")
|
||||
|
||||
for _, q := range qs {
|
||||
_, p := q.ans1030, q.para1030
|
||||
fmt.Printf("【input】:%v 【output】:%v\n", p, allCellsDistOrder(p.R, p.C, p.r0, p.c0))
|
||||
}
|
||||
fmt.Printf("\n\n\n")
|
||||
}
|
||||
54
Algorithms/1030. Matrix Cells in Distance Order/README.md
Normal file
54
Algorithms/1030. Matrix Cells in Distance Order/README.md
Normal file
@@ -0,0 +1,54 @@
|
||||
# [1030. Matrix Cells in Distance Order](https://leetcode.com/problems/matrix-cells-in-distance-order/)
|
||||
|
||||
## 题目
|
||||
|
||||
We are given a matrix with R rows and C columns has cells with integer coordinates (r, c), where 0 <= r < R and 0 <= c < C.
|
||||
|
||||
Additionally, we are given a cell in that matrix with coordinates (r0, c0).
|
||||
|
||||
Return the coordinates of all cells in the matrix, sorted by their distance from (r0, c0) from smallest distance to largest distance. Here, the distance between two cells (r1, c1) and (r2, c2) is the Manhattan distance, |r1 - r2| + |c1 - c2|. (You may return the answer in any order that satisfies this condition.)
|
||||
|
||||
|
||||
|
||||
Example 1:
|
||||
|
||||
```c
|
||||
Input: R = 1, C = 2, r0 = 0, c0 = 0
|
||||
Output: [[0,0],[0,1]]
|
||||
Explanation: The distances from (r0, c0) to other cells are: [0,1]
|
||||
```
|
||||
|
||||
Example 2:
|
||||
|
||||
```c
|
||||
Input: R = 2, C = 2, r0 = 0, c0 = 1
|
||||
Output: [[0,1],[0,0],[1,1],[1,0]]
|
||||
Explanation: The distances from (r0, c0) to other cells are: [0,1,1,2]
|
||||
The answer [[0,1],[1,1],[0,0],[1,0]] would also be accepted as correct.
|
||||
```
|
||||
|
||||
Example 3:
|
||||
|
||||
```c
|
||||
Input: R = 2, C = 3, r0 = 1, c0 = 2
|
||||
Output: [[1,2],[0,2],[1,1],[0,1],[1,0],[0,0]]
|
||||
Explanation: The distances from (r0, c0) to other cells are: [0,1,1,2,2,3]
|
||||
There are other answers that would also be accepted as correct, such as [[1,2],[1,1],[0,2],[1,0],[0,1],[0,0]].
|
||||
```
|
||||
|
||||
Note:
|
||||
|
||||
1. 1 <= R <= 100
|
||||
2. 1 <= C <= 100
|
||||
3. 0 <= r0 < R
|
||||
4. 0 <= c0 < C
|
||||
|
||||
|
||||
## 题目大意
|
||||
|
||||
定义矩阵中两个元素的距离是:`(r1, c1) 和 (r2, c2) 的距离 = |r1 - r2| + |c1 - c2|`。求出矩阵中所有元素距离 `(r0, c0)` 的距离。矩阵中的元素按照坐标系的规则排列。
|
||||
|
||||
|
||||
## 解题思路
|
||||
|
||||
按照题意计算矩阵内给定点到其他每个点的距离即可。
|
||||
48
Algorithms/1054. Distant Barcodes/1054. Distant Barcodes.go
Normal file
48
Algorithms/1054. Distant Barcodes/1054. Distant Barcodes.go
Normal file
@@ -0,0 +1,48 @@
|
||||
package leetcode
|
||||
|
||||
import "sort"
|
||||
|
||||
func rearrangeBarcodes(barcodes []int) []int {
|
||||
bfs := barcodesFrequencySort(barcodes)
|
||||
if len(bfs) == 0 {
|
||||
return []int{}
|
||||
}
|
||||
res := []int{}
|
||||
j := (len(bfs)-1)/2 + 1
|
||||
for i := 0; i <= (len(bfs)-1)/2; i++ {
|
||||
res = append(res, bfs[i])
|
||||
if j < len(bfs) {
|
||||
res = append(res, bfs[j])
|
||||
}
|
||||
j++
|
||||
}
|
||||
return res
|
||||
}
|
||||
|
||||
func barcodesFrequencySort(s []int) []int {
|
||||
if len(s) == 0 {
|
||||
return []int{}
|
||||
}
|
||||
sMap := map[int]int{} // 统计每个数字出现的频次
|
||||
cMap := map[int][]int{} // 按照频次作为 key 排序
|
||||
for _, b := range s {
|
||||
sMap[b]++
|
||||
}
|
||||
for key, value := range sMap {
|
||||
cMap[value] = append(cMap[value], key)
|
||||
}
|
||||
var keys []int
|
||||
for k := range cMap {
|
||||
keys = append(keys, k)
|
||||
}
|
||||
sort.Sort(sort.Reverse(sort.IntSlice(keys)))
|
||||
res := make([]int, 0)
|
||||
for _, k := range keys {
|
||||
for i := 0; i < len(cMap[k]); i++ {
|
||||
for j := 0; j < k; j++ {
|
||||
res = append(res, cMap[k][i])
|
||||
}
|
||||
}
|
||||
}
|
||||
return res
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
package leetcode
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
)
|
||||
|
||||
type question1054 struct {
|
||||
para1054
|
||||
ans1054
|
||||
}
|
||||
|
||||
// para 是参数
|
||||
// one 代表第一个参数
|
||||
type para1054 struct {
|
||||
one []int
|
||||
}
|
||||
|
||||
// ans 是答案
|
||||
// one 代表第一个答案
|
||||
type ans1054 struct {
|
||||
one []int
|
||||
}
|
||||
|
||||
func Test_Problem1054(t *testing.T) {
|
||||
|
||||
qs := []question1054{
|
||||
|
||||
question1054{
|
||||
para1054{[]int{1, 1, 1, 2, 2, 2}},
|
||||
ans1054{[]int{2, 1, 2, 1, 2, 1}},
|
||||
},
|
||||
|
||||
question1054{
|
||||
para1054{[]int{1, 1, 1, 1, 2, 2, 3, 3}},
|
||||
ans1054{[]int{1, 3, 1, 3, 2, 1, 2, 1}},
|
||||
},
|
||||
}
|
||||
|
||||
fmt.Printf("------------------------Leetcode Problem 1054------------------------\n")
|
||||
|
||||
for _, q := range qs {
|
||||
_, p := q.ans1054, q.para1054
|
||||
fmt.Printf("【input】:%v 【output】:%v\n", p, rearrangeBarcodes(p.one))
|
||||
}
|
||||
fmt.Printf("\n\n\n")
|
||||
}
|
||||
40
Algorithms/1054. Distant Barcodes/README.md
Normal file
40
Algorithms/1054. Distant Barcodes/README.md
Normal file
@@ -0,0 +1,40 @@
|
||||
# [1054. Distant Barcodes](https://leetcode.com/problems/distant-barcodes/)
|
||||
|
||||
## 题目
|
||||
|
||||
In a warehouse, there is a row of barcodes, where the i-th barcode is barcodes[i].
|
||||
|
||||
Rearrange the barcodes so that no two adjacent barcodes are equal. You may return any answer, and it is guaranteed an answer exists.
|
||||
|
||||
|
||||
|
||||
Example 1:
|
||||
|
||||
```c
|
||||
Input: [1,1,1,2,2,2]
|
||||
Output: [2,1,2,1,2,1]
|
||||
```
|
||||
|
||||
Example 2:
|
||||
|
||||
```c
|
||||
Input: [1,1,1,1,2,2,3,3]
|
||||
Output: [1,3,1,3,2,1,2,1]
|
||||
```
|
||||
|
||||
|
||||
Note:
|
||||
|
||||
1. 1 <= barcodes.length <= 10000
|
||||
2. 1 <= barcodes[i] <= 10000
|
||||
|
||||
|
||||
## 题目大意
|
||||
|
||||
给出一个条形二维码代码数组,要求重新排列这组数字,保证相邻的两两数字不相等。输出任意一个解即可。题目保证能有一组解。
|
||||
|
||||
|
||||
## 解题思路
|
||||
|
||||
- 这一题和第 767 题原理是完全一样的。第 767 题是 Google 的面试题。
|
||||
- 解题思路比较简单,先按照每个数字的频次从高到低进行排序,注意会有频次相同的数字。排序以后,分别从第 0 号位和中间的位置开始往后取数,取完以后即为最终解。
|
||||
Reference in New Issue
Block a user