Skip to content

Commit 42ebbe0

Browse files
committed
954_Array_of_Doubled_Pairs
1 parent 95fc0c2 commit 42ebbe0

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,7 @@ I have solved quite a number of problems from several topics. See the below tabl
191191
|89| **[128. Longest Consecutive Sequence](https://tinyurl.com/yxzab8pw)** | [Python](https://tinyurl.com/wu6rdaw/128_Longest_Consecutive_Sequence.py), [Swift](https://tinyurl.com/wuja3c4/128_Longest_Consecutive_Sequence.swift) | [Art 1](https://tinyurl.com/y5e34662) | Hard | |
192192
|90| **[1423. Maximum Points You Can Obtain from Cards](https://tinyurl.com/yxry5n5b)** | [Python](https://tinyurl.com/wu6rdaw/1423_Maximum_Points_You_Can_Obtain_from_Cards.py), [Swift](https://tinyurl.com/wuja3c4/1423_Maximum_Points_You_Can_Obtain_from_Cards.swift) | [Art 1](https://tinyurl.com/y5mohcl3), [Art 2](https://tinyurl.com/y4d7p6wh) | Medium | Very important. Learned new ways of sliding window |
193193
|91| [900. RLE Iterator](https://tinyurl.com/yxry5n5b) | [Python](https://tinyurl.com/wu6rdaw/900_RLE_Iterator.py), [Swift](https://tinyurl.com/wuja3c4/900_RLE_Iterator.swift) | --- | Medium | - |
194+
|92| [954. Array of Doubled Pairs](https://tinyurl.com/yftjpl7m) | [Swift](https://tinyurl.com/wuja3c4/954_Array_of_Doubled_Pairs.swift) | --- | Medium | - |
194195

195196

196197
</p>
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
class Solution {
2+
func canReorderDoubled(_ arr: [Int]) -> Bool {
3+
let sortedArray = arr.sorted { abs($0) < abs($1) }
4+
var counterMap = [Int:Int]()
5+
sortedArray.forEach { item in
6+
counterMap[item, default: 0] += 1
7+
}
8+
9+
for item in sortedArray {
10+
if counterMap[item]! == 0 {
11+
continue
12+
} else {
13+
counterMap[item]! -= 1
14+
}
15+
if let counter = counterMap[2 * item] {
16+
if counter == 0 {
17+
return false
18+
} else {
19+
counterMap[2 * item]! -= 1
20+
}
21+
} else {
22+
return false
23+
}
24+
}
25+
return true
26+
}
27+
}

0 commit comments

Comments
 (0)