LeetCode in Kotlin

480. Sliding Window Median

Hard

The 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 values.

You are given an integer array nums and an integer k. 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.

Return the median array for each window in the original array. Answers within 10-5 of the actual value will be accepted.

Example 1:

Input: nums = [1,3,-1,-3,5,3,6,7], k = 3

Output: [1.00000,-1.00000,-1.00000,3.00000,5.00000,6.00000]

Explanation: 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

Example 2:

Input: nums = [1,2,3,4,2,3,1,4,2], k = 3

Output: [2.00000,3.00000,3.00000,3.00000,2.00000,3.00000,2.00000]

Constraints:

Solution

import java.util.TreeSet

class Solution {
    fun medianSlidingWindow(nums: IntArray, k: Int): DoubleArray {
        require(k >= 1) { "Input is invalid" }
        val len = nums.size
        val result = DoubleArray(len - k + 1)
        if (k == 1) {
            for (i in 0 until len) {
                result[i] = nums[i].toDouble()
            }
            return result
        }
        val comparator = Comparator { a: Int?, b: Int? ->
            if (nums[a!!] != nums[b!!]
            ) {
                Integer.compare(nums[a], nums[b])
            } else {
                Integer.compare(a, b)
            }
        }
        val smallNums = TreeSet(comparator.reversed())
        val largeNums = TreeSet(comparator)
        for (i in 0 until len) {
            if (i >= k) {
                removeElement(smallNums, largeNums, i - k)
            }
            addElement(smallNums, largeNums, i)
            if (i >= k - 1) {
                result[i - (k - 1)] = getMedian(smallNums, largeNums, nums)
            }
        }
        return result
    }

    private fun addElement(smallNums: TreeSet<Int?>, largeNums: TreeSet<Int?>, idx: Int) {
        smallNums.add(idx)
        largeNums.add(smallNums.pollFirst()!!)
        if (smallNums.size < largeNums.size) {
            smallNums.add(largeNums.pollFirst())
        }
    }

    private fun removeElement(smallNums: TreeSet<Int?>, largeNums: TreeSet<Int?>, idx: Int) {
        if (largeNums.contains(idx)) {
            largeNums.remove(idx)
            if (smallNums.size == largeNums.size + 2) {
                largeNums.add(smallNums.pollFirst()!!)
            }
        } else {
            smallNums.remove(idx)
            if (smallNums.size < largeNums.size) {
                smallNums.add(largeNums.pollFirst())
            }
        }
    }

    private fun getMedian(smallNums: TreeSet<Int?>, largeNums: TreeSet<Int?>, nums: IntArray): Double {
        return if (smallNums.size == largeNums.size) {
            (nums[smallNums.first()!!].toDouble() + nums[largeNums.first()!!]) / 2
        } else {
            nums[smallNums.first()!!].toDouble()
        }
    }
}