Skip to content

Instantly share code, notes, and snippets.

@skyisle
Created March 27, 2018 06:21
Show Gist options
  • Save skyisle/2112f9bc195ceb8ac83e703866e613c6 to your computer and use it in GitHub Desktop.
Save skyisle/2112f9bc195ceb8ac83e703866e613c6 to your computer and use it in GitHub Desktop.
import java.util.*
class Solution {
fun maxSlidingWindow(nums: IntArray, k: Int): IntArray {
if(nums.isEmpty()) {
return intArrayOf()
}
var maxSlidingWindows = mutableListOf<Int>()
var q = PriorityQueue<Int>(compareBy { -it })
for(i in 0 until k){
q.add(nums[i])
}
maxSlidingWindows.add(q.poll())
for(i in k until nums.size) {
q.remove(nums[i-k]);
q.add(nums[i])
maxSlidingWindows.add(q.poll())
}
return maxSlidingWindows.toIntArray()
}
}
fun main(args: Array<String>) {
Solution().maxSlidingWindow(intArrayOf(1,3,-1,-3,5,3,6,7), 3)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment