Skip to content

Add solution and test-cases for problem 870 #1268

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 9 additions & 14 deletions leetcode/801-900/0870.Advantage-Shuffle/README.md
Original file line number Diff line number Diff line change
@@ -1,28 +1,23 @@
# [870.Advantage Shuffle][title]

> [!WARNING|style:flat]
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)

## Description
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]`.

Return any permutation of `nums1` that maximizes its **advantage** with respect to `nums2`.

**Example 1:**

```
Input: a = "11", b = "1"
Output: "100"
Input: nums1 = [2,7,11,15], nums2 = [1,10,4,11]
Output: [2,11,7,15]
```

## 题意
> ...

## 题解
**Example 2:**

### 思路1
> ...
Advantage Shuffle
```go
```

Input: nums1 = [12,24,8,32], nums2 = [13,25,32,11]
Output: [24,32,8,12]
```

## 结语

Expand Down
37 changes: 35 additions & 2 deletions leetcode/801-900/0870.Advantage-Shuffle/Solution.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,38 @@
package Solution

func Solution(x bool) bool {
return x
import "sort"

func Solution(nums1 []int, nums2 []int) []int {
indies := make([]int, len(nums2))
res := make([]int, len(nums2))
for i := range nums2 {
indies[i] = i
res[i] = -1
}
sort.Slice(indies, func(i, j int) bool {
a, b := indies[i], indies[j]
return nums2[a] < nums2[b]
})
sort.Ints(nums1)
i1, i2 := len(nums1)-1, len(nums2)-1
for i1 >= 0 && i2 >= 0 {
if nums1[i1] > nums2[indies[i2]] {
res[indies[i2]] = nums1[i1]
nums1[i1] = -1
i1, i2 = i1-1, i2-1
continue
}
i2--
}
index := 0
for i := range nums1 {
if nums1[i] == -1 {
continue
}
for ; index < len(nums2) && res[index] != -1; index++ {
}
res[index] = nums1[i]
}

return res
}
21 changes: 10 additions & 11 deletions leetcode/801-900/0870.Advantage-Shuffle/Solution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,31 +9,30 @@ import (
func TestSolution(t *testing.T) {
// 测试用例
cases := []struct {
name string
inputs bool
expect bool
name string
nums1, nums2 []int
expect []int
}{
{"TestCase", true, true},
{"TestCase", true, true},
{"TestCase", false, false},
{"TestCase1", []int{2, 7, 11, 15}, []int{1, 10, 4, 11}, []int{2, 11, 7, 15}},
{"TestCase2", []int{12, 24, 8, 32}, []int{13, 25, 32, 11}, []int{24, 32, 8, 12}},
}

// 开始测试
for i, c := range cases {
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
got := Solution(c.inputs)
got := Solution(c.nums1, c.nums2)
if !reflect.DeepEqual(got, c.expect) {
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
c.expect, got, c.inputs)
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v",
c.expect, got, c.nums1, c.nums2)
}
})
}
}

// 压力测试
// 压力测试
func BenchmarkSolution(b *testing.B) {
}

// 使用案列
// 使用案列
func ExampleSolution() {
}
Loading