Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 6f40df8

Browse files
Create array-of-doubled-pairs.go
1 parent 2e6d89f commit 6f40df8

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

‎array-of-doubled-pairs.go‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
//https://leetcode.com/problems/array-of-doubled-pairs/
2+
3+
package leetcode_solutions_golang
4+
5+
import "sort"
6+
7+
func canReorderDoubled(arr []int) bool {
8+
count := make(map[int]int)
9+
for _, i := range arr {
10+
count[i]++
11+
}
12+
13+
keys := make([]int, 0)
14+
for key, _ := range count {
15+
keys = append(keys, key)
16+
}
17+
18+
sort.Ints(keys)
19+
for i := 0; i < len(keys); i++ {
20+
full := keys[i] * 2
21+
half := keys[i]
22+
countFull, isOk1 := count[full]
23+
countHalf, isOk2 := count[half]
24+
if !isOk1 || !isOk2 {
25+
continue
26+
}
27+
if countFull >= countHalf {
28+
count[full] -= countHalf
29+
count[half] -= countFull
30+
} else {
31+
count[half] -= countFull
32+
count[full] -= countHalf
33+
}
34+
}
35+
36+
for i := 0; i < len(keys); i++ {
37+
if count[keys[i]] > 0 {
38+
return false
39+
}
40+
}
41+
42+
return true
43+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /