力扣(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.总结

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

相关推荐
BB_CC_DD29 分钟前
四. 以Annoy算法建树的方式聚类清洗图像数据集,一次建树,无限次聚类搜索,提升聚类搜索效率。(附完整代码)
深度学习·算法·聚类
梁下轻语的秋缘2 小时前
每日c/c++题 备战蓝桥杯 ([洛谷 P1226] 快速幂求模题解)
c++·算法·蓝桥杯
CODE_RabbitV2 小时前
【深度强化学习 DRL 快速实践】逆向强化学习算法 (IRL)
算法
mit6.8243 小时前
[贪心_7] 最优除法 | 跳跃游戏 II | 加油站
数据结构·算法·leetcode
keep intensify3 小时前
通讯录完善版本(详细讲解+源码)
c语言·开发语言·数据结构·算法
shix .3 小时前
2025年PTA天梯赛正式赛 | 算法竞赛,题目详解
数据结构·算法
风铃儿~3 小时前
Java面试高频问题(26-28)
java·算法·面试
wuqingshun3141593 小时前
蓝桥杯 4. 卡片换位
算法·职场和发展·蓝桥杯
江沉晚呤时3 小时前
深入了解C# List集合及两种常见排序算法:插入排序与堆排序
windows·sql·算法·oracle·c#·排序算法·mybatis
Eric.Lee20213 小时前
数据集-目标检测系列- F35 战斗机 检测数据集 F35 plane >> DataBall
人工智能·算法·yolo·目标检测·计算机视觉