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;
    }
}
相关推荐
Alfred king1 小时前
面试150 生命游戏
leetcode·游戏·面试·数组
薰衣草23338 小时前
一天两道力扣(1)
算法·leetcode·职场和发展
爱coding的橙子8 小时前
每日算法刷题Day41 6.28:leetcode前缀和2道题,用时1h20min(要加快)
算法·leetcode·职场和发展
前端拿破轮11 小时前
不是吧不是吧,leetcode第一题我就做不出来?😭😭😭
后端·算法·leetcode
前端拿破轮11 小时前
😭😭😭看到这个快乐数10s,我就知道快乐不属于我了🤪
算法·leetcode·typescript
今天背单词了吗98016 小时前
算法学习笔记:4.KMP 算法——从原理到实战,涵盖 LeetCode 与考研 408 例题
笔记·学习·考研·算法·leetcode·kmp算法
hn小菜鸡1 天前
LeetCode 377.组合总和IV
数据结构·算法·leetcode
亮亮爱刷题10 天前
飞往大厂梦之算法提升-7
数据结构·算法·leetcode·动态规划
zmuy10 天前
124. 二叉树中的最大路径和
数据结构·算法·leetcode
chao_78910 天前
滑动窗口题解——找到字符串中所有字母异位词【LeetCode】
数据结构·算法·leetcode