11 2016 档案
Leetcode: Random Pick Index
摘要:Three types of answer: Map Solution, O(N) memory, O(N) init, O(1) pick. Like @dettier's Reservoir Sampling. O(1) init, O(1) memory, but O(N) to pick.
阅读全文
Leetcode: Sum of Left Leaves
摘要:Recursion: 是不是left子数完全由bottom往上第二层决定,如果是left子树且是叶子节点,那么就是left leaves, parent得告诉child是不是在left子树 BFS:
阅读全文
Leetcode: Is Subsequence
摘要:Basic Solution: DP, O(mn) time, O(m) space, m is the size of s, n is the size of t Greedy Solution: O(n) time, O(1) space Follow Up: The best solution
阅读全文
Leetcode: Elimination Game
摘要:refer to https://discuss.leetcode.com/topic/59293/java-easiest-solution-o-logn-with-explanation Time Complexity: O(log n) update and record head in ea
阅读全文
Leetcode: Longest Absolute File Path
摘要:Time Complexity: O(N) The depth of the directory/file is calculated by counting how many "\t"s are there.The time complexity is O(n) because each subs
阅读全文
Leetcode: Mini Parser
摘要:有括号这种一般要用stack, stack top 就是当前着眼的那一个NestedInteger, 可以对其添加新的元素。 注意47行判断很关键,顺带处理了 "[]," 括号后面是逗号的情况
阅读全文
Leetcode: First Unique Character in a String
摘要:Given a string, find the first non-repeating character in it and return it's index. If it doesn't exist, return -1. Examples: s = "leetcode" return 0. s = "loveleetcode", return 2. Note: You may a...
阅读全文
Leetcode: Lexicographical Numbers
摘要:Solution 1: Solution 2: O(N) time, O(1) space The basic idea is to find the next number to add.Take 45 for example: if the current number is 45, the n
阅读全文
Leetcode: Linked List Random Node
摘要:Solution 1: Reservior sampling: (wiki introduction) Reservoir sampling is a family of randomized algorithms for randomly choosing a sample of k items
阅读全文
Leetcode: Insert Delete GetRandom O(1) - Duplicates allowed
摘要:The idea is to add a set to the hashMap to remember all the locations of a duplicated number.
阅读全文
Leetcode: Insert Delete GetRandom O(1)
摘要:最先想到是用double LinkedList+Map, 没必要,arraylist+map就够了;另外取random的方法还有,rand.nextInt(int n) returns an integer in the range [0, n) java.util.Random rand = ne
阅读全文
Leetcode: Kth Smallest Element in a Sorted Matrix
摘要:Heap: you need to know the row number and column number of that element(so we can create a tuple class here) Binary Search方法:(12/28仔细看了之后觉得没必要深究,有时间再去
阅读全文
Leetcode: Combination Sum IV && Summary: The Key to Solve DP
摘要:DP 解法: the key to solve DP problem is to think about how to create overlap, how to re-solve subproblems(怎么制造复用) Bottom up dp: Better Solution(Bottom-u
阅读全文
Leetcode: Wiggle Subsequence
摘要:DP solution is O(N^2), better way is greedy Now for explanation, we take example series:2,1,4,5,6,3,3,4,8,4 First we check if the series is starting a
阅读全文
Leetcode: Guess Number Higher or Lower II
摘要:Clarification of the problem: https://discuss.leetcode.com/topic/68252/clarification-on-the-problem-description-problem-description-need-to-be-updated
阅读全文
Leetcode: Guess Number Higher or Lower
摘要:Binary Search Here "My" means the number which is given for you to guess not the number you put into guess(int num).
阅读全文
Leetcode: Find K Pairs with Smallest Sums
摘要:Better Solution: O(KlogK), 转自https://discuss.leetcode.com/topic/50885/simple-java-o-klogk-solution-with-explanation Some observations: For every numbe
阅读全文
Leetcode: Super Pow
摘要:ab % k = (a%k)(b%k)%kSince the power here is an array, we'd better handle it digit by digit.One observation:a^1234567 % k = (a^1234560 % k) * (a^7 % k
阅读全文
Leetcode: Largest Divisible Subset
摘要:DP Solution similar to Longest Increasing Subsequence: 我的解法:用一个arraylist存以某一个element结尾的最长sequence 别人的好方法,大体思路一样,只是不存arraylist, 而用一个preIndex数组存以某一个elem
阅读全文
Leetcode: Water and Jug Problem && Summary: GCD求法(辗转相除法 or Euclidean algorithm)
摘要:参考:https://discuss.leetcode.com/topic/49238/math-solution-java-solution The basic idea is to use the property of Bézout's identity and check if z is a
阅读全文
Leetcode: Sum of Two Integers && Summary: Bit Manipulation
摘要:转自https://discuss.leetcode.com/topic/49771/java-simple-easy-understand-solution-with-explanation/2,注意里面对于减法的讲解 have been confused about bit manipulati
阅读全文
Leetcode: Max Sum of Rectangle No Larger Than K
摘要:Reference: https://discuss.leetcode.com/topic/48875/accepted-c-codes-with-explanation-and-references/2 The naive solution is brute-force, which is O((
阅读全文
Leetcode: Count Numbers with Unique Digits
摘要:Analysis: A number of unique digits is a number which is a combination of unrepeated digits. So, we can calculate the total number. for number with n
阅读全文
Leetcode: Design Twitter
摘要:注意需要maintain一个time stamp, 每次postTweet时++。还有要注意unfollow的时候,不能unfollow自己 关于PriorityQueue还看到这一种写法,挺简单的: 1 Set<Integer> users = userMap.get(userId).follow
阅读全文
Leetcode: Nth Digit
摘要:1-9 : count:9 * len:1 10-99: count:90 * len:2 100-999: count:900 * len:3 1000-9999: count: 9000 * len:4 maintain a count, len, start
阅读全文
Leetcode: Russian Doll Envelopes
摘要:DP 解法, Time Complexity: O(N^2) sort based on width or height, anyone is ok. After the sorting, for each envelope, those envelopes which can fit into t
阅读全文
Leetcode: Data Stream as Disjoint Intervals && Summary of TreeMap
摘要:TreeMap 解法: Use TreeMap to easily find the lower and higher keys, the key is the start of the interval.Merge the lower and higher intervals when neces
阅读全文
Leetcode: Intersection of Two Arrays II
摘要:用HashMap Follow Up 1: 用two pointer解,可以省存成HashMap Follow Up 2: 用在长的数组里面binary Search可解 Follow Up 3: What if elements of nums2 are stored on disk, and t
阅读全文
Leetcode: Intersection of Two Arrays
摘要:Use two hash sets Time complexity: O(n) 注意Set<Integer> set = new HashSet<>(); 第二个泛型可以不写出 Iterator iter = set2.iterator(); for (int i=0; i<res.length;
阅读全文
Leetcode: Top K Frequent Elements
摘要:很像majority element III, 但是那道题有O(k) extra space的限制,这里没有。有任意extra space, 同时知道elem range情况下,bucket sort最节省时间 语法上注意第5行,建立一个ArrayList的array,后面没有泛型generic L
阅读全文
Leetcode: Integer Break
摘要:O(N^2)解法: DP dp[i] represent the maximum product of breaking up integer i O(N)解法: the best factor is 3. we keep breaking n into 3's until n gets small
阅读全文
Leetcode: Flatten Nested List Iterator
摘要:非常精巧地使用stack。push all the nestedList into the stack from back to front,so when we pop the stack, it returns the very first element 执行hasNext()的时候,如果pe
阅读全文
Leetcode: Reverse String
摘要:After the API changes 注意没有append(0, s.charAt(i)), 是sb.insert(0, charAt(i));
阅读全文
Leetcode: Power of Four
摘要:it's easy to find that power of 4 numbers have those 3 common features. First,greater than 0. Second,only have one '1' bit in their binary notation,so
阅读全文
Leetcode: House Robber III
摘要:https://discuss.leetcode.com/topic/39834/step-by-step-tackling-of-the-problem rob(root) which will return the maximum amount of money that we can rob
阅读全文
Leetcode: Palindrome Pairs
摘要:Naive Solution: Time: O(n^2*k) with n the total number of words in the "words" array and k the average length of each word: check each combination see
阅读全文
Leetcode: Self Crossing
摘要:4th line may cross with 1st line, and so on: 5th with 2nd, ...etc 5th line may cross with 1st line, and so on: 6th with 2nd, ...etc 6th line also may
阅读全文
Leetcode: Increasing Triplet Subsequence
摘要:Naive Solution: use DP, Time O(N^2), Space O(N) dp[i] represents the length of longest increasing subsequence till i including element i in nums array
阅读全文