-
Notifications
You must be signed in to change notification settings - Fork 0
/
Array_of_Doubled_Pairs.swift
90 lines (71 loc) · 2.02 KB
/
Array_of_Doubled_Pairs.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/*
Given an array of integers arr of even length, return true if and only if it is possible to reorder it such that arr[2 * i + 1] = 2 * arr[2 * i] for every 0 <= i < len(arr) / 2.
Example 1:
Input: arr = [3,1,3,6]
Output: false
Example 2:
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].
Example 4:
Input: arr = [1,2,4,16,8,4]
Output: false
Constraints:
0 <= arr.length <= 3 * 104
arr.length is even.
-105 <= arr[i] <= 105
*/
/*
Solution 1:
binary search
1. sort the array, for easy handle in later searchings
2. for each element, try to see if we can find corresponding pair
--> num < 0, check num/2 (note: num%2 == 0)
--> num >= 0, check num*2 exist or not
Time Complexity: O(n/2 * log(n))
Space Complexity: O(n)
*/
class Solution {
func canReorderDoubled(_ arr: [Int]) -> Bool {
if arr.isEmpty { return true }
let n = arr.count
var arr = arr.sorted()
for i in 0..<(n/2) {
let cur = arr.removeFirst()
// -4, -2 is okay
var index = -1
if cur < 0, cur % 2 == 0 {
index = findPair(cur/2, arr)
} else if cur >= 0 {
// 0 also can try to match
index = findPair(cur*2, arr)
}
// print(2*cur, index, arr)
if index == -1 {
return false
}
arr.remove(at: index)
}
return true
}
// return index of target if arr[index] == target
func findPair(_ target: Int, _ arr: [Int]) -> Int {
if arr.isEmpty { return -1 }
var left = 0
var right = arr.count-1
while left <= right {
let mid = left + (right-left)/2
if arr[mid] == target {
return mid
} else if arr[mid] < target {
left = mid+1
} else {
right = mid-1
}
}
return -1
}
}