Cookie Consent by Free Privacy Policy Generator 📌 Solving Monotonic Queue/Stack Problems

🏠 Team IT Security News

TSecurity.de ist eine Online-Plattform, die sich auf die Bereitstellung von Informationen,alle 15 Minuten neuste Nachrichten, Bildungsressourcen und Dienstleistungen rund um das Thema IT-Sicherheit spezialisiert hat.
Ob es sich um aktuelle Nachrichten, Fachartikel, Blogbeiträge, Webinare, Tutorials, oder Tipps & Tricks handelt, TSecurity.de bietet seinen Nutzern einen umfassenden Überblick über die wichtigsten Aspekte der IT-Sicherheit in einer sich ständig verändernden digitalen Welt.

16.12.2023 - TIP: Wer den Cookie Consent Banner akzeptiert, kann z.B. von Englisch nach Deutsch übersetzen, erst Englisch auswählen dann wieder Deutsch!

Google Android Playstore Download Button für Team IT Security



📚 Solving Monotonic Queue/Stack Problems


💡 Newskategorie: Programmierung
🔗 Quelle: dev.to

Intro

Monotonic queue and stack problems are common in competitive programming and technical interviews. These problems involve finding the shortest or longest subarray up to a certain number or looking for the distance to a greater or lesser value. In this article, we will go through various examples of monotonic queue/stack problems and demonstrate how to solve them in Java.

Basic Example: Daily Temperatures
The dailyTemperatures method calculates the number of days it would take for the temperature to rise. It maintains a decreasing stack to store the indices. When an element breaks the decreasing order, it calculates the number of days between the current and previous index.

public int[] dailyTemperatures(int[] T) {
    Stack<Integer> stack = new Stack<>();
    int[] res = new int[T.length];
    for (int i = 0; i < T.length; i++) {
        while (!stack.isEmpty() && T[stack.peek()] < T[i]) {
            int prev = stack.pop();
            res[prev] = i - prev;
        }
        stack.push(i);
    }
    return res;
}

Decreasing Stack with Prefix Sum: Longest Well-Performing Interval
The longestWPI method finds the longest well-performing interval in an array of hours worked. It uses a decreasing stack and a prefix sum to calculate the result.

public int longestWPI(int[] hours, int K) {
    int len = hours.length;
    int[] preSum = new int[len+1];   // prefix Sum
    for (int i = 1; i <= len; i++) {
        preSum[i] = preSum[i-1] + (hours[i-1] > 8 ? 1 : -1);
    }
    Deque<Integer> stack = new LinkedList<>(); 
    for (int i = 0; i <= len; i++) {
        //building decreasing stack
        if (stack.isEmpty() || preSum[stack.peek()] > preSum[i]) {
            stack.push(i);
        }
    }
    int res = 0;
    // going from right side
    for (int j = len; j >= 0; j--) { 
        //if summ difference is greater or equals K
        // sum of values between two pointers >= K
        while (!stack.isEmpty() && preSum[stack.peek()] + K <= preSum[j]) {
            res = Math.max(res, j-stack.pop());
        }
    }
    return res;
}

Queue Shortest Subarray: Shortest Subarray with Sum at Least K
The shortestSubarray method calculates the shortest subarray with a sum at least K. It maintains an increasing queue to store the indices.

public int shortestSubarray(int[] A, int K) {
    int n = A.length + 1;
    int[] preSum = new int[n];
    for (int i = 1; i < n; i++) {
        preSum[i] = A[i - 1] + preSum[i - 1]; // presum[0] = 0
    }
    //keep deque increasing
    Deque<Integer> deque = new ArrayDeque<>();
    int result = n;
    for (int i = 0; i < n; i++) {
        //if presum decreasing poll last
        while (!deque.isEmpty() && preSum[i] <= preSum[deque.peekLast()]) {
            deque.pollLast();
        }
        //if diff >= k calculate result
        while (!deque.isEmpty() && preSum[i] - preSum
        [deque.peekFirst()] >= K) {
        result = Math.min(result, i - deque.pollFirst());
     }
     deque.addLast(i);
   }
   return result == n ? -1 : result;
}

Window Monotonic Queue: Maximum Sliding Window

The maxSlidingWindow method calculates the maximum element in a sliding window of size k. It maintains a decreasing queue to store the indices, ensuring that the first element is always the maximum.

public int[] maxSlidingWindow(int[] nums, int k) {
    int n = nums.length;
    int[] res = new int[n - k + 1];
    ArrayDeque<Integer> queue = new ArrayDeque<>();
    int j = 0;
    for (int i = 0; i < k; i++) {
        // we pop last if sequence increasing and find proper position
        while (!queue.isEmpty() && nums[queue.peekLast()] <= nums[i]) {
            queue.pollLast();
        }
        queue.addLast(i);
    }
    for (int i = k; i < n; i++) {
        // we keep first element as max
        res[j++] = nums[queue.peekFirst()];
        // we check if queue first is out of window we discard it
        while (!queue.isEmpty() && queue.peekFirst() < i - k + 1) {
            queue.pollFirst();
        }
        while (!queue.isEmpty() && nums[queue.peekLast()] <= nums[i]) {
            queue.pollLast();
        }
        queue.addLast(i);
    }
    res[j] = nums[queue.peekFirst()];

    return res;
}

