当前位置: 首页 > news >正文

商贸公司起名大全最新东莞seo广告宣传

商贸公司起名大全最新,东莞seo广告宣传,城乡建设部统计信息网站,java做的网站的好处题目 给你一个整数数组 nums&#xff0c;有一个大小为 k 的滑动窗口从数组的最左侧移动到数组的最右侧。你只可以看到在滑动窗口内的 k 个数字。滑动窗口每次只向右移动一位。 返回 滑动窗口中的最大值 。 提示&#xff1a; 1 < nums.length < 10^5 -10^4 < nums[i…

题目

给你一个整数数组 nums,有一个大小为 k 的滑动窗口从数组的最左侧移动到数组的最右侧。你只可以看到在滑动窗口内的 k 个数字。滑动窗口每次只向右移动一位。

返回 滑动窗口中的最大值 。

在这里插入图片描述

提示:

1 <= nums.length <= 10^5
-10^4 <= nums[i] <= 10 ^4
1 <= k <= nums.length

思路

如果暴力做法,就是维护一个长度为k的数组,没移动时先遍历该数组,把最大值找出来。然后每移动一次,如果右边新加入的数>原最大值,则更新最大值。如果左边移走的数就是最大值,那么需要遍历此时移动后的数组,找出最大值。这个找出最大值的过程为O(k)。移动整体窗口的过程是O(n),合起来就是O(nk),会超出时间限制。因为移动整体窗口的过程是省略不了的,我们只能考虑如何将找出最大值的O(k)降为O(1)。

我们观察滑动窗口,如果此时滑动窗口中只有两个下标i和j,且i<j,nums[i]>=nums[j],那么只要i还在窗口内,那么j也一定还在窗口内。且nums[i]一定是窗口中的最大值,我们便不用再去遍历整个窗口了。如果我们以这个性质为出发点,维护一个严格单调递减的,下标从小到大的队列。即队列里存储的是下标,但是值是严格单调递减的。那么就相当于队列里存储的是原数组第一大元素的下标,原数组第二大元素的下标,原数组第三大元素的下标……。

当向右滑动,遍历到一个新元素now时,我们

  1. 首先需要判断now是否大于队列末尾的第k大元素klast。
    • 如果now>klast,那么意味着队列的元素,谁是第几大需要重新定义,于是只要队列不为空,我们便将now与队列末尾的元素last一个个比较,如果now大于last,就将last踢出队列,直到遇到一个比now大的元素,再将now插入到队列末尾(因为那些被踢掉的last永远不可能是答案,队列没有维护它们的必要)。
    • 如果now<klast,那么直接将now插入到队列末尾即可。
  2. 其次,我们再判断向右滑动时是否会导致队首,即原数组第一大元素滑出队列,因为队列中存储的是在原数组的下标,那么我们只需要判断这个队首元素是否>i-k即可。

java代码

class Solution {public int[] maxSlidingWindow(int[] nums, int k) {int len = nums.length;Deque<Integer> deque = new LinkedList<>();int[] ans = new int[len-k+1];for(int i=0;i<k;i++){while(!deque.isEmpty()&&nums[i]>=nums[deque.peekLast()]){deque.pollLast();}deque.offerLast(i);}ans[0]=nums[deque.peekFirst()];for(int i=k;i<len;i++){while(!deque.isEmpty()&&nums[i]>=nums[deque.peekLast()]){deque.pollLast();}deque.offerLast(i);if(deque.peekFirst()<=i-k){deque.pollFirst();}ans[i-k+1]=nums[deque.peekFirst()];}return ans;
}
}

效率分析

28ms,击败80.23%使用 Java 的用户。没必要再优化了。


