Leetcode: sliding window maximum
August 7, 2015
周日玩这个算法, 看到Javascript Array模拟Deque, 非常喜欢, 想用C#数组也模拟; 看有什么新的经历. 试了四五种方法, 花时间研究C# SortedList class. Try to get some workout on the C# programming practice.
我的体会是代码写得太少, 要多读不同的代码, 多调试, 开阔对一个问题思考的思路和深度, 准确度, 实现能力.
1. Blog to read:
C# code implementation (double ended queue is implemented using C# LinkedList class):
https://github.com/jianminchen/slidingWindowMaximum/blob/master/slidingWindowMaximum1.cs
2. Blog to read
C# code implementation (C# does not have deque class, so using C# List<int>,
convert Python code implementation to C#, time limit exceeded)
Good workout on C# List<int>, and also, experience different style on removing head element if out of sliding window.
https://github.com/jianminchen/slidingWindowMaximum/blob/master/slidingWindowMaximu2.cs
3. Blog to read
Method A: naive solution, time complexity O(nw)
Method B: using Self-Balancing Tree (Time complexity: O(nk), need to write c# code)
4. blog:
http://n00tc0d3r.blogspot.ca/2013/04/sliding-window-maximum.html
Good comment about Deque:
We can use a Deque which allow insertions/deletions on both ends. For a Deque implemented by Circular Array/Bufferor Double Linked List, the basic insert/delete operations run in constant time.
discussion of using heap:
The first thought might be heap.
By maintaining a heap for all numbers in the window can give us a O(nlogw)-time solution, where
- building up a heap for initial window takes time O(wlogw)
- when window moves to the next number, each insertion and deletion take time O(logw) and there are n-w moves in total.
- after updating the heap, findMax only takes time O(1) since we know the top of heap is the largest.
So, if w << n, the performance of this solution is good, close to O(n); but if w is not that small, say w = n/3 or n/4, the running time goes up to O(nlogn).
5. blog:
https://github.com/haoel/leetcode/commit/74d83796aa48cc55d7995b1f9e61db40759204bb
using C++ multiset in the above solution, so try to convert it to C# class using SortedList
https://github.com/jianminchen/slidingWindowMaximum/blob/master/slidingWindowMaximu5.cs
6. blog:
http://www.mamicode.com/info-detail-927510.html
convert Java Script code to C#; I spent over 12 months to try to be expert on Java Script, so much fun to read the Java Script code again, and enjoyed the blog about the analysis.
Others:
https://github.com/jianminchen/slidingWindowMaximum/blob/master/slidingWindowMaximu5.cs
Leetcode: sliding window maximum的更多相关文章
- [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 a ...
- 239. [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 a ...
- leetcode面试准备:Sliding Window Maximum
leetcode面试准备:Sliding Window Maximum 1 题目 Given an array nums, there is a sliding window of size k wh ...
- 【LeetCode】239. Sliding Window Maximum
Sliding Window Maximum Given an array nums, there is a sliding window of size k which is moving fr ...
- 【刷题-LeetCode】239. Sliding Window Maximum
Sliding Window Maximum Given an array nums, there is a sliding window of size k which is moving from ...
- [LeetCode] Sliding Window Median 滑动窗口中位数
Median is the middle value in an ordered integer list. If the size of the list is even, there is no ...
- [LeetCode] 239. Sliding Window Maximum 滑动窗口最大值
Given an array nums, there is a sliding window of size k which is moving from the very left of the a ...
- 【LeetCode】239. Sliding Window Maximum 解题报告(Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 单调递减队列 MultiSet 日期 题目地址:ht ...
- LeetCode题解-----Sliding Window Maximum
题目描述: Given an array nums, there is a sliding window of size k which is moving from the very left of ...
随机推荐
- C#写爬虫,版本V2.1
这次是对2.0的小修补,2.0交互几乎没有,这次添加了进度条,和文本框,同时由于取得的链接主要会出现错误是:webResponse错误. 针对这种情况,设置了 try { webResponse = ...
- 为什么document.firstChild找到的不是html节点
DOM是针对HTML4.01开发的,我们现在是XHTML1.0. 所以要想使用核心DOM中的属性和方法,必须去掉DTD类型定义. <!DOCTYPE html PUBLIC "-//W ...
- org.hibernate.exception.SQLGrammarException: could not execute query
SSH项目中出现了 org.hibernate.exception.SQLGrammarException: could not execute query 错误,仔细检查后发现,是把createQu ...
- Zookeeper Api(java)入门与应用(转)
如何使用 Zookeeper 作为一个分布式的服务框架,主要用来解决分布式集群中应用系统的一致性问题,它能提供基于类似于文件系统的目录节点树方式的数据存储,但是 Zookeeper 并不是用来专门存储 ...
- jQuery ClockPicker 圆形时钟
ClockPicker.js是一款时钟插件,其实还可以改进,里面的分可以改成短横线. 在线实例 实例预览 jQuery ClockPicker 圆形时钟 使用方法 <div class=&qu ...
- Promise和$.Deferred总结
语法对比: Promise .then(f).catch(f)是.then(f,f)的语法糖 .all([A,B,C])等最慢的 .race([A,B,C])最快的 $.Deferred .d ...
- qq空间返回顶部代码
点击这里体验效果 以下是源代码: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "h ...
- js 模块化的一些理解和es6模块化学习
模块化 1 IIFE 2 commonjs 3 浏览器中js的模块化 4 简单理解模块加载器的原理 5 es6 之前在参加百度前端技术学院做的小题目的时候,自己写模块的时候 都是写成立即调用表达式( ...
- 原生JS实战:分享一个首页进度加载动画!
本文是苏福的原创文章,转载请注明出处:苏福CNblog:http://www.cnblogs.com/susufufu/p/5871134.html 该程序是本人的个人作品,写的不好,可以参考,但未经 ...
- Angular+Grunt+Bower+Karma+Protractor (Atom)
1. 配置bower 1.安装bower npm install -g bower 2.创建.bowerrc文件 { "directory": "src/bower&qu ...