LeetCode 229 + LintCode: Majority Number II


Related: LeetCode 169+ LintCode: Majority Number I
Lintcode: Majority Number II - neverlandly - 博客园
Given an array of integers, the majority number is the number that occurs more than 1/3 of the size of the array. Find it. 

Note There is only one majority number in the array Example For [1, 2, 1, 2, 1, 3, 3] return 1

X.
https://leetcode.com/problems/majority-element-ii/discuss/174987/topic
    思路:摩尔投票升级版,超过n/3的数最多只能有两个;
    先选出两个候选人A,B,遍历数组,如果投A(等于A),则A的票数++;如果投B,B的票数++;
    如果A,B都不投(即与A,B都不相等),那么检查此时是否AB中候选人的票数是否为0,如果为0,则成为新的候选人;
    如果A,B两个人的票数都不为0,那么A,B两个候选人的票数均--;
    遍历结束后选出两个候选人,但是这两个候选人是否满足>n/3,还需要再遍历一遍数组,找出两个候选人的具体票数
     */
    public List<Integer> majorityElement(int[] nums) {
        if (nums==null||nums.length==0){
            return null;
        }

        //初始化,定义两个候选人以及对应的票数
        int candidateA=nums[0];
        int candidateB=nums[0];
        int countA=0;
        int countB=0;

        // 遍历数组
        for (int num:nums){
            if (num==candidateA){ //投A
                countA++;
                continue;
            }

            if (num==candidateB){// 投B
                countB++;
                continue;
            }
            //此时A,B都不投,检查是否有票数为0情况,如果为0,则更新候选人
            if (countA==0){
                candidateA=num;
                countA++;
                continue;
            }

            if (countB==0){
                candidateB=num;
                countB++;
                continue;
            }

            //此时两个候选人的票数都大于1,且当前选名不投AB,那么A,B对应的票数都要--;
            countA--;
            countB--;
        }

        // 上一轮遍历找出了两个候选人,但是这两个候选人是否均满足票数大于N/3仍然没法确定,需要重新遍历,确定票数
        countA=0;
        countB=0;

        for (int num:nums){
            if (num==candidateA){
                countA++;
            }else if (num==candidateB){
                countB++;
            }
        }

        List<Integer> resultList=new ArrayList<>();

        if (countA>nums.length/3){
            resultList.add(candidateA);
        }

        if (countB>nums.length/3){
            resultList.add(candidateB);
        }

        return resultList;
    }
https://leetcode.com/problems/majority-element-ii/discuss/63520/Boyer-Moore-Majority-Vote-algorithm-and-my-elaboration
I found a great article (http://goo.gl/64Nams) that helps me to understand this fantastic algorithm!!
Please check it out!
The essential concepts is you keep a counter for the majority number X. If you find a number Y that is not X, the current counter should deduce 1. The reason is that if there is 5 X and 4 Y, there would be one (5-4) more X than Y. This could be explained as "4 X being paired out by 4 Y".
And since the requirement is finding the majority for more than ceiling of [n/3], the answer would be less than or equal to two numbers.
So we can modify the algorithm to maintain two counters for two majorities.
Followings are my sample Python code:
class Solution:
# @param {integer[]} nums
# @return {integer[]}
def majorityElement(self, nums):
    if not nums:
        return []
    count1, count2, candidate1, candidate2 = 0, 0, 0, 1
    for n in nums:
        if n == candidate1:
            count1 += 1
        elif n == candidate2:
            count2 += 1
        elif count1 == 0:
            candidate1, count1 = n, 1
        elif count2 == 0:
            candidate2, count2 = n, 1
        else:
            count1, count2 = count1 - 1, count2 - 1
    return [n for n in (candidate1, candidate2)
                    if nums.count(n) > len(nums) // 3]

https://discuss.leetcode.com/topic/29390/concise-java-solution-based-on-moore-s-voting-algorithm
public List<Integer> majorityElement(int[] nums) {
 if (nums == null || nums.length == 0)
  return new ArrayList<Integer>();
 List<Integer> result = new ArrayList<Integer>();
 int number1 = nums[0], number2 = nums[0], count1 = 0, count2 = 0, len = nums.length;
 for (int i = 0; i < len; i++) {
  if (nums[i] == number1)
   count1++;
  else if (nums[i] == number2)
   count2++;
  else if (count1 == 0) {
   number1 = nums[i];
   count1 = 1;
  } else if (count2 == 0) {
   number2 = nums[i];
   count2 = 1;
  } else {
   count1--;
   count2--;
  }
 }
 count1 = 0;
 count2 = 0;
 for (int i = 0; i < len; i++) {
  if (nums[i] == number1)
   count1++;
  else if (nums[i] == number2)
   count2++;
 }
 if (count1 > len / 3)
  result.add(number1);
 if (count2 > len / 3)
  result.add(number2);
 return result;
}
https://leetcode.com/problems/majority-element-ii/discuss/63500/JAVA-Easy-Version-To-Understand!!!!!!!!!!!!
public List<Integer> majorityElement(int[] nums) {
    ArrayList<Integer> res = new ArrayList<Integer>();
        if (nums.length==0) return res;
        
        int count[] = new int[2];        
        int x[] = new int[2];       
   
        x[0] = 0; x[1] = 1;        
        for (int i = 0; i < nums.length; i++) {
         if (x[0] == nums[i])
          count[0]++;
         else if (x[1] == nums[i])
          count[1]++;
         else if (count[0] == 0) {
          x[0] = nums[i];
          count[0] = 1;
         } else if (count[1] == 0) {
          x[1] = nums[i];
          count[1] = 1;
         } else {
          count[0]--;
          count[1]--;          
         }
        }
        
    Arrays.fill(count, 0);
    for (int i : nums) {// Count again for x1, x2
     if (i == x[0]) count[0]++;
     else if (i == x[1]) count[1]++;
    }
    for (int j = 0; j < 2; j++) {
     if (count[j] > nums.length/3 && !res.contains(x[j])) res.add(x[j]);
    }        
    return res;
}
https://discuss.leetcode.com/topic/32510/java-easy-version-to-understand
 public List<Integer> majorityElement(int[] nums) {
 if (nums == null || nums.length == 0)
  return new ArrayList<Integer>();
 List<Integer> result = new ArrayList<Integer>();
 int number1 = nums[0], number2 = nums[0], count1 = 0, count2 = 0, len = nums.length;
 for (int i = 0; i < len; i++) {
  if (nums[i] == number1)
   count1++;
  else if (nums[i] == number2)
   count2++;
  else if (count1 == 0) {
   number1 = nums[i];
   count1 = 1;
  } else if (count2 == 0) {
   number2 = nums[i];
   count2 = 1;
  } else {
   count1--;
   count2--;
  }
 }
 count1 = 0;
 count2 = 0;
 for (int i = 0; i < len; i++) {
  if (nums[i] == number1)
   count1++;
  else if (nums[i] == number2)
   count2++;
 }
 if (count1 > len / 3)
  result.add(number1);
 if (count2 > len / 3)
  result.add(number2);
 return result;
}
https://discuss.leetcode.com/topic/17409/6-lines-general-case-o-n-time-and-o-k-space

http://blog.csdn.net/nicaishibiantai/article/details/43635069
1. 我们对cnt1,cnt2减数时,相当于丢弃了3个数字(当前数字,n1, n2)。也就是说,每一次丢弃数字,我们是丢弃3个不同的数字。
而Majority number超过了1/3所以它最后一定会留下来。
设定总数为N, majority number次数为m。丢弃的次数是x。则majority 被扔的次数是x
而m > N/3, N - 3x > 0. 
3m > N,  N > 3x 所以 3m > 3x, m > x 也就是说 m一定没有被扔完
最坏的情况,Majority number每次都被扔掉了,但它一定会在n1,n2中。
2. 为什么最后要再检查2个数字呢?因为数字的编排可以让majority 数被过度消耗,使其计数反而小于n2,或者等于n2.前面举的例子即是。
另一个例子:
1 1 1 1 2 3 2 3 4 4 4 这个 1就会被消耗过多,最后余下的反而比4少。
 首先处理count == 0的情况,这里需要注意的是count2 == 0 && key1 = num, 不重不漏。最后再次遍历原数组也必不可少,因为由于添加顺序的区别,count1 和 count2的大小只具有相对意义,还需要最后再次比较其真实计数器值。
    public int majorityNumber(ArrayList<Integer> nums) {
        if (nums == null || nums.isEmpty()) return -1;
        // pair
        int key1 = -1, key2 = -1;
        int count1 = 0, count2 = 0;
        for (int num : nums) {
            if (count1 == 0) {
                key1 = num;
                count1 = 1;
                continue;
            } else if (count2 == 0 && key1 != num) {
                key2 = num;
                count2 = 1;
                continue;
            }
            if (key1 == num) {
                count1++;
            } else if (key2 == num) {
                count2++;
            } else {
                count1--;
                count2--;
            }
        }

        count1 = 0;
        count2 = 0;
        for (int num : nums) {
            if (key1 == num) {
                count1++;
            } else if (key2 == num) {
                count2++;
            }
        }
        return count1 > count2 ? key1 : key2;
    }
}
    public int majorityNumber(ArrayList<Integer> nums) {
        int candidate1 = 0, candidate2 = 0;
        int count1, count2;
        count1 = count2 = 0;
        for (int i = 0; i < nums.size(); i++) {
            if (candidate1 == nums.get(i)) {
                count1 ++;
            } else if (candidate2 == nums.get(i)) {
                count2 ++;
            } else if (count1 == 0) {
                candidate1 = nums.get(i);
                count1 = 1;
            } else if (count2 == 0) {
                candidate2 = nums.get(i);
                count2 = 1;
            } else {
                count1--;
                count2--;
            }
        }
        count1 = count2 = 0;
        for (int i = 0; i < nums.size(); i++) {
            if (nums.get(i) == candidate1) {
                count1++;
            } else if (nums.get(i) == candidate2) {
                count2++;
            }
        }    
        return count1 > count2 ? candidate1 : candidate2;
    }
}
这道题和LintCode的Majority Number II很像,但是稍有不同,需要考虑一个特殊情况:

