力扣(LeetCode)每日一题1438绝对差不超过限制的最长连续子数组

  • Post author:
  • Post category:其他




1438. 绝对差不超过限制的最长连续子数组



题目描述

给你一个整数数组

nums

,和一个表示限制的整数

limit

,请你返回最长连续子数组的长度,该子数组中的任意两个元素之间的绝对差必须小于或者等于

limit

如果不存在满足条件的子数组,则返回 0 。

示例 1:

输入:nums = [8,2,4,7], limit = 4
输出:2 
解释:所有子数组如下:
[8] 最大绝对差 |8-8| = 0 <= 4.
[8,2] 最大绝对差 |8-2| = 6 > 4. 
[8,2,4] 最大绝对差 |8-2| = 6 > 4.
[8,2,4,7] 最大绝对差 |8-2| = 6 > 4.
[2] 最大绝对差 |2-2| = 0 <= 4.
[2,4] 最大绝对差 |2-4| = 2 <= 4.
[2,4,7] 最大绝对差 |2-7| = 5 > 4.
[4] 最大绝对差 |4-4| = 0 <= 4.
[4,7] 最大绝对差 |4-7| = 3 <= 4.
[7] 最大绝对差 |7-7| = 0 <= 4. 
因此,满足题意的最长子数组的长度为 2 。

示例 2:

输入:nums = [10,1,2,4,7,2], limit = 5
输出:4 
解释:满足题意的最长子数组是 [2,4,7,2],其最大绝对差 |2-7| = 5 <= 5 。

示例 3:

输入:nums = [4,2,2,2,4,4,2,2], limit = 0
输出:3

提示:

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

来源:力扣(LeetCode)

链接:

https://leetcode-cn.com/problems/longest-continuous-subarray-with-absolute-diff-less-than-or-equal-to-limit



问题解答



解答一

使用 最大、最小优先队列 + 滑动窗口。



评论区高赞

的答案相似

class Solution {
    public int longestSubarray(int[] nums, int limit) {
        Queue<Integer> minQue = new PriorityQueue<>(Comparator.naturalOrder());
        Queue<Integer> maxQue = new PriorityQueue<>(Comparator.reverseOrder());

        int left = 0;
        int right = 1;
        int ans = 0;
        minQue.add(nums[0]);
        maxQue.add(nums[0]);

        while (right <= nums.length) {
            if (maxQue.peek() - minQue.peek() <= limit) {
                ans = Math.max(ans, right - left);
                if (right < nums.length) {
                    minQue.add(nums[right]);
                    maxQue.add(nums[right]);
                }
                right++;
            } else if (maxQue.peek() - minQue.peek() > limit) {
                minQue.remove(nums[left]);
                maxQue.remove(nums[left]);
                left++;
            }
        }
        return ans;
    }
}



解答二

使用 单调队列 + 滑动窗口



官方题解

方法二 相同

class Solution {
    public int longestSubarray(int[] nums, int limit) {
        Deque<Integer> minList = new LinkedList<>(); // 最小 单调列表
        Deque<Integer> maxList = new LinkedList<>(); // 最大 单调列表
        int left = 0, right = 0;
        int ans = 0;
        int n = nums.length;

        while (right < n && left < n) {

            while (!maxList.isEmpty() && nums[right] > maxList.getLast()) {
                maxList.pollLast();
            }
            maxList.offer(nums[right]);

            while (!minList.isEmpty() && nums[right] < minList.getLast()) {
                minList.pollLast();
            }
            minList.offer(nums[right]);


            if (maxList.peek() - minList.peek() > limit) {
                if (maxList.peek() == nums[left]) maxList.pollFirst();
                if (minList.peek() == nums[left]) minList.pollFirst();
                left++;
            } else {
                ans = Math.max(right - left + 1, ans);
            }
            right++;
        }

        return ans;
    }
}



版权声明:本文为Ackerman2原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。