Showing posts with label Heap. Show all posts
Showing posts with label Heap. Show all posts

Tuesday, December 8, 2015

LeetCode [313] Super Ugly Number

Ref
[1] https://leetcode.com/problems/super-ugly-number/

Monday, October 19, 2015

LeetCode [295] Find Median from Data Stream


295. Find Median from Data Stream
Hard

Median is the middle value in an ordered integer list. If the size of the list is even, there is no middle value. So the median is the mean of the two middle value.

For example,

[2,3,4], the median is 3

[2,3], the median is (2 + 3) / 2 = 2.5

Design a data structure that supports the following two operations:

  • void addNum(int num) - Add a integer number from the data stream to the data structure.
  • double findMedian() - Return the median of all elements so far.

 

Example:

addNum(1)
addNum(2)
findMedian() -> 1.5
addNum(3) 
findMedian() -> 2

 

Follow up:

  1. If all integer numbers from the stream are between 0 and 100, how would you optimize it?
  2. If 99% of all integer numbers from the stream are between 0 and 100, how would you optimize it?
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
class MedianFinder {
    priority_queue<int> q1;
    priority_queue<int, vector<int>, greater<int>> q2;
public:

    // Adds a number into the data structure.
    void addNum(int num) {
        if(q1.size()>q2.size()){
            if(num>=q1.top()) q2.push(num);               
            else{
                q2.push(q1.top());
                q1.pop();
                q1.push(num);
            }
        }else{
            if(!q2.empty()&&num>=q2.top()){
                q1.push(q2.top());
                q2.pop();
                q2.push(num);
            }else q1.push(num);
        }
    }

    // Returns the median of current data stream
    double findMedian() {
        if(q1.size()>q2.size()){
            return q1.top();
        }else{
            return (q1.top()+q2.top())/2.0;
        }
    }
};

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
class MedianFinder {
    PriorityQueue<Integer> pq1 = new PriorityQueue<>((a,b)-> b-a);
    PriorityQueue<Integer> pq2 = new PriorityQueue<Integer>();

    /** initialize your data structure here. */
    public MedianFinder() {
        
    }
    
    public void addNum(int num) {
        if(!pq2.isEmpty() && num>=pq2.peek()){
            pq2.add(num);
        }else{
            pq1.add(num);
        }

        if(pq1.size()-1>pq2.size()) pq2.add(pq1.poll());
        if(pq2.size()-1>pq1.size()) pq1.add(pq2.poll());
    }
    
    public double findMedian() {
        if(pq1.size()==pq2.size()){
            return (double)(pq1.peek()+pq2.peek())/2.0;
        }else if(pq1.size()>pq2.size()) return pq1.peek();
        else return pq2.peek();
    }
}

/**
 * Your MedianFinder object will be instantiated and called as such:
 * MedianFinder obj = new MedianFinder();
 * obj.addNum(num);
 * double param_2 = obj.findMedian();
 */

Friday, September 18, 2015

MJ [37] Cross River

Question:
A bug is trying to cross the river start from position 0 to position X. Every time bug can jump no more than the D steps (1 to D steps). Leaves will fall from the tree to the river based on schedule A. A[0] = 1 means a leaf will fall on position 1 at time 0. Need to find the earliest time that the bug can jump from position 0 to X using leaves; If there is no answer, return -1;

Example:
A = [1, 3, 1, 4, 2, 5]
X = 7 
D = 3
Answer: 3
Explanation: At time 3, there will be leaves on position 1,3, and 4; bug can jump 1 step, 3 step, and then 3 steps to cross the river;

Ref
[1] http://www.mitbbs.com/article_t/JobHunting/33055941.html
[2] http://www.mitbbs.com/article_t/JobHunting/33058417.html

Tuesday, August 18, 2015

LeetCode [264] Ugly Number II

Ref
[1] https://leetcode.com/problems/ugly-number-ii/
OJ

Friday, July 17, 2015

LeetCode [239] Sliding Window Maximum

239. Sliding Window Maximum
Hard

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. Return the max sliding window.

Follow up:
Could you solve it in linear time?

Example:

Input: nums = [1,3,-1,-3,5,3,6,7], and k = 3
Output: [3,3,5,5,6,7] 
Explanation: 

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

 

Constraints:

  • 1 <= nums.length <= 10^5
  • -10^4 <= nums[i] <= 10^4
  • 1 <= k <= nums.length
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
class Solution {
public:
    vector<int> maxSlidingWindow(vector<int>& nums, int k) {
        vector<int> ret;
        deque<int> dq;//index. the corresponding numbers are decreasing from front to back
        int n = nums.size();
        for(int i=0; i<n; ++i)
        {
            while(!dq.empty() && dq.front()<i-k+1) dq.pop_front();
            while(!dq.empty() && nums[dq.back()]<=nums[i]) dq.pop_back();
            dq.push_back(i);
            if(i>=k-1)
                ret.push_back(nums[dq.front()]);
        }
        return ret;
    }
};

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
class Solution {
    public int[] maxSlidingWindow(int[] nums, int k) {
        List<Integer> list = new ArrayList<>();
        int n = nums.length;
        Deque<Integer> dq = new ArrayDeque<>();
        for(int i=0; i<n; ++i){
            if(!dq.isEmpty() && dq.getFirst()<i+1-k) dq.removeFirst();
            while(!dq.isEmpty() && nums[dq.peekLast()]<=nums[i]){
                dq.pollLast();
            }
            dq.addLast(i);
            if(i>=k-1) list.add(nums[dq.peekFirst()]);
        }
        return list.stream().mapToInt(i->i).toArray();
    }
}