Input:
[1,2]
Expected:
[1,2]

When there are two different elements in the array, both of the two elements appear more than n/3 times.
    public List<Integer> majorityElement(int[] nums) {
        List<Integer> result = new ArrayList<Integer>();
        if (nums == null || nums.length == 0) {
            return result;
        }
        
        int candidate1 = 0;
        int candidate2 = 0;
        int count1 = 0;
        int count2 = 0;
        
        for (int i = 0; i < nums.length; i++) {
            if (count1 == 0) {
                candidate1 = nums[i];
            } else if (count2 == 0) {
                candidate2 = nums[i];
            }
            
            if (nums[i] == candidate1) {
                count1++;
            }
            else if (nums[i] == candidate2) {
                count2++;
            } else {
                count1--;
                count2--;
            }
        }
        
        count1 = 0;
        count2 = 0;
        for (int i = 0; i < nums.length; i++) {
            if (nums[i] == candidate1) {
                count1++;
            }else if (nums[i] == candidate2) {
                count2++;
            }
        }
        
        if (count1 > nums.length / 3) {
            result.add(candidate1);
        }
        if (count2 > nums.length / 3) {
            result.add(candidate2);
        }
        
        return result;
    }
public List<Integer> majorityElement(int[] nums) {
    List<Integer> result = new ArrayList<Integer>();
 
    Integer n1=null, n2=null;
    int c1=0, c2=0;
 
    for(int i: nums){
        if(n1!=null && i==n1.intValue()){
            c1++;
        }else if(n2!=null && i==n2.intValue()){
            c2++;
        }else if(c1==0){
            c1=1;
            n1=i;
        }else if(c2==0){
            c2=1;
            n2=i;
        }else{
            c1--;
            c2--;
        }
    }
 
    c1=c2=0;
 
    for(int i: nums){
        if(i==n1.intValue()){
            c1++;
        }else if(i==n2.intValue()){
            c2++;
        }
    }
 
    if(c1>nums.length/3)
        result.add(n1);
    if(c2>nums.length/3)
        result.add(n2);
 
    return result;
}
Using Counter Map:
public List<Integer> majorityElement(int[] nums) {
    HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
    for(int i: nums){
        if(map.containsKey(i)){
            map.put(i, map.get(i)+1);
        }else{
            map.put(i, 1);
        }
    }
 
    List<Integer> result = new ArrayList<Integer>();
 
    for(Map.Entry<Integer, Integer> entry: map.entrySet()){
        if(entry.getValue() > nums.length/3){
            result.add(entry.getKey());
        }    
    }
 
    return result;
}


    public int majorityNumber(ArrayList<Integer> nums) {
 7         // write your code
 8         // When there are only 1 or 2 elements in the array,
 9         // there is no solution.
10         if (nums == null || nums.size() <= 2) {
11             return -1;
12         }
13         
14         int n1 = 0;
15         int n2 = 0;
16         
17         int cnt1 = 0;
18         int cnt2 = 0;
19         
20         int size = nums.size();
21         for (int i = 0; i < size; i++) {
22             int num = nums.get(i);
23             if (cnt1 != 0 && num == n1) {
24                 cnt1++;
25             } else if (cnt2 != 0 && num == n2) {
26                 cnt2++;
27             } else if (cnt1 == 0) {
28                 cnt1 = 1;
29                 n1 = num;
30             } else if (cnt2 == 0) {
31                 cnt2 = 1;
32                 n2 = num;
33             } else {
34                 cnt1--;
35                 cnt2--;
36             }
37         }
38         
39         // count the two candiates.
40         cnt1 = 0;
41         cnt2 = 0;
42         for (int num: nums) {
43             if (num == n1) {
44                 cnt1++;
45             } else if (num == n2) {
46                 cnt2++;
47             }
48         }
49         
50         if (cnt1 < cnt2) {
51             return n2;
52         }        
53         
54         return n1;
55     }
X. Follow up: Generalization
What if the frequency becomes 5/n or 6/n or...? Then do we need to define 5 or 6 counts?
    public List<Integer> majorityElement(int[] nums) {
        int n = nums.length, k = 3;  //in this question, k=3 specifically
        List<Integer> result = new ArrayList<Integer>();
        if (n==0 || k<2) return result;
        int[] candidates = new int[k-1];
        int[] counts = new int[k-1];
        for (int num: nums) {
            boolean settled = false;
            for (int i=0; i<k-1; i++) {
                if (candidates[i]==num) {
                    counts[i]++;
                    settled = true;
                    break;
                } 
            }
            if (settled) continue;
            for (int i=0; i<k-1; i++) {
                if (counts[i]==0) {
                    counts[i] = 1;
                    candidates[i] = num;
                    settled = true;
                    break;
                } 
            }
            if (settled) continue;
            for (int i=0; i<k-1; i++) counts[i] = (counts[i] > 0) ? (counts[i]-1) : 0;
        }
        Arrays.fill(counts, 0);
        for (int num: nums) {
            for (int i=0;i<k-1; i++) {
                if (candidates[i]==num) {
                    counts[i]++;
                    break;
                }
            }
        }
        for (int i=0; i<k-1; i++) if (counts[i]>n/k) result.add(candidates[i]);
        return result;
    }
