leetcode 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:
- postTweet(userId, tweetId): Compose a new tweet.
- 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.
- follow(followerId, followeeId): Follower follows a followee.
- unfollow(followerId, followeeId): Follower unfollows a followee.
123456789101112131415161718192021222324 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 -> .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 -> ,// since user 1 is no longer following user 2.twitter.getNewsFeed(1);
leetcode 169. Majority Element
Given an array of size n, find the majority element. The majority element is the element that appears more than⌊ n/2 ⌋ times.
You may assume that the array is non-empty and the majority element always exist in the array.
leetcode Russian Doll Envelopes
You have a number of envelopes with widths and heights given as a pair of integers
(w, h). One envelope can fit into another if and only if both the width and height of one envelope is greater than the width and height of the other envelope.
What is the maximum number of envelopes can you Russian doll? (put one inside other)
Given envelopes =
[[5,4],[6,4],[6,7],[2,3]], the maximum number of envelopes you can Russian doll is
3([2,3] => [5,4] => [6,7]).
leetcode Intersection of Two Arrays
Given two arrays, write a function to compute their intersection.
Given nums1 =
[1, 2, 2, 1], nums2 =
[2, 2], return
- Each element in the result must be unique.
- The result can be in any order.
leetcode Top K Frequent Elements
Given a non-empty array of integers, return the k most frequent elements.
[1,1,1,2,2,3]and k = 2, return
- You may assume k is always valid, 1 ≤ k ≤ number of unique elements.
- Your algorithm’s time complexity must be better than O(n log n), where n is the array’s size.
leetcode Reverse String
Write a function that takes a string as input and returns the string reversed.
Given s = “hello”, return “olleh”.
leetcode Integer Break
Given a positive integer n, break it into the sum of at least two positive integers and maximize the product of those integers. Return the maximum product you can get.
For example, given n = 2, return 1 (2 = 1 + 1); given n = 10, return 36 (10 = 3 + 3 + 4).
Note: you may assume that n is not less than 2.
leetcode Power of Four
Given an integer (signed 32 bits), write a function to check whether it is a power of 4.
Given num = 16, return true. Given num = 5, return false.
Follow up: Could you solve it without loops/recursion?
leetcode Counting Bits
Given a non negative integer number num. For every numbers i in the range 0 ≤ i ≤ num calculate the number of 1’s in their binary representation and return them as an array.
num = 5you should return
- It is very easy to come up with a solution with run time O(n*sizeof(integer)). But can you do it in linear time O(n) /possibly in a single pass?
- Space complexity should be O(n).
- Can you do it like a boss? Do it without using any builtin function like __builtin_popcount in c++ or in any other language.
leetcode House Robber III
The thief has found himself a new place for his thievery again. There is only one entrance to this area, called the “root.” Besides the root, each house has one and only one parent house. After a tour, the smart thief realized that “all houses in this place forms a binary tree”. It will automatically contact the police if two directly-linked houses were broken into on the same night.
Determine the maximum amount of money the thief can rob tonight without alerting the police.
12345 3/ \2 3\ \3 1
Maximum amount of money the thief can rob = 3 + 3 + 1 = 7.
12345 3/ \4 5/ \ \1 3 1
Maximum amount of money the thief can rob = 4 + 5 = 9.