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

devtools/2025/2/12 17:36:17/

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.题解

滑动窗口,常规解法

  
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;  
}

官方题解,属于技巧性

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.总结

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


http://www.ppmy.cn/devtools/132568.html

相关文章

H5播放器EasyPlayer.js 流媒体播放器是否支持npm(yarn) install 安装?

EasyPlayer.js H5播放器是一款功能强大的H5视频播放器&#xff0c;它支持多种流媒体协议播放&#xff0c;包括WebSocket-FLV、HTTP-FLV、HLS&#xff08;m3u8&#xff09;、WebRTC等格式的视频流。它不仅支持H.264和H.265编码格式&#xff0c;还具备实时录像、低延时直播等功能…

springboot基于SpringBoot的旅游网站的设计与实现

摘 要 随着科学技术的飞速发展&#xff0c;各行各业都在努力与现代先进技术接轨&#xff0c;通过科技手段提高自身的优势&#xff0c;旅游网站当然也不能排除在外&#xff0c;随着旅游网站的不断成熟&#xff0c;它彻底改变了过去传统的旅游网站方式&#xff0c;不仅使旅游管理…

聚划算!Transformer-LSTM、Transformer、CNN-LSTM、LSTM、CNN五模型多变量回归预测

聚划算&#xff01;Transformer-LSTM、Transformer、CNN-LSTM、LSTM、CNN五模型多变量回归预测 目录 聚划算&#xff01;Transformer-LSTM、Transformer、CNN-LSTM、LSTM、CNN五模型多变量回归预测预测效果基本介绍程序设计参考资料 预测效果 基本介绍 聚划算&#xff01;Tran…

学习笔记——MathType公式编号:右编号和随章节变化

1.如何在word文档中插入带有编号的公式&#xff1f; 步骤&#xff1a;(前提是已经安装mathtype) 2.MathType公式编号怎么随章节变化&#xff1f; 想要编号级数也随标题级数进行自动变化&#xff0c;则需要插入或修改文档的“分隔符” 步骤&#xff1a;

应用程序知识:什么是企业应用程序?

企业应用程序简介 企业应用程序是一种具有特定用途并帮助员工完成工作的软件&#xff0c;例如文字处理、人力资源管理以及应收账款和应付账款。这些应用程序规模庞大&#xff0c;可让您的整个组织改善内部工作流程和客户服务流程。例如&#xff0c;企业应用程序允许您&#xf…

sublime可以写python吗

首先你需要安装一个Sublime Text&#xff08;http://www.sublimetext.com/&#xff09;和一个Python&#xff08;https://www.python.org/downloads/&#xff09;&#xff0c; 接下来打开Sublime Text&#xff1a; 1、如下图所示&#xff0c;点击菜单栏中的Tools —> Buil…

发布一个npm组件库包

Webpack 配置 (webpack.config.js) const path require(path); const MiniCssExtractPlugin require(mini-css-extract-plugin); const CssMinimizerPlugin require(css-minimizer-webpack-plugin); const TerserPlugin require(terser-webpack-plugin);module.exports {…

线上模型准确率估计——在没有标签的测试数据上估计模型准确率

前言 之前关注过软件工程领域的一些顶会&#xff0c;发现AI模型测试/主动学习这些领域都比较有意思。其中&#xff0c;模型准确率估计 (Automatic Model Evaluation)这个领域应该会比较有实用价值。   训练模型时拥有训练数据和验证数据及标签&#xff0c;模型上线前会用本地…