480. Sliding Window Median
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()) } } }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· .NET10 - 预览版1新功能体验(一)