Skip to content

Commit d2b994e

Browse files
authored
Merge pull request #1268 from 0xff-dev/870
Add solution and test-cases for problem 870
2 parents d530631 + 774945f commit d2b994e

File tree

3 files changed

+54
-27
lines changed

3 files changed

+54
-27
lines changed

leetcode/801-900/0870.Advantage-Shuffle/README.md

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,23 @@
11
# [870.Advantage Shuffle][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+
You are given two integer arrays `nums1` and `nums2` both of the same length. The **advantage** of `nums1` with respect to `nums2` is the number of indices `i` for which `nums1[i] > nums2[i]`.
5+
6+
Return any permutation of `nums1` that maximizes its **advantage** with respect to `nums2`.
77

88
**Example 1:**
99

1010
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
11+
Input: nums1 = [2,7,11,15], nums2 = [1,10,4,11]
12+
Output: [2,11,7,15]
1313
```
1414

15-
## 题意
16-
> ...
17-
18-
## 题解
15+
**Example 2:**
1916

20-
### 思路1
21-
> ...
22-
Advantage Shuffle
23-
```go
2417
```
25-
18+
Input: nums1 = [12,24,8,32], nums2 = [13,25,32,11]
19+
Output: [24,32,8,12]
20+
```
2621

2722
## 结语
2823

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,38 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
import "sort"
4+
5+
func Solution(nums1 []int, nums2 []int) []int {
6+
indies := make([]int, len(nums2))
7+
res := make([]int, len(nums2))
8+
for i := range nums2 {
9+
indies[i] = i
10+
res[i] = -1
11+
}
12+
sort.Slice(indies, func(i, j int) bool {
13+
a, b := indies[i], indies[j]
14+
return nums2[a] < nums2[b]
15+
})
16+
sort.Ints(nums1)
17+
i1, i2 := len(nums1)-1, len(nums2)-1
18+
for i1 >= 0 && i2 >= 0 {
19+
if nums1[i1] > nums2[indies[i2]] {
20+
res[indies[i2]] = nums1[i1]
21+
nums1[i1] = -1
22+
i1, i2 = i1-1, i2-1
23+
continue
24+
}
25+
i2--
26+
}
27+
index := 0
28+
for i := range nums1 {
29+
if nums1[i] == -1 {
30+
continue
31+
}
32+
for ; index < len(nums2) && res[index] != -1; index++ {
33+
}
34+
res[index] = nums1[i]
35+
}
36+
37+
return res
538
}

leetcode/801-900/0870.Advantage-Shuffle/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+
nums1, nums2 []int
14+
expect []int
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase1", []int{2, 7, 11, 15}, []int{1, 10, 4, 11}, []int{2, 11, 7, 15}},
17+
{"TestCase2", []int{12, 24, 8, 32}, []int{13, 25, 32, 11}, []int{24, 32, 8, 12}},
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.nums1, c.nums2)
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 %v",
26+
c.expect, got, c.nums1, c.nums2)
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)