Saturday, November 12, 2016

Meeting Room II -- LeetCode

[Question]
Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2,e2],...] (si < ei), find the minimum number of conference rooms required.
For example, Given [[0, 30],[5, 10],[15, 20]], return 2.
[Analysis]
This is another interval related problem. First, we can sort all the starts (s1, s2, ...) and ends (e1, e2, ...) into two series. Then, for each ends 'e', count how many starts are before 'e' -- that is how many rooms we need before time 'e'. Time complexity is O(N Log N), space complexity is O(N).

The similar problems: "Non-overlapping Intervals", "Minimum Number of Arrows to Burst Balloons".

[Solution]
public class Solution {
    public int minMeetingRooms(vector<Interval> intervals) {
        vector<int> starts(intervals.size(), 0);
        vector<int> ends(intervals.size(), 0);
        for (int i=0; i<intervals.size(); i++) {
            starts[i] = intervals[i].start;
            ends[i] = intervals[i].end;
        }
        sort (starts.begin(), starts.end());
        sort(ends.begin(), ends.end());
        int room=0; int j=0;
        int res =0;
        for (auto e: ends) {
            while (starts[j]<e) {
                j++; room++;
                res = max(res, room);
            }
            room--;
         }
         return res;
    }
}

//
// Using Hash Table
//
class Solution {
public:
    int minMeetingRooms(vector<Interval>& intervals) {
        map<int, int> m;
        for (auto a : intervals) {
            m[a.start]++;
            m[a.end]--;
        }
        int rooms = 0, res = 0;
        for (auto it : m) {
            res = max(res, rooms += it.second);
        }
        return res;
    }
};

Non-overlapping Intervals -- LeetCode 435

[Question]
Given a collection of intervals, find the minimum number of intervals you need to remove to make the rest of the intervals non-overlapping.
Note:
  1. You may assume the interval's end point is always bigger than its start point.
  2. Intervals like [1,2] and [2,3] have borders "touching" but they don't overlap each other.
Example 1:
Input: [ [1,2], [2,3], [3,4], [1,3] ]

Output: 1

Explanation: [1,3] can be removed and the rest of intervals are non-overlapping.
Example 2:
Input: [ [1,2], [1,2], [1,2] ]

Output: 2

Explanation: You need to remove two [1,2] to make the rest of intervals non-overlapping.
Example 3:
Input: [ [1,2], [2,3] ]

Output: 0

Explanation: You don't need to remove any of the intervals since they're already non-overlapping.
[Analysis]
This problem is equivalent to the problem "Minimum Number of Arrows to Burst Balloons". Instead of counting the overlapping intervals, this problem needs to calculate the number of redundant intervals.

The basic idea is to use a greedy approach. 1) sort the intervals by the ends; 2) suppose we have a few selected non-overlapping intervals, use the end of  the last interval X as a vertical scan line from left to right: any intervals with start that is smaller than the scan line will be overlapped with X, therefore, place the first non-overlapping interval X1 into selected list and repeat 2).

[Solution]
class Solution {
public:
    int eraseOverlapIntervals(vector<Interval>& intervals) {
        auto comp = [](Interval a, Interval b) { return a.end==b.end && a.start<b.start|| a.end<b.end;};
        sort(intervals.begin(), intervals.end(), comp);
     
        int count=0, line = INT_MIN;
        for(auto& e: intervals) {
            if (e.start< line ) continue;
            count++;
            line = e.end;
        }
        return intervals.size()-count;
    }
};

Friday, November 11, 2016

Minimum Number of Arrows to Burst Balloons -- LeetCode

[Question]
There are a number of spherical balloons spread in two-dimensional space. For each balloon, provided input is the start and end coordinates of the horizontal diameter. Since it's horizontal, y-coordinates don't matter and hence the x-coordinates of start and end of the diameter suffice. Start is always smaller than end. There will be at most 104 balloons.
An arrow can be shot up exactly vertically from different points along the x-axis. A balloon with xstart and xend bursts by an arrow shot at x if xstart ≤ x ≤ xend. There is no limit to the number of arrows that can be shot. An arrow once shot keeps travelling up infinitely. The problem is to find the minimum number of arrows that must be shot to burst all balloons.
Example:
Input:
[[10,16], [2,8], [1,6], [7,12]]

Output:
2

Explanation:
One way is to shoot one arrow for example at x = 6 (bursting the balloons [2,8] and [1,6]) and another arrow at x = 11 (bursting the other two balloons).
[Analysis]
First, sort the balloons by the end of their end of the diameter suffice.  Then, using those ends as the positions to shot the arrow and all those balloons whose 'start'  <=  'the current arrow position', will burst. The time complexity is O(N LogN) due to sorting, the space complexity is O(1).

[Solution]
class Solution {
public:
    int findMinArrowShots(vector<pair<int, int>>& points) {
        if (points.size()<2) return points.size();
       
        #define PT pair<int,int>        
        auto comp = [](PT& a, PT& b) { return (a.second==b.second)?a.first<b.first: a.second<b.second; };
        sort(points.begin(), points.end(), comp);
       
        int count=0, arrow=INT_MIN;
        for (auto& p:points) {
            if (p.first<=arrow) continue;
            count++, arrow=p.second;
        }
        return count;
    }
};

Friday, November 4, 2016

Sort Characters By Frequency -- LeetCode

[Question]
Given a string, sort it in decreasing order based on the frequency of characters.
Example 1:
Input:
"tree"

Output:
"eert"

Explanation:
'e' appears twice while 'r' and 't' both appear once.
So 'e' must appear before both 'r' and 't'. Therefore "eetr" is also a valid answer.
Example 2:
Input:
"cccaaa"

Output:
"cccaaa"

Explanation:
Both 'c' and 'a' appear three times, so "aaaccc" is also a valid answer.
Note that "cacaca" is incorrect, as the same characters must be together.
Example 3:
Input:
"Aabb"

Output:
"bbAa"

Explanation:
"bbaA" is also a valid answer, but "Aabb" is incorrect.
Note that 'A' and 'a' are treated as two different characters.
[Analysis]
First, use a hash table to count the frequency of each letter. Second, use a heap to sort each element in the hash table, based on the frequency. The time complexity is O(N). The space complexity can be constant, as there are fixed number of characters -- the size of heap is O(1).

[Solution]
class Solution {
public:
    string frequencySort(string s) {
        unordered_map<char, int> counter;
        auto comp = [](pair<char, int>& a, pair<char, int>& b) { return a.second <b.second; };
        priority_queue< pair<char,int>, vector<pair<char,int>>, decltype(comp) > sorter(comp);
       
        for(auto& c:s)
            counter[c]++;
           
        for(auto& p:counter)
            sorter.push(p);
       
        string res="";
        while (!sorter.empty()) {
            auto elem = sorter.top();
            sorter.pop();
            for (int i=0; i<elem.second; i++)
                res+=elem.first;
        }
       
        return res;
    }

};

Friday, October 28, 2016

Maximum XOR of Two Numbers in an Array -- LeetCode

[Question]
Given a non-empty array of numbers, a0, a1, a2, … , an-1, where 0 ≤ ai < 231.
Find the maximum result of ai XOR aj, where 0 ≤ ij < n.
Could you do this in O(n) runtime?
Example:
Input: [3, 10, 5, 25, 2, 8]

Output: 28

Explanation: The maximum result is 5 ^ 25 = 28.

[Analysis]
The brute force approach is to XOR each two of those numbers and find the maximum result. The time complexity is O(N^2).

Consider the maximum result's MSB (Most Significant Bit), there are two numbers from input set that can generate (XOR) the '1' in the position. That means in the position of that bit, (1) there are at least two numbers differ from each other; (2) For the bits to the left of that bit, no two numbers differ from each other. Then consider Most Significant Two Bits, Three Bits, ... , the same logic can apply. Therefore, each bit in maximum result can be decided.

[Solution]
class Solution {
public:
    int findMaximumXOR(vector<int>& nums) {
        if (nums.size()<2) return 0;
     
        long mask = 0;
        long max = 0;
        for (int i=31; i>=0; i--) {
            unordered_set<long> pre_set;
            mask = mask | (1<<i);
            for (auto& n: nums) {
                pre_set.insert( n & mask );
            }
            // -- optional
            if (pre_set.size()==1) continue;
         
            long target = max | (1<<i);
            for (auto& x: pre_set) {
                   // -- if target^x=y then x^y=target
                if ( pre_set.count(x ^ target)> 0 ) {
                    max = target;
                    break;
                }
            }
        }
        return max;
    }

};

Sunday, October 16, 2016

Decode String -- LeetCode

[Question]
Given an encoded string, return it's decoded string.
The encoding rule is: k[encoded_string], where the encoded_string inside the square brackets is being repeated exactly k times. Note that kis guaranteed to be a positive integer.
You may assume that the input string is always valid; No extra white spaces, square brackets are well-formed, etc.
Furthermore, you may assume that the original data does not contain any digits and that digits are only for those repeat numbers, k. For example, there won't be input like 3a or 2[4].
Examples:
s = "3[a]2[bc]", return "aaabcbc".
s = "3[a2[c]]", return "accaccacc".
s = "2[abc]3[cd]ef", return "abcabccdcdcdef".

[Analysis]
Use stack and push every character into stack until encounter ']'. When ']' is encountered, the deepest encoded sub string, in the stack, can be decoded. Push the decoded sub string back to stack and continue the process until reaching the end of the string. 
The time complexity is O(N), N is the size of the input string. Note: it is better to have a stack of "string" instead of "char" for convenience.
[Solution]
class Solution {
public:
    string decodeString(string s) {
        stack<string> st;
     
        for (auto& c: s) {
            if (c!=']') {
                st.push(string(1, c));
                continue;
            }
            //c==']'
            string item;
            string cnt;
            while(!st.empty() && st.top()!="[" ) {
                item= st.top() + item;
                st.pop();
            }
         
            if (st.top()=="[") {
                st.pop();
                while (!st.empty() && isdigit(st.top()[0]) ) {
                    cnt=st.top()+cnt;
                    st.pop();
                }
            }

            string buf="";
            for(int i=0; i<stoi(cnt); i++) {
                buf+=item;
            }
            st.push(buf);
        }
     
        string rslt;
        while (!st.empty()) {
            rslt = st.top() + rslt;
            st.pop();
        }
        return rslt;
    }

};

Friday, September 2, 2016

Max Sum of Rectangle No Larger Than K -- LeetCode

[Question]
Given a non-empty 2D matrix matrix and an integer k, find the max sum of a rectangle in the matrix such that its sum is no larger than k.
Example:
Given matrix = [
  [1,  0, 1],
  [0, -2, 3]
]
k = 2
The answer is 2. Because the sum of rectangle [[0, 1], [-2, 3]] is 2 and 2 is the max number no larger than k (k = 2).
[Analysis]
Using brute force, every rectangle needs to be checked. For a matrix m x n, the time complexity is O(m^2 x n^2).
Consider the one-dimensional array, A[0], A[1] ... A[n-1], to get a max sum of a consecutive sub-array is no larger than K, we can construct the cumulative sum array: S[0] = A[0], S[1]=S[0]+A[1], ... S[i]=S[i-1]+A[i], ..S[n-1]=S[n-2]+A[n-1]. Sum of A's sub-array A[i]..A[j] = S[j]-S[i-1]. The problem becomes, for a given S[j], apply binary search on S[0],...S[j-1], to find S[j]-K. 
On two-dimensional matrix, we can construct this kind of one-dimensional array array between any two columns. Then we can find the needed rectangle in time complexity O( m x n^2 x log n).
[Solution]

class Solution {
public:
    int maxSumSubmatrix(vector<vector<int>>& matrix, int k) {
        int m=matrix.size(), n=matrix[0].size();
        int res=INT_MIN;
        for (int i=0; i<n; i++)  {
            vector<int> list(m, 0);
            for (int j=i; j<n; j++) {
                for(int k=0; k<m; k++) {
                    list[k]+=matrix[k][j];
                }
                set<int> sums;
                sums.insert(0);
                int curSum = 0;
                for(auto a: list) {
                    curSum+=a;
                    auto it= sums.lower_bound( curSum-k);
                    if (it!=sums.end()) res = max(res, curSum-*it);
                    sums.insert(curSum);
                }
            }
        }
        return res;
    }
};