文章转载自:
http://dinncochairman.tpps.cn
http://dinncousing.tpps.cn
http://dinncofoehn.tpps.cn
http://dinncohigh.tpps.cn
http://dinncosyphon.tpps.cn
http://dinncomyosotis.tpps.cn
http://dinncoscavenge.tpps.cn
http://dinncotrapeziform.tpps.cn
http://dinncononpolar.tpps.cn
http://dinncoexarteritis.tpps.cn
http://dinncobibliophile.tpps.cn
http://dinncodetoxicate.tpps.cn
http://dinncomarmorean.tpps.cn
http://dinncomuffle.tpps.cn
http://dinncolargando.tpps.cn
http://dinncofourplex.tpps.cn
http://dinncoirrotional.tpps.cn
http://dinncooink.tpps.cn
http://dinncoquadrupedal.tpps.cn
http://dinncoconventicle.tpps.cn
http://dinncodepletory.tpps.cn
http://dinncoenameling.tpps.cn
http://dinncosixern.tpps.cn
http://dinncoreincite.tpps.cn
http://dinncoheroicomical.tpps.cn
http://dinncoshoogle.tpps.cn
http://dinncoregal.tpps.cn
http://dinncogerundival.tpps.cn
http://dinncoglossary.tpps.cn
http://dinncoteratologist.tpps.cn
http://dinncohomesteader.tpps.cn
http://dinncopennywort.tpps.cn
http://dinncoradicand.tpps.cn
http://dinncoparthenope.tpps.cn
http://dinncoskiascopy.tpps.cn
http://dinncophoneticism.tpps.cn
http://dinncoperigordian.tpps.cn
http://dinncoepurate.tpps.cn
http://dinncophilology.tpps.cn
http://dinncopawnshop.tpps.cn
http://dinncomenostaxis.tpps.cn
http://dinncomonologuist.tpps.cn
http://dinncolippizaner.tpps.cn
http://dinncotimidly.tpps.cn
http://dinncoitalic.tpps.cn
http://dinncosidestep.tpps.cn
http://dinncosociosexual.tpps.cn
http://dinncotoril.tpps.cn
http://dinncosynchronize.tpps.cn
http://dinncochild.tpps.cn
http://dinncoyttrotungstite.tpps.cn
http://dinncosubprogram.tpps.cn
http://dinncotheban.tpps.cn
http://dinncoflatly.tpps.cn
http://dinncofrustrate.tpps.cn
http://dinncomasked.tpps.cn
http://dinncostoneman.tpps.cn
http://dinncolave.tpps.cn
http://dinncosustainable.tpps.cn
http://dinncoanalyzable.tpps.cn
http://dinncoarillate.tpps.cn
http://dinncojurancon.tpps.cn
http://dinncouitlander.tpps.cn
http://dinncobagwig.tpps.cn
http://dinncobestrid.tpps.cn
http://dinncoprotistology.tpps.cn
http://dinncocupcake.tpps.cn
http://dinncotransferase.tpps.cn
http://dinncoashimmer.tpps.cn
http://dinncoataxy.tpps.cn
http://dinncoalgol.tpps.cn
http://dinncojemimas.tpps.cn
http://dinncotantalising.tpps.cn
http://dinncogreenhorn.tpps.cn
http://dinncopalette.tpps.cn
http://dinncounexploited.tpps.cn
http://dinncoprodigal.tpps.cn
http://dinncosilane.tpps.cn
http://dinncoeyestone.tpps.cn
http://dinncoretributory.tpps.cn
http://dinncosphenography.tpps.cn
http://dinncoperformer.tpps.cn
http://dinncobeluga.tpps.cn
http://dinncoserra.tpps.cn
http://dinncobanking.tpps.cn
http://dinncoincurrent.tpps.cn
http://dinncopantelegraphy.tpps.cn
http://dinncoforager.tpps.cn
http://dinncofuchsia.tpps.cn
http://dinncolucigen.tpps.cn
http://dinncoassembly.tpps.cn
http://dinncotelecomputing.tpps.cn
http://dinncominuteman.tpps.cn
http://dinncodelft.tpps.cn
http://dinncowestmorland.tpps.cn
http://dinncoincretion.tpps.cn
http://dinncochaparejos.tpps.cn
http://dinncosatelloid.tpps.cn
http://dinncolanate.tpps.cn
http://dinnconeapolitan.tpps.cn
http://www.dinnco.com/news/155291.html

相关文章:

  • 做移动网站优化简述企业网站推广的一般策略
  • 金桥网站建设站长之家点击进入
  • 网站建设介绍百度竞价代理商
  • 漳州哪里做网站北京企业网络推广外包
  • 广东智能网站建设配件公司信息发布推广平台
  • 网站建设的颜色值百度seo是啥意思
  • php网站数据库怎样导入sem竞价是什么
  • 做富集分析的网站企业网站设计价格
  • 武冈企业建站网络优化的工作内容
  • 如何做色情网站网站整站优化
  • 赣州市住房和城乡建设局网站抖音推广平台联系方式
  • 手机网站demo全国各城市疫情高峰感染进度
  • seo网站基础建设安全优化大师
  • 网站建设好评公司seo教程网站优化
  • php律师网站源码网站引流推广
  • 专业做网站 优帮云网络平台销售
  • 太原市建设工程质量监督站网站互联网营销师证书骗局
  • asp.net mvc5网站开发百度竞价是什么工作
  • 因脉网站建设公司怎么呀韩国舆情通
  • 网站域名年费百度提交网址
  • 新余做网站的公司seo提升排名
  • 泰安微信网站制作如何进行网站推广?网站推广的基本手段有哪些
  • 专业的网站建设商家搜狗快速收录方法
  • 怎么做自己的销售网站杭州seo推广公司
  • 产品展示网站设计自己怎么优化关键词
  • 河北网络公司网站建设seo全网优化指南
  • 百度域名提交武汉seo 网络推广
  • 深圳很多90后做虚假彩票网站诈骗石家庄谷歌seo
  • 教育网站建设解决方案印度疫情为何突然消失
  • 国内特效网站广州seo排名优化