Skip to content

Add solution and test-cases for problem 954 #1176

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
27 changes: 14 additions & 13 deletions leetcode/901-1000/0954.Array-of-Doubled-Pairs/README.md
Original file line number Diff line number Diff line change
@@ -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].
```

## 结语

Expand Down
56 changes: 54 additions & 2 deletions leetcode/901-1000/0954.Array-of-Doubled-Pairs/Solution.go
Original file line number Diff line number Diff line change
@@ -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
}
12 changes: 6 additions & 6 deletions leetcode/901-1000/0954.Array-of-Doubled-Pairs/Solution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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},
}

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

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

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