Skip to content

Commit b857cb5

Browse files
committed
Add solution and test-cases for problem 3668
1 parent 7ce34f6 commit b857cb5

File tree

3 files changed

+48
-26
lines changed

3 files changed

+48
-26
lines changed

leetcode/3601-3700/3668.Restore-Finishing-Order/README.md

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,41 @@
11
# [3668.Restore Finishing Order][title]
22

3-
> [!WARNING|style:flat]
4-
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
5-
63
## Description
4+
are given an integer array `order` of length n and an integer array `friends`.
5+
6+
- `order` contains every integer from 1 to `n` **exactly once**, representing the IDs of the participants of a race in their **finishing** order.
7+
- `friends` contains the IDs of your friends in the race **sorted** in strictly increasing order. Each ID in friends is guaranteed to appear in the `order` array.
8+
9+
Return an array containing your friends' IDs in their **finishing** order.
710

811
**Example 1:**
912

1013
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
13-
```
1414
15-
## 题意
16-
> ...
1715
18-
## 题解
16+
Input: order = [3,1,2,5,4], friends = [1,3,4]
17+
18+
Output: [3,1,4]
19+
20+
Explanation:
21+
22+
The finishing order is [3, 1, 2, 5, 4]. Therefore, the finishing order of your friends is [3, 1, 4].
23+
```
24+
25+
**Example 2:**
1926

20-
### 思路1
21-
> ...
22-
Restore Finishing Order
23-
```go
2427
```
2528
2629
30+
Input: order = [1,4,5,3,2], friends = [2,5]
31+
32+
Output: [5,2]
33+
34+
Explanation:
35+
36+
The finishing order is [1, 4, 5, 3, 2]. Therefore, the finishing order of your friends is [5, 2].
37+
```
38+
2739
## 结语
2840

2941
如果你同我一样热爱数据结构、算法、LeetCode,可以关注我 GitHub 上的 LeetCode 题解:[awesome-golang-algorithm][me]
Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,16 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(order []int, friends []int) []int {
4+
friendsMap := make(map[int]struct{})
5+
for _, n := range friends {
6+
friendsMap[n] = struct{}{}
7+
}
8+
index := 0
9+
for _, n := range order {
10+
if _, ok := friendsMap[n]; ok {
11+
friends[index] = n
12+
index++
13+
}
14+
}
15+
return friends
516
}

leetcode/3601-3700/3668.Restore-Finishing-Order/Solution_test.go

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,31 +9,30 @@ import (
99
func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
12-
name string
13-
inputs bool
14-
expect bool
12+
name string
13+
order, friends []int
14+
expect []int
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase1", []int{3, 1, 2, 5, 4}, []int{1, 3, 4}, []int{3, 1, 4}},
17+
{"TestCase2", []int{1, 4, 5, 3, 2}, []int{2, 5}, []int{5, 2}},
1918
}
2019

2120
// 开始测试
2221
for i, c := range cases {
2322
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
24-
got := Solution(c.inputs)
23+
got := Solution(c.order, c.friends)
2524
if !reflect.DeepEqual(got, c.expect) {
26-
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
27-
c.expect, got, c.inputs)
25+
t.Fatalf("expected: %v, but got: %v, with inputs: %v 5v",
26+
c.expect, got, c.order, c.friends)
2827
}
2928
})
3029
}
3130
}
3231

33-
// 压力测试
32+
// 压力测试
3433
func BenchmarkSolution(b *testing.B) {
3534
}
3635

37-
// 使用案列
36+
// 使用案列
3837
func ExampleSolution() {
3938
}

0 commit comments

Comments
 (0)