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

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

文章目录

思路

这个问题可以使用滑动窗口和两个单调队列来解决。一个单调队列用来维护窗口内的最大值,另一个单调队列用来维护窗口内的最小值。我们从左到右遍历数组,对于每个元素,我们首先检查如果将这个元素加入窗口后,窗口内的最大值和最小值的差是否超过限制。如果超过限制,我们就移动窗口的左边界,直到窗口内的最大值和最小值的差不超过限制为止。

解题方法

我们首先初始化两个空的单调队列和一个结果变量。然后,我们从左到右遍历输入数组,对于每个元素,我们首先检查如果将这个元素加入窗口后,窗口内的最大值和最小值的差是否超过限制。如果超过限制,我们就移动窗口的左边界,直到窗口内的最大值和最小值的差不超过限制为止。然后,我们将当前元素加入到两个单调队列中,并更新结果变量。

复杂度

时间复杂度:

O ( n ) O(n) O(n),其中n是数组的长度。每个元素只会被加入到单调队列一次,所以时间复杂度是线性的。

空间复杂度:

O ( n ) O(n) O(n),在最坏的情况下,两个单调队列中可能会包含所有的元素,所以空间复杂度是线性的。

Code

java 复制代码
class Solution {
    public static int MAXN = 100010;
    public static int[] maxdeque = new int[MAXN];
    public static int[] mindeque = new int[MAXN];
    public static int maxh, maxt, minh, mint;
    public static int[] arr;

    public static int longestSubarray(int[] nums, int limit) {
        maxh = maxt = minh = mint = 0;
        int n = nums.length;
        arr = nums;
        int ans = 0;
        for (int l = 0, r = 0; l < n; l++) {
            while (r < n && ok(limit, nums[r])) {
                push(r++);
            }
            ans = Math.max(ans, r - l);
            poll(l);
        }
        return ans;
    }
    public static void push(int r) {
        while(maxh < maxt && arr[maxdeque[maxt - 1]] <= arr[r]) {
            maxt--;
        }
        maxdeque[maxt++] = r;
        while(minh < mint && arr[mindeque[mint - 1]] >= arr[r]) {
            mint--;
        }
        mindeque[mint++] = r;

    }

    public static void poll(int l) {
        if(maxh < maxt && maxdeque[maxh] == l) {
            maxh++;
        }
        if(minh < mint && mindeque[minh] == l) {
            minh++;
        }
    }

    public static boolean ok(int limit, int number) {
        int max = maxh < maxt ? Math.max(arr[maxdeque[maxh]], number) : number;
        int min = minh < mint ? Math.min(arr[mindeque[minh]], number) : number;
        return max - min <= limit;
    }
}
相关推荐
Navigator_Z2 小时前
LeetCode //C - 1203. Sort Items by Groups Respecting Dependencies
c语言·算法·leetcode
Scabbards_15 小时前
面试Leetcode - Heap 堆
java·leetcode·面试
ValhallaCoder18 小时前
Leetcode-hot100(2026.08.17)
python·算法·leetcode
青 春 记 忆1 天前
LeetCode 104. 二叉树的最大深度|Python 解法详解
python·算法·leetcode
evans在进步1 天前
LeetCode 198:打家劫舍——Java 动态规划详解
java·leetcode·动态规划
Nil2081 天前
leetcode 234回文链表
算法·leetcode·链表
ZC跨境爬虫1 天前
LeetCode 119. 杨辉三角 II(原地更新优化详解 + Java Python 实现)
java·python·leetcode
吃着火锅x唱着歌1 天前
LeetCode 3597.分割字符串
算法·leetcode·职场和发展
Nil2082 天前
leetcode 160相交链表
算法·leetcode·链表
ZC跨境爬虫2 天前
LeetCode 108. 将有序数组转换为二叉搜索树(递归构建详解 + Java Python 实现)
java·python·leetcode