Loading [MathJax]/jax/output/HTML-CSS/fonts/TeX/fontdata.js

Find Maximum in Sliding Window

Descripción

Given an array of integers, rotate the array by 'N' elements.
Suhas S
Fichas por Suhas S, actualizado hace más de 1 año
Suhas S
Creado por Suhas S hace más de 7 años
4
0
1 2 3 4 5 (0)

Resumen del Recurso

Pregunta Respuesta
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; } }
Mostrar resumen completo Ocultar resumen completo

0 comentarios

There are no comments, be the first and leave one below:

Similar

JavaScript Fundamentals
Andrew Watters
Two-Dimensional Arrays
José Miguel Gallardo
Arrays - Vocabulary
vgeetha
Arrays
Tony Meman
Arrays
vgeetha
Data Types - Arrays and Records
Adele Tidball
Arrays
Jenny Degling
Computer science unit 2
tabassum88 abedi
Computer science unit 2
tabassum88 abedi