Maintaining Previous Less Element and Next Less Elements: Sum of Subarray Minimums
The sumSubarrayMins method calculates the sum of subarray minimums. It maintains a stack to store the indices and keeps track of the previous and next lesser elements for each index.

public int sumSubarrayMins(int[] A) {
    int n = A.length;
    int sum = 0;
    Stack<Integer> stack = new Stack<>();

    int[] prev = new int[n];
    int[] next = new int[n];
    Arrays.fill(next, n);
    Arrays.fill(prev, -1);
    for(int i = 0; i < A.length; i++) {
        while(!stack.isEmpty() && A[stack.peek()] > A[i]) {
            int prevIndex = stack.pop();
            next[prevIndex] = i;
        }
        if (!stack.isEmpty()) {
            prev[i] = stack.peek();
        }
        stack.add(i);
    }
    for(int i = 0; i < n; i++) {
        int left = i - prev[i];
        int right = next[i] - i;
        int subarraysBtw = (left * right) % mod;
        sum += (A[i] * subarraysBtw) % mod;
        sum %= mod;
    }
    return sum;
}

Conclusion

Monotonic queue and stack problems can be effectively tackled using the concepts demonstrated in the examples above. By maintaining a decreasing or increasing stack or queue, we can efficiently solve problems related to shortest or longest subarrays, distance to greater or lesser values, and more.

...



📌 Solving Monotonic Queue/Stack Problems


📈 75.43 Punkte

📌 Understanding the Monotonic Stack To UP Your Data Structure Game


📈 36.24 Punkte

📌 iPhone 8 Plus Review - Bionic Monotonic


📈 29.95 Punkte

📌 Introduction to Monotonic Queues


📈 29.95 Punkte

📌 Hands On Monotonic Time Series Forecasting with XGBoost, using Python


📈 29.95 Punkte

📌 FreeRTOS up to 10.4.2 Queue queue.c integer overflow


📈 29.32 Punkte

📌 Amazon SQS Queue Types: Exploring Amazon Simple Queue Service?


📈 29.32 Punkte

📌 How to quickly queue songs on Apple Music (and clear the queue)


📈 29.32 Punkte

📌 You're Not Solving the Problem You Think You're Solving


📈 27.86 Punkte

📌 Solving a VM-based CTF challenge without solving it properly - gynvael.coldwind//vx.log


📈 27.86 Punkte

📌 You're Not Solving the Problem You Think You're Solving | AI Show


📈 27.86 Punkte

📌 Solving Layout Problems With CSS Grid and Friends


📈 24.52 Punkte

📌 NeuroSAT: An AI That Learned Solving Logic Problems


📈 24.52 Punkte

📌 Solving business problems at scale with IoT and context data


📈 24.52 Punkte

📌 AI Is in a 'Golden Age' and Solving Problems That Were Once Sci-fi, Amazon CEO Says


📈 24.52 Punkte

📌 Advanced Asyncio: Solving Real World Production Problems


📈 24.52 Punkte

📌 Solving problems by working together: Could quantum computing hold the key to Covid-19?


📈 24.52 Punkte

📌 Next Level Ops Podcast: Solving the Most Common WordPress Problems with Lucas Radke


📈 24.52 Punkte

📌 Meet the global students solving local problems with code


📈 24.52 Punkte

📌 #DiVOC R2R - Solving problems of the Anthropocene -- The tricky, the hard and the impossible - deuts


📈 24.52 Punkte

📌 #DiVOC R2R - Solving problems of the Anthropocene -- The tricky, the hard and the impossible


📈 24.52 Punkte

📌 Spotting and solving everyday problems with machine learning


📈 24.52 Punkte

📌 Solving Security Problems Isn't Sexy


📈 24.52 Punkte

📌 Solving business problems at scale with IoT and context data | Internet of Things Show


📈 24.52 Punkte

📌 Solving Business Problems using PowerApps with Ryan Cunningham | #LessCodeMorePower


📈 24.52 Punkte

📌 What I Learned from Solving Leetcode Problems


📈 24.52 Punkte

📌 Divide and Conquer: A powerful strategy for solving problems


📈 24.52 Punkte

📌 Quantum Computing for Optimization Problems — Solving the Knapsack Problem


📈 24.52 Punkte

📌 How to Add An App to the Google Play Store? Solving Problems With Publishing Apps


📈 24.52 Punkte

📌 Solving (some) formal math olympiad problems


📈 24.52 Punkte

📌 Course to learn solving Linux problems


📈 24.52 Punkte

📌 Solving (Some) Formal Math Olympiad Problems


📈 24.52 Punkte

📌 Solving Optimization Problems With A Quantum Computer Is Surprisingly Easy


📈 24.52 Punkte

📌 Making IP a force-enabler for solving big problems


📈 24.52 Punkte

📌 Multimodal Chain of Thoughts: Solving Problems in a Multimodal World


📈 24.52 Punkte











matomo