https://leetcode.com/problems/majority-element-ii/discuss/63524/Java-solution-for-generalized-n-k-case
    public List<Integer> majorityElement(int[] nums) {
        if(nums == null || nums.length == 0) return new ArrayList<>();
        return helper(nums, 3);
    }
    public List<Integer> helper(int[] nums, int k){
        Map<Integer, Integer> majorities = new HashMap<>();
        List<Integer> ret = new ArrayList<>();
        for(int num : nums){
            if(majorities.containsKey(num)) majorities.put(num, majorities.get(num) + 1);
            else if(majorities.keySet().size() < k - 1) majorities.put(num, 1);
            else{
                Iterator<Map.Entry<Integer, Integer>> ite = majorities.entrySet().iterator();
                while(ite.hasNext()){
                    Map.Entry<Integer, Integer> entry = ite.next();
                    int val = entry.getValue();
                    if(val == 1) ite.remove();
                    else entry.setValue(val - 1);
                }
            }
        }
        for(Integer i : majorities.keySet()){
            majorities.put(i, 0);
        }
        for(int num : nums){
            if(majorities.containsKey(num)) majorities.put(num, majorities.get(num) + 1);
        }
        int target = nums.length / k;
        for(Integer i : majorities.keySet()){
            if(majorities.get(i) > target) ret.add(i);
        }
        return ret;
    }
https://leetcode.com/problems/majority-element-ii/discuss/63502/6-lines-general-case-O(N)-time-and-O(k)-space
I keep up to two candidates in my counter, so this fulfills the O(N) time and O(1) space requirements.
def majorityElement(self, nums):
    ctr = collections.Counter()
    for n in nums:
        ctr[n] += 1
        if len(ctr) == 3:
            ctr -= collections.Counter(set(ctr))
    return [n for n in ctr if nums.count(n) > len(nums)/3]

Explanation
Think of it this way: Find three different votes and hide them. Repeat until there aren't three different votes left. A number that originally had more than one third of the votes now still has at least one vote, because to hide all of its votes you would've had to hide more than three times one third of the votes - more votes than there were. You can easily have false positives, though, so in the end check whether the remaining up to two candidates actually had more than one third of the votes.
My code does just that: Collect (count) the votes for every number, but remove triples of three different votes on the fly, as soon as we have such a triple.

Generalization to ⌊N/k⌋, still O(N) time but O(k) space
For the general problem, looking for elements appearing more than ⌊N/k⌋ times for some positive integer k, I just have to change my 3 to k. Then it already works and takes takes O(k) space and O(kN) time.
The O(kN) time does not come from the main loop, though. Yes, each ctr -= ... does cost k, but I only have to do it at most N/k times. To put it in terms of the above explanation, I can't hide a vote more than once.
No, the culprit is my last line, counting each remaining candidate separately. If I count them at the same time, I get O(N) again. Here's the full generalized code:
def majorityElement(self, nums, k):
    ctr = collections.Counter()
    for n in nums:
        ctr[n] += 1
        if len(ctr) == k:
            ctr -= collections.Counter(set(ctr))
    ctr = collections.Counter(n for n in nums if n in ctr)
    return [n for n in ctr if ctr[n] > len(nums)/k]

https://reeestart.wordpress.com/2016/07/01/google-majority-number-sorted/
给一个sorted array, 找出所有出现次数大于n/4的数。
[Solution]
看到sorted,应该很本能的反应出binary search。 但是这道题怎么binary search还是有点技巧。首先找出1/4, 2/4, 3/4三个数作为candidates, 因为题目要求是__大于n/4的才是majority,如果是大于等于__,那么就应该取1/4, 2/4, 3/4, 4/4四个candidates。
取完candidates,对每个进行binary search分别找floor和ceiling,然后看index range是不是满足要求(大于n/4)。
[Note]
candidates之间注意去重,有可能某一个candidate甚至出现了超过n/2次。
  public List<Integer> majorityNumber(int[] nums) {
    List<Integer> result = new ArrayList<>();
    if (nums == null || nums.length == 0) {
      return result;
    }
    int n = nums.length;
    int candidate1 = nums[n / 4];
    int candidate2 = nums[n / 2];
    int candidate3 = nums[3 * n / 4];
    if (isMajority(nums, candidate1)) {
      result.add(candidate1);
    }
    if (candidate2 != candidate1 && isMajority(nums, candidate2)) {
      result.add(candidate2);
    }
    if (candidate3 != candidate2 && isMajority(nums, candidate3)) {
      result.add(candidate3);
    }
    return result;
  }
  private boolean isMajority(int[] nums, int candidate) {
    int floor = findFloor(nums, candidate);
    int ceiling = findCeiling(nums, candidate);
    return ceiling - floor - 1 > nums.length / 4;
  }
  private int findFloor(int[] nums, int candidate) {
    if (nums[nums.length - 1] < candidate) {
      return nums.length - 1;
    }
    if (nums[0] >= candidate) {
      return -1;
    }
    int l = 0;
    int r = nums.length - 1;
    while (l <= r) {
      int mid = l + (r - l) / 2;
      if (nums[mid] < candidate) {
        if (mid + 1 < nums.length && nums[mid + 1] >= candidate) {
          return mid;
        }
        else {
          l = mid + 1;
        }
      }
      else {
        if (mid - 1 >= 0 && nums[mid - 1] < candidate) {
          return mid - 1;
        }
        else {
          r = mid - 1;
        }
      }
    }
    return -1;
  }
  private int findCeiling(int[] nums, int candidate) {
    if (nums[nums.length - 1] <= candidate) {
      return nums.length;
    }
    if (nums[0] > candidate) {
      return 0;
    }
    int l = 0;
    int r = nums.length - 1;
    while (l <= r) {
      int mid = l + (r - l) / 2;
      if (nums[mid] <= candidate) {
        if (mid + 1 < nums.length && nums[mid + 1] > candidate) {
          return mid + 1;
        }
        else {
          l = mid + 1;
        }
      }
      else {
        if (mid - 1 >= 0 && nums[mid - 1] <= candidate) {
          return mid;
        }
        else {
          r = mid - 1;
        }
      }
    }
    return -1;
  }


