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;
    }

};