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的更多相关文章

  1. [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 ...

  2. 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 ...

  3. leetcode面试准备:Sliding Window Maximum

    leetcode面试准备:Sliding Window Maximum 1 题目 Given an array nums, there is a sliding window of size k wh ...

  4. 【LeetCode】239. Sliding Window Maximum

    Sliding Window Maximum   Given an array nums, there is a sliding window of size k which is moving fr ...

  5. 【刷题-LeetCode】239. Sliding Window Maximum

    Sliding Window Maximum Given an array nums, there is a sliding window of size k which is moving from ...

  6. [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 ...

  7. [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 ...

  8. 【LeetCode】239. Sliding Window Maximum 解题报告(Python&C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 单调递减队列 MultiSet 日期 题目地址:ht ...

  9. LeetCode题解-----Sliding Window Maximum

    题目描述: Given an array nums, there is a sliding window of size k which is moving from the very left of ...

随机推荐

  1. bootstrap-简单实用的垂直手风琴滑动菜单列表特效

    前端: <html lang="zh"> <head> <meta charset="UTF-8"> <meta ht ...

  2. .Net调用R语言

    ///加载自己写的R语言算法库 public List<double> GetZTFB(double[] data) { List<double> par = new List ...

  3. HttpWebRequest的GetResponse或GetRequestStream偶尔超时 + 总结各种超时死掉的可能和相应的解决办法

    [问题] 用C#模拟网页登陆,其中去请求几个页面,会发起对应的http的请求request,其中keepAlive设置为true,提交请求后,然后会有对应的response: resp = (Http ...

  4. LNMP环境搭建完整步骤

    零.resource   http://pan.baidu.com/s/1o83r3S2 一.centos 6.4.VirtualBox 5.0.14 二.nginx 1.9.9 安装 [root@p ...

  5. 从零开始学 Java - CentOS 安装 JDK

    我来总结一下吧 昨天我写了一篇从零开始学 Java - 我放弃了 .NET ?,在园子里突然引起了强烈的讨论,有期待我能持续更新的.有鼓励支持的.有相同经历的.也有同一个学校的师兄弟(我们相认了).当 ...

  6. B-Tree索引在sqlserver和mysql中的应用

    在谈论数据库性能优化的时候,通常都会提到“索引”,但很多人其实并没有真正理解索引,也没有搞清楚索引为什么就能加快检索速度,以至于在实践中并不能很好的应用索引.事实上,索引是一种廉价而且十分有效的优化手 ...

  7. html5实现银联海购商品分类列表

    银联海购官网请点击 http://haigou.unionpay.com/ 1.实现效果预览展示如下: 2.源码如下 <!DOCTYPE html> <html> <he ...

  8. js 循环li添加点击事件 (闭包的应用)

    var aLi = document.querySelectorAll('.article-tab li');  for (var i = 0; i <= aLi.length; i++) {  ...

  9. View and Data API Tips: how to make viewer full screen

    By Daniel Du If you have not heard of View and Data API, here is the idea, the View & Data API e ...

  10. eclipse启动的时候报错 出现Java was started but returned exit code=13

    eclipse启动的时候出现 这是你的jdk环境与你eclipse版本不匹配, 如果你的eclipse是32位的 jdk也得是32位的   重新安装一个比配的jdk就好了 如果你的jdk是解压版的   ...