Labels

LeetCode (1432) GeeksforGeeks (1122) LeetCode - Review (1067) Review (882) Algorithm (668) to-do (609) Classic Algorithm (270) Google Interview (237) Classic Interview (222) Dynamic Programming (220) DP (186) Bit Algorithms (145) POJ (141) Math (137) Tree (132) LeetCode - Phone (129) EPI (122) Cracking Coding Interview (119) DFS (115) Difficult Algorithm (115) Lintcode (115) Different Solutions (110) Smart Algorithm (104) Binary Search (96) BFS (91) HackerRank (90) Binary Tree (86) Hard (79) Two Pointers (78) Stack (76) Company-Facebook (75) BST (72) Graph Algorithm (72) Time Complexity (69) Greedy Algorithm (68) Interval (63) Company - Google (62) Geometry Algorithm (61) Interview Corner (61) LeetCode - Extended (61) Union-Find (60) Trie (58) Advanced Data Structure (56) List (56) Priority Queue (53) Codility (52) ComProGuide (50) LeetCode Hard (50) Matrix (50) Bisection (48) Segment Tree (48) Sliding Window (48) USACO (46) Space Optimization (45) Company-Airbnb (41) Greedy (41) Mathematical Algorithm (41) Tree - Post-Order (41) ACM-ICPC (40) Algorithm Interview (40) Data Structure Design (40) Graph (40) Backtracking (39) Data Structure (39) Jobdu (39) Random (39) Codeforces (38) Knapsack (38) LeetCode - DP (38) Recursive Algorithm (38) String Algorithm (38) TopCoder (38) Sort (37) Introduction to Algorithms (36) Pre-Sort (36) Beauty of Programming (35) Must Known (34) Binary Search Tree (33) Follow Up (33) prismoskills (33) Palindrome (32) Permutation (31) Array (30) Google Code Jam (30) HDU (30) Array O(N) (29) Logic Thinking (29) Monotonic Stack (29) Puzzles (29) Code - Detail (27) Company-Zenefits (27) Microsoft 100 - July (27) Queue (27) Binary Indexed Trees (26) TreeMap (26) to-do-must (26) 1point3acres (25) GeeksQuiz (25) Merge Sort (25) Reverse Thinking (25) hihocoder (25) Company - LinkedIn (24) Hash (24) High Frequency (24) Summary (24) Divide and Conquer (23) Proof (23) Game Theory (22) Topological Sort (22) Lintcode - Review (21) Tree - Modification (21) Algorithm Game (20) CareerCup (20) Company - Twitter (20) DFS + Review (20) DP - Relation (20) Brain Teaser (19) DP - Tree (19) Left and Right Array (19) O(N) (19) Sweep Line (19) UVA (19) DP - Bit Masking (18) LeetCode - Thinking (18) KMP (17) LeetCode - TODO (17) Probabilities (17) Simulation (17) String Search (17) Codercareer (16) Company-Uber (16) Iterator (16) Number (16) O(1) Space (16) Shortest Path (16) itint5 (16) DFS+Cache (15) Dijkstra (15) Euclidean GCD (15) Heap (15) LeetCode - Hard (15) Majority (15) Number Theory (15) Rolling Hash (15) Tree Traversal (15) Brute Force (14) Bucket Sort (14) DP - Knapsack (14) DP - Probability (14) Difficult (14) Fast Power Algorithm (14) Pattern (14) Prefix Sum (14) TreeSet (14) Algorithm Videos (13) Amazon Interview (13) Basic Algorithm (13) Codechef (13) Combination (13) Computational Geometry (13) DP - Digit (13) LCA (13) LeetCode - DFS (13) Linked List (13) Long Increasing Sequence(LIS) (13) Math-Divisible (13) Reservoir Sampling (13) mitbbs (13) Algorithm - How To (12) Company - Microsoft (12) DP - Interval (12) DP - Multiple Relation (12) DP - Relation Optimization (12) LeetCode - Classic (12) Level Order Traversal (12) Prime (12) Pruning (12) Reconstruct Tree (12) Thinking (12) X Sum (12) AOJ (11) Bit Mask (11) Company-Snapchat (11) DP - Space Optimization (11) Dequeue (11) Graph DFS (11) MinMax (11) Miscs (11) Princeton (11) Quick Sort (11) Stack - Tree (11) 尺取法 (11) 挑战程序设计竞赛 (11) Coin Change (10) DFS+Backtracking (10) Facebook Hacker Cup (10) Fast Slow Pointers (10) HackerRank Easy (10) Interval Tree (10) Limited Range (10) Matrix - Traverse (10) Monotone Queue (10) SPOJ (10) Starting Point (10) States (10) Stock (10) Theory (10) Tutorialhorizon (10) Kadane - Extended (9) Mathblog (9) Max-Min Flow (9) Maze (9) Median (9) O(32N) (9) Quick Select (9) Stack Overflow (9) System Design (9) Tree - Conversion (9) Use XOR (9) Book Notes (8) Company-Amazon (8) DFS+BFS (8) DP - States (8) Expression (8) Longest Common Subsequence(LCS) (8) One Pass (8) Quadtrees (8) Traversal Once (8) Trie - Suffix (8) 穷竭搜索 (8) Algorithm Problem List (7) All Sub (7) Catalan Number (7) Cycle (7) DP - Cases (7) Facebook Interview (7) Fibonacci Numbers (7) Flood fill (7) Game Nim (7) Graph BFS (7) HackerRank Difficult (7) Hackerearth (7) Inversion (7) Kadane’s Algorithm (7) Manacher (7) Morris Traversal (7) Multiple Data Structures (7) Normalized Key (7) O(XN) (7) Radix Sort (7) Recursion (7) Sampling (7) Suffix Array (7) Tech-Queries (7) Tree - Serialization (7) Tree DP (7) Trie - Bit (7) 蓝桥杯 (7) Algorithm - Brain Teaser (6) BFS - Priority Queue (6) BFS - Unusual (6) Classic Data Structure Impl (6) DP - 2D (6) DP - Monotone Queue (6) DP - Unusual (6) DP-Space Optimization (6) Dutch Flag (6) How To (6) Interviewstreet (6) Knapsack - MultiplePack (6) Local MinMax (6) MST (6) Minimum Spanning Tree (6) Number - Reach (6) Parentheses (6) Pre-Sum (6) Probability (6) Programming Pearls (6) Rabin-Karp (6) Reverse (6) Scan from right (6) Schedule (6) Stream (6) Subset Sum (6) TSP (6) Xpost (6) n00tc0d3r (6) reddit (6) AI (5) Abbreviation (5) Anagram (5) Art Of Programming-July (5) Assumption (5) Bellman Ford (5) Big Data (5) Code - Solid (5) Code Kata (5) Codility-lessons (5) Coding (5) Company - WMware (5) Convex Hull (5) Crazyforcode (5) DFS - Multiple (5) DFS+DP (5) DP - Multi-Dimension (5) DP-Multiple Relation (5) Eulerian Cycle (5) Graph - Unusual (5) Graph Cycle (5) Hash Strategy (5) Immutability (5) Java (5) LogN (5) Manhattan Distance (5) Matrix Chain Multiplication (5) N Queens (5) Pre-Sort: Index (5) Quick Partition (5) Quora (5) Randomized Algorithms (5) Resources (5) Robot (5) SPFA(Shortest Path Faster Algorithm) (5) Shuffle (5) Sieve of Eratosthenes (5) Strongly Connected Components (5) Subarray Sum (5) Sudoku (5) Suffix Tree (5) Swap (5) Threaded (5) Tree - Creation (5) Warshall Floyd (5) Word Search (5) jiuzhang (5)

Popular Posts