Skip to content

Commit 267b286

Browse files
committed
Add solution and test-cases for problem 493
1 parent d569e0c commit 267b286

File tree

3 files changed

+69
-23
lines changed

3 files changed

+69
-23
lines changed

leetcode/401-500/0493.Reverse-Pairs/README.md

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,34 @@
11
# [493.Reverse Pairs][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+
Given an integer array `nums`, return the number of **reverse pairs** in the array.
5+
6+
A **reverse pair** is a pair `(i, j)` where:
7+
8+
- `0 <= i < j < nums.length` and
9+
- `nums[i] > 2 * nums[j]`.
10+
711

812
**Example 1:**
913

1014
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
15+
Input: nums = [1,3,2,3,1]
16+
Output: 2
17+
Explanation: The reverse pairs are:
18+
(1, 4) --> nums[1] = 3, nums[4] = 1, 3 > 2 * 1
19+
(3, 4) --> nums[3] = 3, nums[4] = 1, 3 > 2 * 1
1320
```
1421

15-
## 题意
16-
> ...
22+
**Example 2:**
1723

18-
## 题解
19-
20-
### 思路1
21-
> ...
22-
Reverse Pairs
23-
```go
2424
```
25-
25+
Input: nums = [2,4,3,5,1]
26+
Output: 3
27+
Explanation: The reverse pairs are:
28+
(1, 4) --> nums[1] = 4, nums[4] = 1, 4 > 2 * 1
29+
(2, 4) --> nums[2] = 3, nums[4] = 1, 3 > 2 * 1
30+
(3, 4) --> nums[3] = 5, nums[4] = 1, 5 > 2 * 1
31+
```
2632

2733
## 结语
2834

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

3-
func Solution(x bool) bool {
4-
return x
3+
func merge(nums []int, start, end int) int {
4+
mid := start + (end-start)/2
5+
l, r := start, mid+1
6+
tmpNums := make([]int, 0)
7+
count := 0
8+
for ; l <= mid; l++ {
9+
for ; r <= end && nums[l] > 2*nums[r]; r++ {
10+
11+
}
12+
count += r - 1 - mid
13+
}
14+
l, r = start, mid+1
15+
for l <= mid && r <= end {
16+
if nums[l] < nums[r] {
17+
tmpNums = append(tmpNums, nums[l])
18+
l++
19+
} else {
20+
tmpNums = append(tmpNums, nums[r])
21+
r++
22+
}
23+
}
24+
for ; l <= mid; l++ {
25+
tmpNums = append(tmpNums, nums[l])
26+
}
27+
for ; r <= end; r++ {
28+
tmpNums = append(tmpNums, nums[r])
29+
}
30+
for k := 0; k < len(tmpNums); k++ {
31+
nums[k+start] = tmpNums[k]
32+
}
33+
return count
34+
}
35+
36+
func MergeSort(nums []int, start, end int) int {
37+
if start < end {
38+
mid := start + (end-start)/2
39+
return MergeSort(nums, start, mid) + MergeSort(nums, mid+1, end) + merge(nums, start, end)
40+
}
41+
return 0
42+
}
43+
44+
func Solution(nums []int) int {
45+
return MergeSort(nums, 0, len(nums)-1)
546
}

leetcode/401-500/0493.Reverse-Pairs/Solution_test.go

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

2120
// 开始测试
@@ -30,10 +29,10 @@ func TestSolution(t *testing.T) {
3029
}
3130
}
3231

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

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

0 commit comments

Comments
 (0)