Skip to content

Commit 0ad3629

Browse files
authored
Merge pull request #1168 from 0xff-dev/775
Add solution and test-cases for problem 775
2 parents d47417a + 6121a2c commit 0ad3629

File tree

3 files changed

+71
-22
lines changed

3 files changed

+71
-22
lines changed

leetcode/701-800/0775.Global-and-Local-Inversions/README.md

+21-14
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,35 @@
11
# [775.Global and Local Inversions][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 an integer array `nums` of length `n` which represents a permutation of all the integers in the range `[0, n - 1]`.
5+
6+
The number of **global inversions** is the number of the different pairs `(i, j)` where:
7+
8+
- `0 <= i < j < n`
9+
- `nums[i] > nums[j]`
10+
11+
The number of **local inversions** is the number of indices `i` where:
12+
13+
- `0 <= i < n - 1`
14+
- `nums[i] > nums[i + 1]`
15+
16+
Return `true` if the number of **global inversions** is equal to the number of **local inversions**.
717

818
**Example 1:**
919

1020
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
21+
Input: nums = [1,0,2]
22+
Output: true
23+
Explanation: There is 1 global inversion and 1 local inversion.
1324
```
1425

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

20-
### 思路1
21-
> ...
22-
Global and Local Inversions
23-
```go
2428
```
25-
29+
Input: nums = [1,2,0]
30+
Output: false
31+
Explanation: There are 2 global inversions and 1 local inversion.
32+
```
2633

2734
## 结语
2835

Original file line numberDiff line numberDiff line change
@@ -1,5 +1,48 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
func merge(nums []int, left, right int) int {
4+
mid := (right-left)/2 + left
5+
i, j := left, mid+1
6+
temp := make([]int, right-left+1)
7+
k := 0
8+
reverseOrderPairs := 0
9+
for ; i <= mid && j <= right; k++ {
10+
if nums[i] > nums[j] {
11+
reverseOrderPairs += mid - i + 1
12+
temp[k] = nums[j]
13+
j++
14+
continue
15+
}
16+
temp[k] = nums[i]
17+
i++
18+
}
19+
for ; i <= mid; i, k = i+1, k+1 {
20+
temp[k] = nums[i]
21+
22+
}
23+
for ; j <= right; j, k = j+1, k+1 {
24+
temp[k] = nums[j]
25+
}
26+
for ; k > 0; k-- {
27+
nums[left+k-1] = temp[k-1]
28+
}
29+
return reverseOrderPairs
30+
}
31+
func mergeSort(nums []int, left, right int) int {
32+
if left < right {
33+
mid := (right-left)/2 + left
34+
return mergeSort(nums, left, mid) + mergeSort(nums, mid+1, right) + merge(nums, left, right)
35+
}
36+
return 0
37+
}
38+
39+
func Solution(nums []int) bool {
40+
local := 0
41+
for i := 0; i < len(nums)-1; i++ {
42+
if nums[i] > nums[i+1] {
43+
local++
44+
}
45+
}
46+
global := mergeSort(nums, 0, len(nums)-1)
47+
return local == global
548
}

leetcode/701-800/0775.Global-and-Local-Inversions/Solution_test.go

+5-6
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
13+
inputs []int
1414
expect bool
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase1", []int{1, 0, 2}, true},
17+
{"TestCase2", []int{1, 2, 0}, false},
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)