力扣(leetcode)每日一题 3255 长度为 K 的子数组的能量值 II|滑动窗口

3255. Find the Power of K-Size Subarrays II

1.题干

You are given an array of integers nums of length n and a positive integer k.

The power of an array is defined as:

  • Its maximum element if all of its elements are consecutive and sorted in ascending order.
  • -1 otherwise.

You need to find the power of all

subarrays

of nums of size k.

Return an integer array results of size n - k + 1, where results[i] is the power of nums[i..(i + k - 1)].

Example 1:

Input: nums = 1,2,3,4,3,2,5, k = 3

Output: 3,4,-1,-1,-1

Explanation:

There are 5 subarrays of nums of size 3:

  • [1, 2, 3] with the maximum element 3.
  • [2, 3, 4] with the maximum element 4.
  • [3, 4, 3] whose elements are not consecutive.
  • [4, 3, 2] whose elements are not sorted.
  • [3, 2, 5] whose elements are not consecutive.

Example 2:

Input: nums = 2,2,2,2,2, k = 4

Output: -1,-1

Example 3:

Input: nums = 3,2,3,2,3,2, k = 2

Output: -1,3,-1,3,-1

2.题解

滑动窗口,常规解法

java 复制代码
  
public static int[] resultsArray(int[] nums, int k) { //3  
    LinkedList<Integer> list = new LinkedList<>();  
    for (int i = 0; i < k - 1; i++) {  
        // 保证队列一次从小到大差值1排列  
        while (!list.isEmpty() && nums[list.getLast()] != (nums[i] - 1)) {  
            list.pollLast();  
        }  
        list.add(i);  
    }  
    int[] res = new int[nums.length - k + 1];  
    for (int i = k - 1; i < nums.length; i++) {  
        // 保证队列一次从小到大差值1排列  
        while (!list.isEmpty() && nums[list.getLast()] != (nums[i] - 1)) {  
            list.pollLast();  
        }  
        list.add(i);  
        // 如果队列数量和k相等,就是成立的  
        if (list.size() == k) {  
            res[i - k + 1] = nums[i];  
        } else {  
            res[i - k + 1] = -1;  
        }  
        // 加入当前为index 6,然后队列长度k为3,那么对于index 4的数据进行弹出,确保下个循环index 7进来之后,队列长度维持在k为3  
        if (list.getFirst() == i - k + 1) {  
            list.pollFirst();  
        }  
    }  
    return res;  
}

官方题解,属于技巧性

java 复制代码
public int[] resultsArray(int[] nums, int k) {  
    int n = nums.length;  
    int[] ans = new int[n - k + 1];  
    Arrays.fill(ans, -1);  
    int cnt = 0;  
    for (int i = 0; i < n; i++) {  
        cnt = i == 0 || nums[i] - nums[i - 1] != 1 ? 1 : cnt + 1;  
        // 当连续数的时候,累计加上1  
        if (cnt >= k) {  
            ans[i - k + 1] = nums[i];  
        }  
    }  
    return ans;  
}
3.总结

面试编程时候肯定思维紧张,往滑动窗口上靠就对了

相关推荐
罗西的思考15 分钟前
机器人模型(WM / WAM / VLA)综合分析与对比:从「看」到「想」再到「做」
人工智能·算法·机器学习
小哈里1 小时前
【知识】从学科到实践:自然・工程・社科・人文|算法・研发・产品・品牌设计
程序人生·算法·产品·设计·工程
小π军2 小时前
最大重叠区间数量
数据结构·算法
镜像视界(浙江)科技有限公司2 小时前
《视频孪生之上:二维展示终结,三维空间计算重构城市逻辑》——跨摄像连续表达 × 三角测量厘米级定位 × 动态轨迹建模,构建新一代城市空间
大数据·人工智能·算法·矩阵·音视频·空间计算
a187927218313 小时前
【算法】双指针与滑动窗口(二):滑动窗口——吃进、判定、吐出
算法·leetcode·双指针·滑动窗口·原理·模板·算法讲解
铭哥的编程日记3 小时前
从一道 LeetCode Hard 到吃透一类题:加权区间调度「排序 + 二分 + DP」
算法·leetcode·职场和发展
Navigator_Z3 小时前
LeetCode //C - 1248. Count Number of Nice Subarrays
c语言·算法·leetcode
wzdark4 小时前
多维数组在算法设计中的存储映射问题4
算法
Phil3235 小时前
多智能体不是越多越好:Google《Towards a Science of Scaling Agent Systems》论文深度解读
算法
6Hzlia5 小时前
【Classic 150 刷题计划】 LeetCode 26. 删除有序数组中的重复项 | C++ 快慢双指针经典模板
c++·算法·leetcode