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

LeetCode -- Sliding Window Maximum

题目描述:


Given an array nums, there is a sliding window of size k which is moving from the very left of the array to the very right. You can only see the k numbers in the window. Each time the sliding window moves right by one position.


For example,
Given nums = [1,3,-1,-3,5,3,6,7], and k = 3.


Window position                Max
---------------               -----
[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
Therefore, return the max sliding window as [3,3,5,5,6,7].


Note: 
You may assume k is always valid, ie: 1 ≤ k ≤ input array's size for non-empty array.


就是在一个数组中,存在一个长度为k的窗口,每次把这个窗口向后移动1个单元,把最大值取出放入list,直到窗口右端到达数组末尾为止,把最大值的数组list返回。


思路:
1.本题主要是需要使用1个list来表达窗口,在首位移除,末端添加。
2.为了优化性能,可以维护1个maxIndex变量来减小最大值的计算次数。当窗口发生移动时,假设index表示窗口的末尾位置,则:
a.如果首位移除的是maxIndex,则重新计算最大值并更新maxIndex
b.如果首位移除的不是maxIndex,则只需比较nums[maxIndex]与nums[index]即可,更新maxIndex




实现代码:




public class Solution {
    public int[] MaxSlidingWindow(int[] nums, int k) {
        if(nums.Length == 0){
    		return new int[0];
    	}
    	if(k > nums.Length - 1){
    		return new int[]{nums.Max()};
    	}
    	 
    	 var result = new List<int>();
    	 var window = new List<int>();
    	 var maxIndex = 0;
    	 for(var i = 0;i < k; i++){
    	 	window.Add(nums[i]);
    		if(nums[maxIndex] < nums[i]){
    			maxIndex = i;
    		}
    	 }
    	 result.Add(nums[maxIndex]);
    	 
    	 var right = k;
    	 while(right < nums.Length){
    	 	window.RemoveAt(0);
    		window.Add(nums[right]);
    		
    		if(right - k == maxIndex){
    			maxIndex = CalcMaxIndex(right-k+1, k, nums);
    		}else{
    			if(nums[right] > nums[maxIndex]){
    				maxIndex = right;
    			}
    		}
    		result.Add(nums[maxIndex]);
    		right ++;
    	 }
    	 
    	 return result.ToArray();
}


private int CalcMaxIndex(int left, int k, int[] nums){
	var maxIndex = left ;
	for(var j = left ;j < left + k; j++){
		if(nums[j] > nums[maxIndex]){
			maxIndex = j;
		}
	}
	return maxIndex;
}
    
}


相关文章:

  • LeetCode -- Binary Tree Paths
  • 参加OPUG第二次活动的有关BPM主题聚会记
  • LeetCode -- Bulls and Cows
  • 如何修改Windows CE的平台类型
  • LeetCode -- Game of Life
  • 黄舒骏,改变1995
  • LeetCode -- H-Index II
  • vim插件 ctags 和 taglist 的安装和使用
  • LeetCode -- Longest Increasing Subsequence
  • LeetCode -- Serialize and Deserialize Binary Tree
  • Ubuntu中用apt安装和卸载软件
  • LeetCode -- Single Number III
  • Linux 常用C函数(内存及字符串操作篇2)
  • LeetCode -- Subsets II
  • WCDMA与CDMA2000网络结构比较
  • SegmentFault for Android 3.0 发布
  • 【108天】Java——《Head First Java》笔记(第1-4章)
  • 【跃迁之路】【669天】程序员高效学习方法论探索系列(实验阶段426-2018.12.13)...
  • 002-读书笔记-JavaScript高级程序设计 在HTML中使用JavaScript
  • 2019.2.20 c++ 知识梳理
  • ES6系列(二)变量的解构赋值
  • ESLint简单操作
  • express + mock 让前后台并行开发
  • javascript 总结(常用工具类的封装)
  • Java比较器对数组,集合排序
  • Yeoman_Bower_Grunt
  • 程序员最讨厌的9句话,你可有补充?
  • 多线程事务回滚
  • 讲清楚之javascript作用域
  • 前端学习笔记之观察者模式
  • 微信小程序实战练习(仿五洲到家微信版)
  • 消息队列系列二(IOT中消息队列的应用)
  • 东超科技获得千万级Pre-A轮融资,投资方为中科创星 ...
  • ​决定德拉瓦州地区版图的关键历史事件
  • $().each和$.each的区别
  • (03)光刻——半导体电路的绘制
  • (42)STM32——LCD显示屏实验笔记
  • (C语言)逆序输出字符串
  • (HAL库版)freeRTOS移植STMF103
  • (ZT)薛涌:谈贫说富
  • (大众金融)SQL server面试题(1)-总销售量最少的3个型号的车及其总销售量
  • (第9篇)大数据的的超级应用——数据挖掘-推荐系统
  • (附源码)基于SSM多源异构数据关联技术构建智能校园-计算机毕设 64366
  • (教学思路 C#之类三)方法参数类型(ref、out、parmas)
  • (全部习题答案)研究生英语读写教程基础级教师用书PDF|| 研究生英语读写教程提高级教师用书PDF
  • (算法设计与分析)第一章算法概述-习题
  • .NET NPOI导出Excel详解
  • .NET 中的轻量级线程安全
  • .net 重复调用webservice_Java RMI 远程调用详解,优劣势说明
  • .net网站发布-允许更新此预编译站点
  • .vimrc php,修改home目录下的.vimrc文件,vim配置php高亮显示
  • @vue/cli 3.x+引入jQuery
  • [ 手记 ] 关于tomcat开机启动设置问题
  • []我的函数库
  • [2016.7 Day.4] T1 游戏 [正解:二分图 偏解:奇葩贪心+模拟?(不知如何称呼不过居然比std还快)]