All Downloads are FREE. Search and download functionalities are using the official Maven repository.

g0201_0300.s0239_sliding_window_maximum.Solution.dart Maven / Gradle / Ivy

There is a newer version: 1.8
Show newest version
// #Hard #Top_100_Liked_Questions #Array #Heap_Priority_Queue #Sliding_Window #Queue
// #Monotonic_Queue #Udemy_Arrays #Big_O_Time_O(n*k)_Space_O(n+k)
// #2024_10_09_Time_440_ms_(100.00%)_Space_211.3_MB_(100.00%)

import 'dart:collection';

class Solution {
  List maxSlidingWindow(List nums, int k) {
    int n = nums.length;
    List res = List.filled(n - k + 1, 0);
    int x = 0;
    Queue dq = Queue();
    int i = 0;
    int j = 0;

    while (j < nums.length) {
      // Remove elements from the deque that are smaller than the current element
      while (dq.isNotEmpty && dq.last < nums[j]) {
        dq.removeLast();
      }

      // Add the current element to the deque
      dq.addLast(nums[j]);

      // When the window size reaches k
      if (j - i + 1 == k) {
        // The element at the front of the deque is the largest in the current window
        res[x] = dq.first;
        x++;

        // If the element at the front of the deque is going out of the window, remove it
        if (dq.first == nums[i]) {
          dq.removeFirst();
        }

        // Slide the window to the right
        i++;
      }

      j++;
    }

    return res;
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy