Skip to content

Commit 2992926

Browse files
committed
Add solution and test-cases for problem 954
1 parent a01e89c commit 2992926

File tree

3 files changed

+74
-21
lines changed

3 files changed

+74
-21
lines changed

leetcode/901-1000/0954.Array-of-Doubled-Pairs/README.md

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,29 @@
11
# [954.Array of Doubled Pairs][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+
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.
75

86
**Example 1:**
97

108
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
9+
Input: arr = [3,1,3,6]
10+
Output: false
1311
```
1412

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

20-
### 思路1
21-
> ...
22-
Array of Doubled Pairs
23-
```go
15+
```
16+
Input: arr = [2,1,2,6]
17+
Output: false
2418
```
2519

20+
**Example 3:**
21+
22+
```
23+
Input: arr = [4,-2,2,-4]
24+
Output: true
25+
Explanation: We can take two groups, [-2,-4] and [2,4] to form [-2,-4,2,4] or [2,4,-2,-4].
26+
```
2627

2728
## 结语
2829

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,57 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
import "sort"
4+
5+
type index954 struct {
6+
idx int
7+
indies []int
8+
}
9+
10+
func Solution(arr []int) bool {
11+
sort.Slice(arr, func(i, j int) bool {
12+
a := arr[i]
13+
b := arr[j]
14+
if a < 0 {
15+
a = -a
16+
}
17+
if b < 0 {
18+
b = -b
19+
}
20+
return a < b
21+
})
22+
23+
cnt := make(map[int]index954)
24+
for i, n := range arr {
25+
if _, ok := cnt[n]; !ok {
26+
cnt[n] = index954{idx: 0, indies: []int{}}
27+
}
28+
ic := cnt[n]
29+
ic.indies = append(ic.indies, i)
30+
cnt[n] = ic
31+
}
32+
33+
skip := make([]bool, len(arr))
34+
index := 0
35+
for ; index < len(arr) && arr[index] == 0; index++ {
36+
}
37+
if index&1 == 1 {
38+
return false
39+
}
40+
for ; index < len(arr); index++ {
41+
if skip[index] {
42+
continue
43+
}
44+
target := arr[index] * 2
45+
v, ok := cnt[target]
46+
if !ok {
47+
return false
48+
}
49+
if v.idx == len(v.indies) {
50+
return false
51+
}
52+
skip[v.indies[v.idx]] = true
53+
v.idx++
54+
cnt[target] = v
55+
}
56+
return true
557
}

leetcode/901-1000/0954.Array-of-Doubled-Pairs/Solution_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ 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{3, 1, 3, 6}, false},
17+
{"TestCase2", []int{2, 1, 2, 6}, false},
18+
{"TestCase3", []int{4, -2, 2, -4}, true},
1919
}
2020

2121
// 开始测试
@@ -30,10 +30,10 @@ func TestSolution(t *testing.T) {
3030
}
3131
}
3232

33-
// 压力测试
33+
// 压力测试
3434
func BenchmarkSolution(b *testing.B) {
3535
}
3636

37-
// 使用案列
37+
// 使用案列
3838
func ExampleSolution() {
3939
}

0 commit comments

Comments
 (0)