diff --git a/leetcode/401-500/0493.Reverse-Pairs/README.md b/leetcode/401-500/0493.Reverse-Pairs/README.md index 86fb1c88f..0cf009b11 100644 --- a/leetcode/401-500/0493.Reverse-Pairs/README.md +++ b/leetcode/401-500/0493.Reverse-Pairs/README.md @@ -1,28 +1,34 @@ # [493.Reverse Pairs][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 +Given an integer array `nums`, return the number of **reverse pairs** in the array. + +A **reverse pair** is a pair `(i, j)` where: + +- `0 <= i < j < nums.length` and +- `nums[i] > 2 * nums[j]`. + **Example 1:** ``` -Input: a = "11", b = "1" -Output: "100" +Input: nums = [1,3,2,3,1] +Output: 2 +Explanation: The reverse pairs are: +(1, 4) --> nums[1] = 3, nums[4] = 1, 3 > 2 * 1 +(3, 4) --> nums[3] = 3, nums[4] = 1, 3 > 2 * 1 ``` -## 题意 -> ... +**Example 2:** -## 题解 - -### 思路1 -> ... -Reverse Pairs -```go ``` - +Input: nums = [2,4,3,5,1] +Output: 3 +Explanation: The reverse pairs are: +(1, 4) --> nums[1] = 4, nums[4] = 1, 4 > 2 * 1 +(2, 4) --> nums[2] = 3, nums[4] = 1, 3 > 2 * 1 +(3, 4) --> nums[3] = 5, nums[4] = 1, 5 > 2 * 1 +``` ## 结语 diff --git a/leetcode/401-500/0493.Reverse-Pairs/Solution.go b/leetcode/401-500/0493.Reverse-Pairs/Solution.go index d115ccf5e..74a4fed7c 100644 --- a/leetcode/401-500/0493.Reverse-Pairs/Solution.go +++ b/leetcode/401-500/0493.Reverse-Pairs/Solution.go @@ -1,5 +1,46 @@ package Solution -func Solution(x bool) bool { - return x +func merge(nums []int, start, end int) int { + mid := start + (end-start)/2 + l, r := start, mid+1 + tmpNums := make([]int, 0) + count := 0 + for ; l <= mid; l++ { + for ; r <= end && nums[l] > 2*nums[r]; r++ { + + } + count += r - 1 - mid + } + l, r = start, mid+1 + for l <= mid && r <= end { + if nums[l] < nums[r] { + tmpNums = append(tmpNums, nums[l]) + l++ + } else { + tmpNums = append(tmpNums, nums[r]) + r++ + } + } + for ; l <= mid; l++ { + tmpNums = append(tmpNums, nums[l]) + } + for ; r <= end; r++ { + tmpNums = append(tmpNums, nums[r]) + } + for k := 0; k < len(tmpNums); k++ { + nums[k+start] = tmpNums[k] + } + return count +} + +func MergeSort(nums []int, start, end int) int { + if start < end { + mid := start + (end-start)/2 + return MergeSort(nums, start, mid) + MergeSort(nums, mid+1, end) + merge(nums, start, end) + } + return 0 +} + +func Solution(nums []int) int { + return MergeSort(nums, 0, len(nums)-1) } diff --git a/leetcode/401-500/0493.Reverse-Pairs/Solution_test.go b/leetcode/401-500/0493.Reverse-Pairs/Solution_test.go index 14ff50eb4..5bbf05e2c 100644 --- a/leetcode/401-500/0493.Reverse-Pairs/Solution_test.go +++ b/leetcode/401-500/0493.Reverse-Pairs/Solution_test.go @@ -10,12 +10,11 @@ func TestSolution(t *testing.T) { // 测试用例 cases := []struct { name string - inputs bool - expect bool + inputs []int + expect int }{ - {"TestCase", true, true}, - {"TestCase", true, true}, - {"TestCase", false, false}, + {"TestCase1", []int{1, 3, 2, 3, 1}, 2}, + {"TestCase2", []int{2, 4, 3, 5, 1}, 3}, } // 开始测试 @@ -30,10 +29,10 @@ func TestSolution(t *testing.T) { } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }