June 30, 2017
It is a great idea to learn C# programming language using Leetcode. Julia searches Google using keyword "C# SortedSet Leetcode", and then she finds the following algorithms to work on:
Leetcode 23 Merge K Sorted Lists
Leetcode 57 Insert Intervals
Leetcode 128 Longest Consecutive Sequence
Leetcode 220 Remove Duplicate III
Leetcode 239 Sliding Window Maximum
Leetcode 295 Median of Streams
It takes a few hours to study those algorithm written using C# SortedSet.
From January 2015, she started to practice leetcode questions; she trains herself to stay focus, develops "muscle" memory when she practices those questions one by one. 2015年初, Julia开始参与做Leetcode, 开通自己第一个博客. 刷Leet code的题目, 她看了很多的代码, 每个人那学一点, 也开通Github, 发表自己的代码, 尝试写自己的一些体会. She learns from her favorite sports – tennis, 10,000 serves practice builds up good memory for a great serve. Just keep going. Hard work beats talent when talent fails to work hard.
Showing posts with label Leetcode 239. Show all posts
Showing posts with label Leetcode 239. Show all posts
Friday, June 30, 2017
Thursday, April 21, 2016
Window minimum
April 21, 2016
Try to get some algorithm reading and then find a similar algorithm in HackerRank, and get some workout on coding.
It is called Window minimum.
Problem statement from the geeksforgeeks website
Try to get some algorithm reading and then find a similar algorithm in HackerRank, and get some workout on coding.
It is called Window minimum.
Problem statement from the geeksforgeeks website
Given an array of size N, a window of size W slides over it by increment of slide S. If the window reaches to the end, we should stop there. Find a formula in form of N, S, W so that we can find the number of valid windows. Write a program to find minimum in every window and print it. Optimize it.
e.g. {1,2,3,4,5}, W=2, S=1
first window: {1,2} min=1
second window(increment by S=1): {2,3}, min=2
first window: {1,2} min=1
second window(increment by S=1): {2,3}, min=2
…
last window: {4,5}, min=4
The array might not be sorted. I have taken sorted array for simplicity.
or in Chinese,
滑动窗口求最小:
给了一个ArrayList:4, 2, 12, 11, -5,窗口size为2,返回的ArrayList为:2, 2, 11, -5。这里窗口size是一个参数。
Leetcode 239: Sliding window maximum
Read one of solutions using Java:
Java solution
Java solution
Saturday, August 8, 2015
Leetcode 239: sliding window maximum
August 7, 2015
Julia spent 20-30 minutes to think about solution first, and read the following blogs, write some code as well. She practised three time using C# and here are the details.
Here are 3 practice Julia did using LinkedList, List and SortedList.
C# practices
Here are 3 practice Julia did using LinkedList, List and SortedList.
1. C# LinkedList
C# code implementation
2. C# List<int>
3. C# SortedList
More detail
Double ended queue is implemented using C# LinkedList class, here is Julia's C# practice code: C# code implementationJulia chose to study the blog on Leetcode: sliding window maximum
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>, Julia experienced different style on removing head element if out of sliding window.
Julia chose to read the blog on geekforgeek.com called "maximum of all subarrays of size k".
Method A: naive solution, time complexity O(nw)
C# practice code is here.
C# practice code is here.
Method B: using Self-Balancing Tree (Time complexity: O(nk), need to write c# code)
C# practice code is here.
C# practice code is here.
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/Buffer or 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).
C# practice code
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
Subscribe to:
Posts (Atom)