Skip to content
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

Add solution and test-cases for problem 775 #1168

Merged
merged 1 commit into from
Apr 11, 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
35 changes: 21 additions & 14 deletions leetcode/701-800/0775.Global-and-Local-Inversions/README.md
Original file line number Diff line number Diff line change
@@ -1,28 +1,35 @@
# [775.Global and Local Inversions][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 an integer array `nums` of length `n` which represents a permutation of all the integers in the range `[0, n - 1]`.

The number of **global inversions** is the number of the different pairs `(i, j)` where:

- `0 <= i < j < n`
- `nums[i] > nums[j]`

The number of **local inversions** is the number of indices `i` where:

- `0 <= i < n - 1`
- `nums[i] > nums[i + 1]`

Return `true` if the number of **global inversions** is equal to the number of **local inversions**.

**Example 1:**

```
Input: a = "11", b = "1"
Output: "100"
Input: nums = [1,0,2]
Output: true
Explanation: There is 1 global inversion and 1 local inversion.
```

## 题意
> ...

## 题解
**Example 2:**

### 思路1
> ...
Global and Local Inversions
```go
```

Input: nums = [1,2,0]
Output: false
Explanation: There are 2 global inversions and 1 local inversion.
```

## 结语

Expand Down
47 changes: 45 additions & 2 deletions leetcode/701-800/0775.Global-and-Local-Inversions/Solution.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,48 @@
package Solution

func Solution(x bool) bool {
return x
func merge(nums []int, left, right int) int {
mid := (right-left)/2 + left
i, j := left, mid+1
temp := make([]int, right-left+1)
k := 0
reverseOrderPairs := 0
for ; i <= mid && j <= right; k++ {
if nums[i] > nums[j] {
reverseOrderPairs += mid - i + 1
temp[k] = nums[j]
j++
continue
}
temp[k] = nums[i]
i++
}
for ; i <= mid; i, k = i+1, k+1 {
temp[k] = nums[i]

}
for ; j <= right; j, k = j+1, k+1 {
temp[k] = nums[j]
}
for ; k > 0; k-- {
nums[left+k-1] = temp[k-1]
}
return reverseOrderPairs
}
func mergeSort(nums []int, left, right int) int {
if left < right {
mid := (right-left)/2 + left
return mergeSort(nums, left, mid) + mergeSort(nums, mid+1, right) + merge(nums, left, right)
}
return 0
}

func Solution(nums []int) bool {
local := 0
for i := 0; i < len(nums)-1; i++ {
if nums[i] > nums[i+1] {
local++
}
}
global := mergeSort(nums, 0, len(nums)-1)
return local == global
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,11 @@ func TestSolution(t *testing.T) {
// 测试用例
cases := []struct {
name string
inputs bool
inputs []int
expect bool
}{
{"TestCase", true, true},
{"TestCase", true, true},
{"TestCase", false, false},
{"TestCase1", []int{1, 0, 2}, true},
{"TestCase2", []int{1, 2, 0}, false},
}

// 开始测试
Expand All @@ -30,10 +29,10 @@ func TestSolution(t *testing.T) {
}
}

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

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