当前位置: 首页 > 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网络结构比较
  • [LeetCode] Wiggle Sort
  • 2017-08-04 前端日报
  • eclipse(luna)创建web工程
  • java2019面试题北京
  • Java编程基础24——递归练习
  • Node + FFmpeg 实现Canvas动画导出视频
  • React 快速上手 - 07 前端路由 react-router
  • Spring框架之我见(三)——IOC、AOP
  • Traffic-Sign Detection and Classification in the Wild 论文笔记
  • TypeScript实现数据结构(一)栈,队列,链表
  • ViewService——一种保证客户端与服务端同步的方法
  • v-if和v-for连用出现的问题
  • vue-cli3搭建项目
  • 简单实现一个textarea自适应高度
  • 前端相关框架总和
  • 系统认识JavaScript正则表达式
  • 小程序button引导用户授权
  • 移动端唤起键盘时取消position:fixed定位
  • 怎么把视频里的音乐提取出来
  • ​软考-高级-信息系统项目管理师教程 第四版【第23章-组织通用管理-思维导图】​
  • #if 1...#endif
  • (+4)2.2UML建模图
  • (17)Hive ——MR任务的map与reduce个数由什么决定?
  • (ctrl.obj) : error LNK2038: 检测到“RuntimeLibrary”的不匹配项: 值“MDd_DynamicDebug”不匹配值“
  • (JS基础)String 类型
  • (vue)el-checkbox 实现展示区分 label 和 value(展示值与选中获取值需不同)
  • (差分)胡桃爱原石
  • (第9篇)大数据的的超级应用——数据挖掘-推荐系统
  • (分类)KNN算法- 参数调优
  • (附源码)ssm高校社团管理系统 毕业设计 234162
  • (十五)使用Nexus创建Maven私服
  • (一)Dubbo快速入门、介绍、使用
  • (原+转)Ubuntu16.04软件中心闪退及wifi消失
  • (转)memcache、redis缓存
  • .Net mvc总结
  • .NET/C# 利用 Walterlv.WeakEvents 高性能地中转一个自定义的弱事件(可让任意 CLR 事件成为弱事件)
  • .NET/C# 推荐一个我设计的缓存类型(适合缓存反射等耗性能的操作,附用法)
  • .net企业级架构实战之7——Spring.net整合Asp.net mvc
  • .NET中两种OCR方式对比
  • ::前边啥也没有