From 6121a2c0901a23e6efa839328b3f5994b4616180 Mon Sep 17 00:00:00 2001 From: 0xff-dev Date: Sat, 5 Apr 2025 22:19:16 +0800 Subject: [PATCH] Add solution and test-cases for problem 775 --- .../README.md | 35 ++++++++------ .../Solution.go | 47 ++++++++++++++++++- .../Solution_test.go | 11 ++--- 3 files changed, 71 insertions(+), 22 deletions(-) diff --git a/leetcode/701-800/0775.Global-and-Local-Inversions/README.md b/leetcode/701-800/0775.Global-and-Local-Inversions/README.md index 473da0157..2acff5a76 100644 --- a/leetcode/701-800/0775.Global-and-Local-Inversions/README.md +++ b/leetcode/701-800/0775.Global-and-Local-Inversions/README.md @@ -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. +``` ## 结语 diff --git a/leetcode/701-800/0775.Global-and-Local-Inversions/Solution.go b/leetcode/701-800/0775.Global-and-Local-Inversions/Solution.go index d115ccf5e..462422092 100644 --- a/leetcode/701-800/0775.Global-and-Local-Inversions/Solution.go +++ b/leetcode/701-800/0775.Global-and-Local-Inversions/Solution.go @@ -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 } diff --git a/leetcode/701-800/0775.Global-and-Local-Inversions/Solution_test.go b/leetcode/701-800/0775.Global-and-Local-Inversions/Solution_test.go index 14ff50eb4..8ea36efcf 100644 --- a/leetcode/701-800/0775.Global-and-Local-Inversions/Solution_test.go +++ b/leetcode/701-800/0775.Global-and-Local-Inversions/Solution_test.go @@ -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}, } // 开始测试 @@ -30,10 +29,10 @@ func TestSolution(t *testing.T) { } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }