LeetCode 355 - Design Twitter


https://leetcode.com/problems/design-twitter/
Design a simplified version of Twitter where users can post tweets, follow/unfollow another user and is able to see the 10 most recent tweets in the user's news feed. Your design should support the following methods:
  1. postTweet(userId, tweetId): Compose a new tweet.
  2. getNewsFeed(userId): Retrieve the 10 most recent tweet ids in the user's news feed. Each item in the news feed must be posted by users who the user followed or by the user herself. Tweets must be ordered from most recent to least recent.
  3. follow(followerId, followeeId): Follower follows a followee.
  4. unfollow(followerId, followeeId): Follower unfollows a followee.
Example:
Twitter twitter = new Twitter();

// User 1 posts a new tweet (id = 5).
twitter.postTweet(1, 5);

// User 1's news feed should return a list with 1 tweet id -> [5].
twitter.getNewsFeed(1);

// User 1 follows user 2.
twitter.follow(1, 2);

// User 2 posts a new tweet (id = 6).
twitter.postTweet(2, 6);

// User 1's news feed should return a list with 2 tweet ids -> [6, 5].
// Tweet id 6 should precede tweet id 5 because it is posted after tweet id 5.
twitter.getNewsFeed(1);

// User 1 unfollows user 2.
twitter.unfollow(1, 2);

// User 1's news feed should return a list with 1 tweet id -> [5],
// since user 1 is no longer following user 2.
twitter.getNewsFeed(1);
Merge k sorted list
X. https://leetcode.com/problems/design-twitter/discuss/82863/Python-solution

https://leetcode.com/problems/design-twitter/discuss/82920/Java-Solution-using-HashMap-and-PriorityQueue

https://leetcode.com/problems/design-twitter/discuss/82825/Java-OO-Design-with-most-efficient-function-getNewsFeed
public class Twitter {
 private static int timeStamp=0;

 // easy to find if user exist
 private Map<Integer, User> userMap;

 // Tweet link to next Tweet so that we can save a lot of time
 // when we execute getNewsFeed(userId)
 private class Tweet{
  public int id;
  public int time;
  public Tweet next;

  public Tweet(int id){
   this.id = id;
   time = timeStamp++;
   next=null;
  }
 }


 // OO design so User can follow, unfollow and post itself
 public class User{
  public int id;
  public Set<Integer> followed;
  public Tweet tweet_head;

  public User(int id){
   this.id=id;
   followed = new HashSet<>();
   follow(id); // first follow itself
   tweet_head = null;
  }

  public void follow(int id){
   followed.add(id);
  }

  public void unfollow(int id){
   followed.remove(id);
  }


  // everytime user post a new tweet, add it to the head of tweet list.
  public void post(int id){
   Tweet t = new Tweet(id);
   t.next=tweet_head;
   tweet_head=t;
  }
 }




 /** Initialize your data structure here. */
 public Twitter() {
  userMap = new HashMap<Integer, User>();
 }

 /** Compose a new tweet. */
 public void postTweet(int userId, int tweetId) {
  if(!userMap.containsKey(userId)){
   User u = new User(userId);
   userMap.put(userId, u);
  }
  userMap.get(userId).post(tweetId);

 }



 // Best part of this.
 // first get all tweets lists from one user including itself and all people it followed.
 // Second add all heads into a max heap. Every time we poll a tweet with 
 // largest time stamp from the heap, then we add its next tweet into the heap.
 // So after adding all heads we only need to add 9 tweets at most into this 
 // heap before we get the 10 most recent tweet.
 public List<Integer> getNewsFeed(int userId) {
  List<Integer> res = new LinkedList<>();

  if(!userMap.containsKey(userId))   return res;

  Set<Integer> users = userMap.get(userId).followed;
  PriorityQueue<Tweet> q = new PriorityQueue<Tweet>(users.size(), (a,b)->(b.time-a.time));
  for(int user: users){
   Tweet t = userMap.get(user).tweet_head;
   // very imporant! If we add null to the head we are screwed.
   if(t!=null){
    q.add(t);
   }
  }
  int n=0;
  while(!q.isEmpty() && n<10){
    Tweet t = q.poll();
    res.add(t.id);
    n++;
    if(t.next!=null)
   q.add(t.next);
  }

  return res;

 }

 /** Follower follows a followee. If the operation is invalid, it should be a no-op. */
 public void follow(int followerId, int followeeId) {
  if(!userMap.containsKey(followerId)){
   User u = new User(followerId);
   userMap.put(followerId, u);
  }
  if(!userMap.containsKey(followeeId)){
   User u = new User(followeeId);
   userMap.put(followeeId, u);
  }
  userMap.get(followerId).follow(followeeId);
 }

 /** Follower unfollows a followee. If the operation is invalid, it should be a no-op. */
 public void unfollow(int followerId, int followeeId) {
  if(!userMap.containsKey(followerId) || followerId==followeeId)
   return;
  userMap.get(followerId).unfollow(followeeId);
 }
}
Let a Tweet hold a ref to the next Tweet cannot be implemented in practice. It means that all tweets in a feed have to be loaded from db to memory at the same time. The loading time and memory usage can be very high. A more practical design is to load tweets in small batches, or one by one. An iterator can be used to hide this complexity. Saving a user with all his/her followees has the same problem. And in terms of code design, you should always try to refer other objects(entities) with their id, instead of the actual object reference. Check out the design below, which avoid the problems mentioned.
    Map<Integer, List<Tweet>> tweets = new HashMap<>(); // userid -> user's tweets
    Map<Integer, Set<Integer>> followees = new HashMap<>(); // userid -> user's followees

    /** Initialize your data structure here. */
    public Twitter() {

    }

    /** Compose a new tweet. */
    public void postTweet(int userId, int tweetId) {
        if (!tweets.containsKey(userId)) tweets.put(userId, new LinkedList<>());
        tweets.get(userId).add(0, new Tweet(tweetId));
    }

    /** Retrieve the 10 most recent tweet ids in the user's news feed. Each item in the news feed must be posted by users who the user followed or by the user herself. Tweets must be ordered from most recent to least recent. */
    public List<Integer> getNewsFeed(int userId) {
        Queue<Feed> q = new PriorityQueue<>(Comparator.comparing(f -> -f.curr.order)); // descending

        if (!tweets.getOrDefault(userId, Collections.emptyList()).isEmpty()) {
            q.offer(new Feed(tweets.get(userId)));
        }

        for (Integer followee : followees.getOrDefault(userId, Collections.emptySet())) {
            if (!tweets.getOrDefault(followee, Collections.emptyList()).isEmpty()){
                q.offer(new Feed(tweets.get(followee)));
            }
        }

        List<Integer> feeds = new ArrayList<>();
        for (int i = 0; i < 10 && !q.isEmpty(); i++) {
            Feed feed = q.poll();
            feeds.add(feed.curr.id);

            if (feed.advance()) {
                q.offer(feed);
            }
        }

        return feeds;
    }

    /** Follower follows a followee. If the operation is invalid, it should be a no-op. */
    public void follow(int followerId, int followeeId) {
        if (followerId == followeeId) return;
        if (!followees.containsKey(followerId)) followees.put(followerId, new HashSet<>());
        followees.get(followerId).add(followeeId);
    }

    /** Follower unfollows a followee. If the operation is invalid, it should be a no-op. */
    public void unfollow(int followerId, int followeeId) {
        if (!followees.containsKey(followerId)) return;
        followees.get(followerId).remove(followeeId);
    }

    int globalOrder = 0;

    class Tweet {
        int id;
        int order;

        Tweet(int id) {
            this.id = id;
            this.order = globalOrder++;
        }
    }

    class Feed {
        Iterator<Tweet> iterator;
        Tweet curr;

        Feed(List<Tweet> tweets) {
            // tweets cannot be empty
            iterator = tweets.iterator();
            curr = iterator.next();
        }

        boolean advance() {
            if (!iterator.hasNext()) return false;
            this.curr = iterator.next();
            return true;
        }
    }
http://bookshadow.com/weblog/2016/06/11/leetcode-design-twitter/
Map + Set + PriorityQueue
系统应当维护下列信息:
1). 一个全局的推文计数器:postCount 用户发推文时,计数器+1

2). 推文Id -> 推文计数器的映射:tweetIdMap 用来记录推文的次序

3). 推文Id -> 用户Id的映射:tweetOwnerMap 用来记录推文的发送者是谁

4). 用户Id -> 关注对象集合的映射: followeeMap 用来记录用户的关注对象(关注/取消关注)
方法的实现:
1). 关注 follow / 取消关注 unfollow:
只需要维护followeeMap中对应的关注对象集合followeeSet即可
2). 发送推文 postTweet:
更新推文计数器postCount,维护tweetIdMap;

向用户的推文发送列表中新增一条记录
3). 获取推文推送 getNewsFeed:
这里需要使用优先队列PriorityQueue,记为pq

遍历用户的关注对象列表,将每一位关注对象的最新一条推文添加至pq中。

然后从pq中弹出最近的一条推文,记为topTweetId;

通过tweetOwnerMap找到这条推文的发送者userId,然后将该发送者的下一条比较新的推文添加至pq。

直到弹出10条最新的推文,或者pq为空为止。
private int postCount; private Map<Integer, Integer> tweetCountMap; private Map<Integer, List<Integer>> tweetIdMap; private Map<Integer, Integer> tweetOwnerMap; private Map<Integer, Set<Integer>> followeeMap; /** Initialize your data structure here. */ public Twitter() { tweetCountMap = new HashMap<Integer, Integer>(); tweetIdMap = new HashMap<Integer, List<Integer>>(); tweetOwnerMap = new HashMap<Integer, Integer>(); followeeMap = new HashMap<Integer, Set<Integer>>(); } /** Compose a new tweet. */ public void postTweet(int userId, int tweetId) { tweetCountMap.put(tweetId, ++postCount); tweetOwnerMap.put(tweetId, userId); getTweetIdList(userId).add(tweetId); } /** Retrieve the 10 most recent tweet ids in the user's news feed. Each item in the news feed must be posted by users who the user followed or by the user herself. Tweets must be ordered from most recent to least recent. */ public List<Integer> getNewsFeed(int userId) { List<Integer> result = new ArrayList<Integer>(); Set<Integer> followeeSet = getFolloweeSet(userId); PriorityQueue<Integer> pq = new PriorityQueue<Integer>(new Comparator<Integer>(){ @Override public int compare(Integer a, Integer b) { return tweetCountMap.get(b) - tweetCountMap.get(a); } }); Map<Integer, Integer> idxMap = new HashMap<Integer, Integer>(); for (Integer followeeId : followeeSet) { List<Integer> tweetIdList = getTweetIdList(followeeId); int idx = tweetIdList.size() - 1; if (idx >= 0) { idxMap.put(followeeId, idx - 1); pq.add(tweetIdList.get(idx)); } } while (result.size() < 10 && !pq.isEmpty()) { Integer topTweetId = pq.poll(); result.add(topTweetId); Integer ownerId = tweetOwnerMap.get(topTweetId); int idx = idxMap.get(ownerId); if (idx >= 0) { List<Integer> tweetIdList = getTweetIdList(ownerId); pq.add(tweetIdList.get(idx)); idxMap.put(ownerId, idx - 1); } } return result; } /** Follower follows a followee. If the operation is invalid, it should be a no-op. */ public void follow(int followerId, int followeeId) { getFolloweeSet(followerId).add(followeeId); } /** Follower unfollows a followee. If the operation is invalid, it should be a no-op. */ public void unfollow(int followerId, int followeeId) { if (followerId != followeeId) { getFolloweeSet(followerId).remove(followeeId); } } /** Get a non-empty followee set of an user. */ private Set<Integer> getFolloweeSet(int userId) { Set<Integer> followeeSet = followeeMap.get(userId); if (followeeSet == null) { followeeSet = new HashSet<Integer>(); followeeSet.add(userId); followeeMap.put(userId, followeeSet); } return followeeSet; } /** Get a non-empty tweet id list of an user. */ private List<Integer> getTweetIdList(int userId) { List<Integer> tweetIdList = tweetIdMap.get(userId); if (tweetIdList == null) { tweetIdList = new ArrayList<Integer>(); tweetIdMap.put(userId, tweetIdList); } return tweetIdList; }
https://leetcode.com/discuss/107638/java-solution-using-hashmap-%26-hashset-%26-priorityqueue
private Map<Integer,Set<Integer>> users = new HashMap<>(); private Map<Integer,Map<Integer,Integer>> tweets = new HashMap<>(); private int timeStamp = 0; public Twitter() { } public void postTweet(int userId, int tweetId) { if(users.get(userId) == null){ users.put(userId,new HashSet<>()); tweets.put(userId,new HashMap<>()); } tweets.get(userId).put(timeStamp++,tweetId); } public List<Integer> getNewsFeed(int userId) { List<Integer> res = new ArrayList<>(); if(users.get(userId) == null) return res; Queue<Map.Entry<Integer,Integer>> queue = new PriorityQueue<>((e1,e2) -> e2.getKey() - e1.getKey()); for(Map.Entry<Integer,Integer> e : tweets.get(userId).entrySet()) queue.offer(e); for(Integer user : users.get(userId)){ for(Map.Entry<Integer,Integer> e : tweets.get(user).entrySet()){ queue.offer(e); } } for(int i = 0; i < 10 && !queue.isEmpty(); i++){ res.add(queue.poll().getValue()); } return res; } public void follow(int followerId, int followeeId) { if(followerId == followeeId) return; if(users.get(followerId) == null){ users.put(followerId,new HashSet<>()); tweets.put(followerId,new HashMap<>()); } if(users.get(followeeId) == null){ users.put(followeeId,new HashSet<>()); tweets.put(followeeId,new HashMap<>()); } users.get(followerId).add(followeeId); } public void unfollow(int followerId, int followeeId) { if(followerId == followeeId) return; if(users.get(followerId) == null || users.get(followeeId) == null) return; users.get(followerId).remove(followeeId); }
https://leetcode.com/discuss/107697/java-solution-using-hashmap-and-priorityqueue
private static class Tweet { int timestamp; int tweetId; public Tweet(int tweetId, int timestamp) { this.tweetId = tweetId; this.timestamp = timestamp; } } private Map<Integer, Set<Integer>> followMap = new HashMap<Integer, Set<Integer>>(); private Map<Integer, List<Tweet>> tweetMap = new HashMap<Integer, List<Tweet>>(); private AtomicInteger timestamp; /** Initialize your data structure here. */ public Twitter() { timestamp = new AtomicInteger(0); } /** Compose a new tweet. */ public void postTweet(int userId, int tweetId) { Tweet newTweet = new Tweet(tweetId, timestamp.getAndIncrement()); if (!tweetMap.containsKey(userId)) { tweetMap.put(userId, new ArrayList<Tweet>()); //Assuming no deletion for now? } tweetMap.get(userId).add(newTweet); } /** * Retrieve the 10 most recent tweet ids in the user's news feed. Each item * in the news feed must be posted by users who the user followed or by the * user herself. Tweets must be ordered from most recent to least recent. */ public List<Integer> getNewsFeed(int userId) { List<Integer> result = new ArrayList<Integer>(10); PriorityQueue<int[]> pq = new PriorityQueue<int[]>(new Comparator<int[]>() { public int compare(int[] it1, int[] it2) { return tweetMap.get(it2[0]).get(it2[1]).timestamp - tweetMap.get(it1[0]).get(it1[1]).timestamp; } }); Set<Integer> followeeSet = new HashSet<Integer>(); followeeSet.add(userId); if (followMap.containsKey(userId)) { followeeSet.addAll(followMap.get(userId)); } for (Integer followee : followeeSet) { if (tweetMap.containsKey(followee)) { List<Tweet> tweetList = tweetMap.get(followee); if (tweetList.size() > 0) { pq.add(new int[] { followee, tweetList.size() - 1 }); } } } while (result.size() < 10 && pq.size() > 0) { int[] it = pq.poll(); result.add(tweetMap.get(it[0]).get(it[1]).tweetId); it[1]--; if (it[1] >= 0) { pq.add(it); } } return result; } /** * Follower follows a followee. If the operation is invalid, it should be a * no-op. */ public void follow(int followerId, int followeeId) { Set<Integer> followSet = followMap.get(followerId); if (followSet == null) { followSet = new HashSet<Integer>(); followMap.put(followerId, followSet); } followSet.add(followeeId); } /** * Follower unfollows a followee. If the operation is invalid, it should be * a no-op. */ public void unfollow(int followerId, int followeeId) { Set<Integer> followSet = followMap.get(followerId); if (followSet == null) { followSet = new HashSet<Integer>(); followMap.put(followerId, followSet); } followSet.remove(followeeId); }
http://www.w2bc.com/article/149069
用户之间的follow关系因为userId是唯一的,所以可以用HashMap保存user之间的follow关系。
如何设计用户的feed流呢?看到本题的tag有heap,想了一下没想到如何用heap实现feed, 那就暴力点,将所有用户发布的feed 用一个list保存。 当要获取某用户的feeds时,按时间顺序从list后往前,如果一个feed的userid属于该用户或其follower则 将该feed存入结果集。
题目不难,甚至有点简单。。follow和unfollow时间复杂度为O(1),空间复杂度为O(n),getNewsFeed时间复杂度为O(n),空间复杂度为O(1)。
    HashMap<Integer, Set<Integer>> maps = new HashMap<Integer, Set<Integer>>();
    List<Feed> feeds = new ArrayList<Feed>();

    class Feed {
        int userId, tweetId;

        public Feed(int userId, int tweetId) {
            this.userId = userId;
            this.tweetId = tweetId;
        }

        public String toString(){
            return tweetId+"";
        }
    }

    /** Initialize your data structure here. */
    public Twitter() {

    }

    /** Compose a new tweet. */
    public void postTweet(int userId, int tweetId) {
        Feed f = new Feed(userId, tweetId);
        feeds.add(f);
    }

    /**
     * Retrieve the 10 most recent tweet ids in the user's news feed. Each item
     * in the news feed must be posted by users who the user followed or by the
     * user herself. Tweets must be ordered from most recent to least recent.
     */
    public List<Integer> getNewsFeed(int userId) {
        List<Integer> res = new ArrayList<Integer>();
        Set<Integer> users = maps.get(userId);
        if(users==null)
            users=  new HashSet<Integer>();
        users.add(userId);
        for (int i=feeds.size()-1;i>=0;i--) {
            Feed f = feeds.get(i);
            if (res.size()<10&&users.contains(f.userId)) {
                res.add(f.tweetId);
            }
            if(res.size()>=10)
                break;
        }
        return res;
    }

    /**
     * Follower follows a followee. If the operation is invalid, it should be a
     * no-op.
     */
    public void follow(int followerId, int followeeId) {
        Set<Integer> sets;
        if (maps.containsKey(followerId)) {
            sets = maps.get(followerId);
        } else {
            sets = new HashSet<Integer>();
        }
        sets.add(followeeId);
        maps.put(followerId, sets);
    }

    /**
     * Follower unfollows a followee. If the operation is invalid, it should be
     * a no-op.
     */
    public void unfollow(int followerId, int followeeId) {
        Set<Integer> sets = maps.get(followerId);
        if(sets!=null&&sets.contains(followeeId)){
            sets.remove(followeeId);
            maps.put(followerId, sets);
        }
    }
https://www.hrwhisper.me/leetcode-design-twitter/
题意:要求设计一个数据结构,使其能满足twitter的4种基本操作,发推、获得关注用户和自身最新10条推文、关注用户和取消关注。
思路:水题。和上一题一样,不明白为啥为hard。  可能难点就在于,直接写出无bug 的code吧。
需要那些非法的情况需要进行考虑吧。比如:
  • follow操作 followerId, followeeId 相等
  • unfollow操作followerId 不存在,或者followerId 压根就没关注 followeeId
求10个最近的推文可以用堆(当然这里我没有)

Python
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
class Twitter(object):
    def __init__(self):
        """
        Initialize your data structure here.
        """
        self.tweets_cnt = 0
        self.tweets = collections.defaultdict(list)
        self.follower_ship = collections.defaultdict(set)
    def postTweet(self, userId, tweetId):
        """
        Compose a new tweet.
        :type userId: int
        :type tweetId: int
        :rtype: void
        """
        self.tweets[userId].append([tweetId, self.tweets_cnt])
        self.tweets_cnt += 1
    def getNewsFeed(self, userId):
        """
        Retrieve the 10 most recent tweet ids in the user's news feed. Each item in the news feed must be posted by users who the user followed or by the user herself. Tweets must be ordered from most recent to least recent.
        :type userId: int
        :rtype: List[int]
        """
        recent_tweets = []
        user_list = list(self.follower_ship[userId]) + [userId]
        userId_tweet_index = [[userId, len(self.tweets[userId]) - 1] for userId in user_list if userId in self.tweets]
        for _ in xrange(10):
            max_index = max_tweet_id = max_user_id = -1
            for i, (user_id, tweet_index) in enumerate(userId_tweet_index):
                if tweet_index >= 0:
                    tweet_info = self.tweets[user_id][tweet_index]
                    if tweet_info[1] > max_tweet_id:
                        max_index, max_tweet_id, max_user_id = i, tweet_info[1], user_id
            if max_index < 0: break
            recent_tweets.append(self.tweets[max_user_id][userId_tweet_index[max_index][1]][0])
            userId_tweet_index[max_index][1] -= 1
        return recent_tweets
    def follow(self, followerId, followeeId):
        """
        Follower follows a followee. If the operation is invalid, it should be a no-op.
        :type followerId: int
        :type followeeId: int
        :rtype: void
        """
        if followerId != followeeId:
            self.follower_ship[followerId].add(followeeId)
    def unfollow(self, followerId, followeeId):
        """
        Follower unfollows a followee. If the operation is invalid, it should be a no-op.
        :type followerId: int
        :type followeeId: int
        :rtype: void
        """
        if followerId in self.follower_ship and followeeId in self.follower_ship[followerId]:
            self.follower_ship[followerId].remove(followeeId)

https://www.programcreek.com/2014/08/leetcode-design-twitter-java/
http://www.cnblogs.com/grandyang/p/5577038.html
就是在保存用户所有消息的时候,用的是vector<pair<int, int>>,这样我们可以用priority_queue来帮助我们找出最新10条消息


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