zoukankan      html  css  js  c++  java
  • 1438. Longest Continuous Subarray With Absolute Diff Less Than or Equal to Limit

    Given an array of integers nums and an integer limit, return the size of the longest continuous subarray such that the absolute difference between any two elements is less than or equal to limit.

    In case there is no subarray satisfying the given condition return 0.

    Example 1:

    Input: nums = [8,2,4,7], limit = 4
    Output: 2 
    Explanation: All subarrays are: 
    [8] with maximum absolute diff |8-8| = 0 <= 4.
    [8,2] with maximum absolute diff |8-2| = 6 > 4. 
    [8,2,4] with maximum absolute diff |8-2| = 6 > 4.
    [8,2,4,7] with maximum absolute diff |8-2| = 6 > 4.
    [2] with maximum absolute diff |2-2| = 0 <= 4.
    [2,4] with maximum absolute diff |2-4| = 2 <= 4.
    [2,4,7] with maximum absolute diff |2-7| = 5 > 4.
    [4] with maximum absolute diff |4-4| = 0 <= 4.
    [4,7] with maximum absolute diff |4-7| = 3 <= 4.
    [7] with maximum absolute diff |7-7| = 0 <= 4. 
    Therefore, the size of the longest subarray is 2.
    

    Example 2:

    Input: nums = [10,1,2,4,7,2], limit = 5
    Output: 4 
    Explanation: The subarray [2,4,7,2] is the longest since the maximum absolute diff is |2-7| = 5 <= 5.
    

    Example 3:

    Input: nums = [4,2,2,2,4,4,2,2], limit = 0
    Output: 3
    

    Constraints:

    • 1 <= nums.length <= 10^5
    • 1 <= nums[i] <= 10^9
    • 0 <= limit <= 10^9

    题意:

      给出一个数组,求连续子串的最大长度,所求的子串要求满足 |max_element - min_element| < limit

    思路:

      用两个双向队列来模拟,max_deque用来存储当前所在位置之前元素的最大值,min_deque用来存储当前所在位置之前所有元素的最小值。用两个指针leftPointer和rightPointer来寻找最长的subArray,通过对rightPointer向后迭代,更新max_deque 和 min_deque,同时通过shrink leftPointer来使subArray满足题目要求。

    Code:

     1 class Solution {
     2 public:
     3     int longestSubarray(vector<int>& nums, int limit) {
     4         deque<int> max_deque, min_deque;
     5         int left = 0, ans = 0;
     6         for (int right = 0; right < nums.size(); ++right) {
     7             while (!max_deque.empty() && max_deque.back() < nums[right])
     8                 max_deque.pop_back();
     9             while (!min_deque.empty() && min_deque.back() > nums[right])
    10                 min_deque.pop_back();
    11             max_deque.push_back(nums[right]);
    12             min_deque.push_back(nums[right]);
    13             while (max_deque.front() - min_deque.front() > limit) {
    14                 if (max_deque.front() == nums[left]) max_deque.pop_front();
    15                 if (min_deque.front() == nums[left]) min_deque.pop_front();
    16                 left++;
    17             }
    18             ans = max(ans, right - left + 1);
    19         }
    20         return ans;
    21     }
    22 };

    参考:

      https://leetcode.com/problems/longest-continuous-subarray-with-absolute-diff-less-than-or-equal-to-limit/discuss/609743/Java-Detailed-Explanation-Sliding-Window-Deque-O(N)

    永远渴望,大智若愚(stay hungry, stay foolish)
  • 相关阅读:
    [背包问题][二进制优化] Jzoj P4224 食物
    [并查集][排序] Jzoj P4223 旅游
    [哈夫曼树][优先队列] Bzoj P4198 荷马史诗
    [hash][差分][虚树] Jzoj P6011 天天爱跑步
    [dp] Jzoj P6012 荷马史诗
    [dp][递归] Jzoj P4211 送你一棵圣诞树
    [数学] Jzoj P3912 超氧化钾
    堆学习笔记(未完待续)(洛谷p1090合并果子)
    [AC自动机]luogu P2444 病毒
    [概率期望][DP]luogu P3830 随机树
  • 原文地址:https://www.cnblogs.com/h-hkai/p/12822819.html
Copyright © 2011-2022 走看看