-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path480_hard_[heap_and_lazy]_sliding_window_median.kt
139 lines (113 loc) · 3.84 KB
/
480_hard_[heap_and_lazy]_sliding_window_median.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import kotlin.math.*
import java.util.*
data class IntegerNumber(
val index: Int,
val value: Int,
var removed: Boolean = false
)
class MedianFinder {
private var maxHeap = PriorityQueue<IntegerNumber> { integerNumber1, integerNumber2 ->
if (integerNumber1.value > 0 && integerNumber2.value < 0) {
-1
} else if (integerNumber1.value < 0 && integerNumber2.value > 0) {
1
} else {
integerNumber2.value - integerNumber1.value
}
}
private var actualMaxHeapSize = 0
private var minHeap = PriorityQueue<IntegerNumber> { integerNumber1, integerNumber2 ->
if (integerNumber1.value > 0 && integerNumber2.value < 0) {
1
} else if (integerNumber1.value < 0 && integerNumber2.value > 0) {
-1
} else {
integerNumber1.value - integerNumber2.value
}
}
private var actualMinHeapSize = 0
private var index_to_integer_number_map = mutableMapOf<Int, IntegerNumber>()
fun addNum(index: Int, num: Int) {
val integerNumberObject = IntegerNumber(index, num)
if (minHeap.isEmpty() || num > minHeap.peek().value) {
minHeap.add(integerNumberObject)
actualMinHeapSize += 1
} else {
maxHeap.add(integerNumberObject)
actualMaxHeapSize += 1
}
index_to_integer_number_map[index] = integerNumberObject
}
fun removeNumIndex(index: Int) {
var integerNumberObject = index_to_integer_number_map[index]!!
integerNumberObject.removed = true
if (minHeap.isNotEmpty() && integerNumberObject.value >= minHeap.peek().value) {
actualMinHeapSize -= 1
} else {
actualMaxHeapSize -= 1
}
}
fun findMedian(): Double {
truncateTopRemovedIntegerNumber()
if (actualMinHeapSize == actualMaxHeapSize) {
return (minHeap.peek().value.toDouble() + maxHeap.peek().value) / 2
}
if (actualMinHeapSize > actualMaxHeapSize) {
return minHeap.peek().value.toDouble()
}
return maxHeap.peek().value.toDouble()
}
fun reconcile() {
if (!shouldReconcile()) return
if (actualMinHeapSize > actualMaxHeapSize) {
val integerNumber = minHeap.poll()
maxHeap.add(integerNumber)
actualMinHeapSize -= 1
actualMaxHeapSize += 1
} else {
val integerNumber = maxHeap.poll()
minHeap.add(integerNumber)
actualMaxHeapSize -= 1
actualMinHeapSize += 1
}
}
fun truncateTopRemovedIntegerNumber() {
while (minHeap.isNotEmpty() && minHeap.peek().removed == true) {
minHeap.poll()
}
while (maxHeap.isNotEmpty() && maxHeap.peek().removed == true) {
maxHeap.poll()
}
}
private fun shouldReconcile(): Boolean {
return abs(actualMaxHeapSize - actualMinHeapSize) >= 2
}
}
class Solution {
fun medianSlidingWindow(nums: IntArray, k: Int): DoubleArray {
val medianFinder = MedianFinder()
var answer = DoubleArray(nums.size - k + 1)
var i = 0
var j = k-1
for (l in 0..j) {
medianFinder.addNum(l, nums[l])
medianFinder.reconcile()
}
while (j < nums.size) {
val median = medianFinder.findMedian()
answer[i] = median
if (j+1 == nums.size) {
break
}
medianFinder.removeNumIndex(i)
medianFinder.reconcile()
medianFinder.addNum(j+1, nums[j+1])
medianFinder.truncateTopRemovedIntegerNumber()
medianFinder.reconcile()
medianFinder.truncateTopRemovedIntegerNumber()
i += 1
j += 1
}
return answer
}
}