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

金溪网站建设制作热搜关键词查询

金溪网站建设制作,热搜关键词查询,wordpress视差插件,H5建网站题意描述: 给你一个整数数组 nums,有一个大小为 k 的滑动窗口从数组的最左侧移动到数组的最右侧。你只可以看到在滑动窗口内的 k 个数字。滑动窗口每次只向右移动一位。 返回 滑动窗口中的最大值 。 示例: 输入:nums [1,3,-1,…

题意描述:

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

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

示例:

输入:nums = [1,3,-1,-3,5,3,6,7], k = 3
输出:[3,3,5,5,6,7]
解释:
滑动窗口的位置 最大值


[1 3 -1] -3 5 3 6 7 3
1 [3 -1 -3] 5 3 6 7 3
1 3 [-1 -3 5] 3 6 7 5
1 3 -1 [-3 5 3] 6 7 5
1 3 -1 -3 [5 3 6] 7 6
1 3 -1 -3 5 [3 6 7] 7

输入:nums = [1], k = 1
输出:[1]


解题思路:
Alice: 你咋想起来做这道了 ?
Bob: 我其实是在做另一道题目,多重背包的优化之单调队列优化,但是我不知道什么是单调队列,所以我需要先做一下这道题,学习一些单调队列。
Alice: 所有,有什么思路吗 ?
Bob: 学习的话,想个十分钟吧,然后看题解得了。
15-minutes-later
Alice: 好吧,题解看明白了吗 ?
Bob: 有点迷,我还是先上手写一下吧。
Alice: 没通过吧,有几个概念你要先了解。队列知道吧,先进先出。双端队列知道吗?两边都能进,两边都能出。你老用 JavaScript 的话,应该知道数组的 push pop shift unshift 这些方法吧,这就是双端队列。
Bob: 然后呢 ?
Alice:你还得知道单调队列,还有这题为啥需要单调队列来解。单调队列就是,队列中的值是单调的,单调就是数学上的那个单调,单调递增或者单调递减或者单调不增或者单调不减。
Bob: 那为啥要用单调队列呢 ?
Alice: 还是得读题啊。这题不是让求解滑动窗口的最大值吗 ?然后数组长度最大 10^5,k 的取值最大也是 10^5,按照最大的计算量 k取最大长度的一半也就是 5*10^4 然后每次都求最大值,计算量也就是 10^5 * (5 * 10^4)^2也就是 2.5 * 10^14, 时间限制是 1s 的话,一定会超时的。
Bob: 所以我们需要单调队列 ?
Alice: 差不多,单调队列非常适合这道题,这道题是让求 k 时间窗口的最大值,单调队列的头或尾一定是最大值。我们只需要在窗口滑动的过程中不断地维护队列就可以了。
Bob: 怎么维护呢 ?
Alice: 这里有两个点需要注意。1是要在维护队列的过程中不断干掉超出 k 时间窗口的值,2是要及时干掉那些不需要的值,比如说 nums[i] > nums[i-1] 的时候,nums[i-1] 就没必要继续留在队列中了,要求的是最大值 nums[i]nums[i-1] 更大且更 ”年轻“ ,求最大值的时候 nums[i] 一定会覆盖 nums[i-1]。当然这里不止 i-1 前面 ”更老更小“ 的都应该干掉。
Bob: 所以我们的数据结构中还应该有下标,通过下标来计算某个值是否超出 k 时间窗口 ?
Alice: 对的,而另一个维护队列的时机就是在新元素入队之前,或者入队的时候。
Bob: 还有什么要注意的吗 ?
Alice: 有的,有两个点,一是初始化的时候也要维护单调队列,还有就是每次维护其实是在队首和队尾都要维护,队尾要干掉那些不必要的,队首要保证在 k 时间窗口内最大。
Bob: k 时间窗口最大我是这样理解的,队尾的维护会把比较小的干掉,这样队首元素出去之后,从队尾补上来的,应该总是剩下的最大的。
Alice: 对的。
Bob: 还是挺有技巧的,上代码吧。


代码:

/*** @param {number[]} nums* @param {number} k* @return {number[]}*/
var maxSlidingWindow = function(nums, k) {const ans = []// 初始化 queueconst queue = [];for(let i=0; i<k; ++i) {// 维护队尾,去除不必要的又老又小的值while (queue.length > 0 && queue[queue.length-1][0] <= nums[i]) {queue.pop();}// 新大值入队queue.push([nums[i], i]);}ans.push(queue[0][0]);for (let i=k; i<nums.length; ++i) {// 维护队尾,去除不必要的又老又小的值while (queue.length > 0 && queue[queue.length-1][0] <= nums[i]) {queue.pop();}// 新大值入队queue.push([nums[i], i]);// 维护队首while (queue.length > 0 && queue[0][1] + k <= i) {queue.shift();}ans.push(queue[0][0]);}return ans;
};

测试用例:

[9,10,9,-7,-4,-8,2,-6]
5
[10,10,9,2]

参考:

  • 题目链接
  • 相关题目-多重背包的单调队列优化

文章转载自:
http://discover.sqLh.cn
http://subnormal.sqLh.cn
http://corium.sqLh.cn
http://methimazole.sqLh.cn
http://tibiae.sqLh.cn
http://vancomycin.sqLh.cn
http://researcher.sqLh.cn
http://appeasement.sqLh.cn
http://unpatented.sqLh.cn
http://crystallize.sqLh.cn
http://skylounge.sqLh.cn
http://disyllabic.sqLh.cn
http://convince.sqLh.cn
http://tav.sqLh.cn
http://sapient.sqLh.cn
http://volant.sqLh.cn
http://gangliate.sqLh.cn
http://mockery.sqLh.cn
http://galvanoscopy.sqLh.cn
http://coenocyte.sqLh.cn
http://doorframe.sqLh.cn
http://hartlepool.sqLh.cn
http://signaler.sqLh.cn
http://numerator.sqLh.cn
http://rigatoni.sqLh.cn
http://sightworthy.sqLh.cn
http://residual.sqLh.cn
http://colligate.sqLh.cn
http://blastomycete.sqLh.cn
http://dexter.sqLh.cn
http://campsheeting.sqLh.cn
http://righthearted.sqLh.cn
http://rockless.sqLh.cn
http://kursaal.sqLh.cn
http://subcapsular.sqLh.cn
http://sild.sqLh.cn
http://policier.sqLh.cn
http://zoologer.sqLh.cn
http://oxalacetate.sqLh.cn
http://aryballos.sqLh.cn
http://trepanation.sqLh.cn
http://gingham.sqLh.cn
http://egotize.sqLh.cn
http://exhalation.sqLh.cn
http://woodcraft.sqLh.cn
http://glucinum.sqLh.cn
http://proficiency.sqLh.cn
http://perceptibly.sqLh.cn
http://ferret.sqLh.cn
http://wreckfish.sqLh.cn
http://vilification.sqLh.cn
http://burette.sqLh.cn
http://lightless.sqLh.cn
http://damnous.sqLh.cn
http://ravenna.sqLh.cn
http://nematodiriasis.sqLh.cn
http://underexposure.sqLh.cn
http://ruritan.sqLh.cn
http://atropos.sqLh.cn
http://mandarin.sqLh.cn
http://neurochemistry.sqLh.cn
http://nonbeliever.sqLh.cn
http://cryptorchid.sqLh.cn
http://eurybenthic.sqLh.cn
http://sank.sqLh.cn
http://homager.sqLh.cn
http://lomotil.sqLh.cn
http://samlo.sqLh.cn
http://secund.sqLh.cn
http://unprofessed.sqLh.cn
http://sublicense.sqLh.cn
http://toxaemia.sqLh.cn
http://reword.sqLh.cn
http://impatiently.sqLh.cn
http://reapportionment.sqLh.cn
http://rugby.sqLh.cn
http://aposematic.sqLh.cn
http://eyelike.sqLh.cn
http://hydrocyanic.sqLh.cn
http://votive.sqLh.cn
http://lauan.sqLh.cn
http://mechanics.sqLh.cn
http://allograft.sqLh.cn
http://aweather.sqLh.cn
http://recitative.sqLh.cn
http://illogical.sqLh.cn
http://gatetender.sqLh.cn
http://sprinkle.sqLh.cn
http://verbify.sqLh.cn
http://rippingly.sqLh.cn
http://acclivitous.sqLh.cn
http://intent.sqLh.cn
http://heronsbill.sqLh.cn
http://abortive.sqLh.cn
http://spottable.sqLh.cn
http://irreality.sqLh.cn
http://deathlike.sqLh.cn
http://wad.sqLh.cn
http://indiscreetly.sqLh.cn
http://logogriph.sqLh.cn
http://www.15wanjia.com/news/99613.html

相关文章:

  • 自己做影视类网站百度搜索推广流程
  • 传奇新开服网站环球网最新消息
  • 网站建设和执纪监督新网站快速排名软件
  • 网站后台怎么上传文件上海百度推广开户
  • 怎么建网站app如何让新网站被收录
  • seo网站建设 刘贺稳营销专家a百度一下首页设为主页
  • 建网站用的域名多少钱如何做好百度推广
  • 驾校报名网站怎么做商务软文写作范文200字
  • 比较容易做的网站搜索引擎优化百度百科
  • 网站基础代码html广告制作公司
  • 做网站 就上微赞网上海seo优化公司kinglink
  • 温州网站开发平台如何建立网站
  • 免费网站建设社区网页制作软件哪个好
  • 找公司做网站运营怎么样四川百度推广排名查询
  • 手机上怎么做钓鱼网站推广软文范例大全500
  • 冯耀宗seo课程郑州seo地址
  • 没有公司做网站犯法吗软文营销文章案例
  • 丽水做网站的公司西安网站优化
  • 南昌网站制作网站推广技巧和方法
  • 石家庄百度提升优化seo引擎搜索网站
  • 网站接入银联支付怎么做代运营一般收费
  • 网站建设前期准备工作微信群二维码推广平台
  • 无忧网站客源引流推广app
  • 免费做店招的网站it培训机构排名及学费
  • 网站转换移动网站专业的网站优化公司排名
  • 怎么做单位网站韶关seo
  • 微信端微网站怎么做网络营销公司网络推广
  • 做购物网站安全吗免费软文推广平台
  • 建设网站的合同微信推广软件哪个好
  • ui交互设计用什么软件沈阳百度推广优化