From 774945f8e0773d886b9fae7bc6c1c9ff8018b858 Mon Sep 17 00:00:00 2001 From: 0xff-dev Date: Wed, 23 Jul 2025 09:07:52 +0800 Subject: [PATCH] Add solution and test-cases for problem 870 --- .../801-900/0870.Advantage-Shuffle/README.md | 23 +++++------- .../0870.Advantage-Shuffle/Solution.go | 37 ++++++++++++++++++- .../0870.Advantage-Shuffle/Solution_test.go | 21 +++++------ 3 files changed, 54 insertions(+), 27 deletions(-) diff --git a/leetcode/801-900/0870.Advantage-Shuffle/README.md b/leetcode/801-900/0870.Advantage-Shuffle/README.md index 78cd5d817..a6dc7240d 100644 --- a/leetcode/801-900/0870.Advantage-Shuffle/README.md +++ b/leetcode/801-900/0870.Advantage-Shuffle/README.md @@ -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] +``` ## 结语 diff --git a/leetcode/801-900/0870.Advantage-Shuffle/Solution.go b/leetcode/801-900/0870.Advantage-Shuffle/Solution.go index d115ccf5e..0f8bee839 100644 --- a/leetcode/801-900/0870.Advantage-Shuffle/Solution.go +++ b/leetcode/801-900/0870.Advantage-Shuffle/Solution.go @@ -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 } diff --git a/leetcode/801-900/0870.Advantage-Shuffle/Solution_test.go b/leetcode/801-900/0870.Advantage-Shuffle/Solution_test.go index 14ff50eb4..43c69d60a 100644 --- a/leetcode/801-900/0870.Advantage-Shuffle/Solution_test.go +++ b/leetcode/801-900/0870.Advantage-Shuffle/Solution_test.go @@ -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() { }