随笔分类 - Leetcode
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
阅读全文
浙公网安备 33010602011771号