package LeetCode_480 import java.util.* /** * 480. Sliding Window Median * https://leetcode.com/problems/sliding-window-median/description/ * * Median is the middle value in an ordered integer list. If the size of the list is even, there is no middle value. So the median is the mean of the two middle value. Examples: [2,3,4] , the median is 3 [2,3], the median is (2 + 3) / 2 = 2.5 Given an array nums, there is a sliding window of size k which is moving from the very left of the array to the very right. You can only see the k numbers in the window. Each time the sliding window moves right by one position. Your job is to output the median array for each window in the original array. For example, Given nums = [1,3,-1,-3,5,3,6,7], and k = 3. Window position Median --------------- ----- [1 3 -1] -3 5 3 6 7 1 1 [3 -1 -3] 5 3 6 7 -1 1 3 [-1 -3 5] 3 6 7 -1 1 3 -1 [-3 5 3] 6 7 3 1 3 -1 -3 [5 3 6] 7 5 1 3 -1 -3 5 [3 6 7] 6 Therefore, return the median sliding window as [1,-1,-1,3,5,6]. Note: You may assume k is always valid, ie: k is always smaller than input array‘s size for non-empty array. Answers within 10^-5 of the actual value will be accepted as correct. * */ class Solution { //max heap val leftHeap = PriorityQueue<Int> { a, b -> b - a } //min heap val rightHeap = PriorityQueue<Int>() fun medianSlidingWindow(nums: IntArray, k: Int): DoubleArray { /* * solution 1: Array+IntRange, TLE, 30/42 test cases passed. * solution 2: two heap, minHeap and maxHeap, Time complexity:O(nlogn), Space complexity:O(n) * */ //solution 2 val list = ArrayList<Double>() for (i in nums.indices) { val num = nums[i] if (leftHeap.isEmpty() || num < leftHeap.peek()) { leftHeap.offer(num) } else { rightHeap.offer(num) } rebalance() //println("index-k+1:${i - k + 1}") //when current position is valid sub-array if (i - k + 1 >= 0) { if (leftHeap.size == rightHeap.size) { val left = leftHeap.peek().toDouble() val right = rightHeap.peek().toDouble() list.add((left + right) / 2) } else { list.add(leftHeap.peek().toDouble()) } val elementToBeRemove = nums[i - k + 1] /* * keep two heap can combination to below format: * [1 3 -1] -3 5 3 6 7: [1 3 -1] 1 [3 -1 -3] 5 3 6 7: [3 -1 -3] 1 3 [-1 -3 5] 3 6 7: [-1 -3 5] 1 3 -1 [-3 5 3] 6 7: [-3 5 3] 1 3 -1 -3 [5 3 6] 7: [5 3 6] 1 3 -1 -3 5 [3 6 7]:[3 6 7] * */ if (elementToBeRemove <= leftHeap.peek()) { leftHeap.remove(elementToBeRemove) } else { rightHeap.remove(elementToBeRemove) } //keep balance after heap operation rebalance() } } return list.toDoubleArray() //solution 1 /*val size = nums.size val list = ArrayList<Double>() if (size == 1) { list.add(nums[0].toDouble()) //println(list) return list.toDoubleArray() } var right = k for (i in nums.indices) { val intRange = IntRange(i, right - 1) val subArray = nums.slice(intRange).sorted() right = i + k + 1 if (subArray.size % 2 == 0) { val index = subArray.size / 2 val index2 = index - 1 val sum = subArray[index].toDouble() + subArray[index2].toDouble() val value = sum / 2 list.add(value) } else { list.add(subArray[k/2].toDouble()) } if (right > size) { break } } return list.toDoubleArray()*/ } //balance minHeap and maxHeap //the size of minHeap just can larger than the size fo maxHeap by 1 private fun rebalance() { if (leftHeap.size < rightHeap.size) { leftHeap.offer(rightHeap.poll()) } else if (leftHeap.size - rightHeap.size == 2) { rightHeap.offer(leftHeap.poll()) } } }