-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
1675-minimize-deviation-in-array.kt
68 lines (58 loc) · 2.13 KB
/
1675-minimize-deviation-in-array.kt
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
//another solution using a max Heap and a min Heap.
class Solution {
fun minimumDeviation(nums: IntArray): Int {
val minHeap = PriorityQueue<Int>()
var maxHeap = PriorityQueue<Int>(Collections.reverseOrder())
var res = Integer.MAX_VALUE
// O(N)
// For each number, we are adding the largest number it can become
// Even numbers can't get bigger, so we add it
// Odd Numbers can get to twice it size, so we add that
for(num in nums) {
minHeap.add( if(num % 2 == 0) num else num * 2)
maxHeap.add( if(num % 2 == 0) num else num * 2)
}
var maxDiff = maxHeap.peek() - minHeap.peek()
var max = maxHeap.poll()
// O(nlogM * logN)
// We are halving each even number in our max, adding it to min and max, and getting the new possible min each time
// Loop until maxHeap top reached an odd number, then we are checked all possible mins
while(max % 2 == 0) {
max /= 2
minHeap.add(max)
maxHeap.add(max)
max = maxHeap.poll()
maxDiff = minOf(maxDiff, max - minHeap.peek())
}
return maxDiff
}
}
class Solution {
fun minimumDeviation(nums: IntArray): Int {
// minHeap with pair of N to it's maximum value X that N can get to.
// For odd Numbers, N = N and X = 2 * N
// For even numbers, N = N/2 until it's odd, X = N
val minHeap = PriorityQueue<Pair<Int, Int>> {a,b -> a.first - b.first}
var heapMax = 0
var res = Integer.MAX_VALUE
// O(nlogm)
for(_num in nums) {
var num = _num
while(num % 2 == 0) {
num /= 2
}
minHeap.add(num to maxOf(_num, 2 * num))
heapMax = maxOf(heapMax, num)
}
// O(nlogm * logn)
while(minHeap.size == nums.size) {
val (n, nMax) = minHeap.poll()
res = minOf(res, heapMax - n)
if(n < nMax) {
minHeap.add(n * 2 to nMax)
heapMax = maxOf(heapMax, n * 2)
}
}
return res
}
}