File tree 1 file changed +43
-0
lines changed
1 file changed +43
-0
lines changed Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments