Question | Answer |
Find Maximum in Sliding Window | class Solution { public int[] maxSlidingWindow(int[] nums, int k) { if(nums.length == 0 || k <= 0) return nums; int r = 0; int[] slide = new int[nums.length - k + 1]; Deque<Integer> q = new ArrayDeque<>(); for(int i = 0; i< nums.length; i++){ while(!q.isEmpty() && q.peek() < i - k + 1){ q.poll(); } while(!q.isEmpty() && nums[q.peekLast()] < nums[i]){ q.pollLast(); } q.offer(i); if(i >= k - 1){ slide[r++] = nums[q.peek()]; } } return slide; } } |
There are no comments, be the first and leave one below:
Want to create your own Flashcards for free with GoConqr? Learn more.