From 2992926ed3bd0bef19ec737f28b2d3d3c2222ea9 Mon Sep 17 00:00:00 2001 From: 0xff-dev Date: Mon, 14 Apr 2025 22:47:31 +0800 Subject: [PATCH] Add solution and test-cases for problem 954 --- .../0954.Array-of-Doubled-Pairs/README.md | 27 ++++----- .../0954.Array-of-Doubled-Pairs/Solution.go | 56 ++++++++++++++++++- .../Solution_test.go | 12 ++-- 3 files changed, 74 insertions(+), 21 deletions(-) diff --git a/leetcode/901-1000/0954.Array-of-Doubled-Pairs/README.md b/leetcode/901-1000/0954.Array-of-Doubled-Pairs/README.md index e00614b81..20beb7592 100644 --- a/leetcode/901-1000/0954.Array-of-Doubled-Pairs/README.md +++ b/leetcode/901-1000/0954.Array-of-Doubled-Pairs/README.md @@ -1,28 +1,29 @@ # [954.Array of Doubled 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 of even length `arr`, return `true` if it is possible to reorder arr such that `arr[2 * i + 1] = 2 * arr[2 * i]` for every `0 <= i < len(arr) / 2`, or `false` otherwise. **Example 1:** ``` -Input: a = "11", b = "1" -Output: "100" +Input: arr = [3,1,3,6] +Output: false ``` -## 题意 -> ... - -## 题解 +**Example 2:** -### 思路1 -> ... -Array of Doubled Pairs -```go +``` +Input: arr = [2,1,2,6] +Output: false ``` +**Example 3:** + +``` +Input: arr = [4,-2,2,-4] +Output: true +Explanation: We can take two groups, [-2,-4] and [2,4] to form [-2,-4,2,4] or [2,4,-2,-4]. +``` ## 结语 diff --git a/leetcode/901-1000/0954.Array-of-Doubled-Pairs/Solution.go b/leetcode/901-1000/0954.Array-of-Doubled-Pairs/Solution.go index d115ccf5e..cf12e5066 100644 --- a/leetcode/901-1000/0954.Array-of-Doubled-Pairs/Solution.go +++ b/leetcode/901-1000/0954.Array-of-Doubled-Pairs/Solution.go @@ -1,5 +1,57 @@ package Solution -func Solution(x bool) bool { - return x +import "sort" + +type index954 struct { + idx int + indies []int +} + +func Solution(arr []int) bool { + sort.Slice(arr, func(i, j int) bool { + a := arr[i] + b := arr[j] + if a < 0 { + a = -a + } + if b < 0 { + b = -b + } + return a < b + }) + + cnt := make(map[int]index954) + for i, n := range arr { + if _, ok := cnt[n]; !ok { + cnt[n] = index954{idx: 0, indies: []int{}} + } + ic := cnt[n] + ic.indies = append(ic.indies, i) + cnt[n] = ic + } + + skip := make([]bool, len(arr)) + index := 0 + for ; index < len(arr) && arr[index] == 0; index++ { + } + if index&1 == 1 { + return false + } + for ; index < len(arr); index++ { + if skip[index] { + continue + } + target := arr[index] * 2 + v, ok := cnt[target] + if !ok { + return false + } + if v.idx == len(v.indies) { + return false + } + skip[v.indies[v.idx]] = true + v.idx++ + cnt[target] = v + } + return true } diff --git a/leetcode/901-1000/0954.Array-of-Doubled-Pairs/Solution_test.go b/leetcode/901-1000/0954.Array-of-Doubled-Pairs/Solution_test.go index 14ff50eb4..5c10d2d37 100644 --- a/leetcode/901-1000/0954.Array-of-Doubled-Pairs/Solution_test.go +++ b/leetcode/901-1000/0954.Array-of-Doubled-Pairs/Solution_test.go @@ -10,12 +10,12 @@ 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{3, 1, 3, 6}, false}, + {"TestCase2", []int{2, 1, 2, 6}, false}, + {"TestCase3", []int{4, -2, 2, -4}, true}, } // 开始测试 @@ -30,10 +30,10 @@ func TestSolution(t *testing.T) { } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }