摘要:
Exactly the same with Split Array Largest Sum. Binary search or DP Binary Search 阅读全文
摘要:
Store the words in the trie with reverse order, and check the query string from the end 阅读全文
摘要:
Complexity Analysis Time Complexity: O(M + N)O(M+N), where M, NM,N are the lengths of S and T respectively. Space Complexity: O(1)O(1). Time Complexit 阅读全文
只有注册用户登录后才能阅读该文。 阅读全文
摘要:
Greedy Use two pointers, one for source: i, one for target: j. While j scan through target, try to match each char of j in source by moving i. Count h 阅读全文
只有注册用户登录后才能阅读该文。 阅读全文
摘要:
Basic: Backtracking + pruning DP: refer to https://leetcode.com/problems/campus-bikes-ii/discuss/305218/DFS-%2B-Pruning-And-DP-Solution state : dp[i][ 阅读全文
摘要:
1. The final uniform character should be either A[0] or B[0] 2. A[0] could be at the top, or the bottom. Same applies to B[0] 3. If A[0] works, no nee 阅读全文
摘要:
DP: Extend palindrome: (better) 阅读全文
摘要:
Facebook / Eng tech lead Dec 30, 2018 68 Comments Facebook / Eng tech lead Facebook / Eng tech lead Dec 30, 2018 68 Comments New Year Gift to every fe 阅读全文
摘要:
Recursion: 阅读全文
摘要:
DP with a 2D array Time & space: O(m * n) (Skim through) memory optimization, referencing: https://leetcode.com/problems/longest-common-subsequence/di 阅读全文
摘要:
账号之间如果有公共email就连边,我觉得要用hashMap, 存(email, user) pair 如果当前user的某个email在hashMap里出现过,当时是user1, 就看user和user1是否connected,否的话就要union,根据Union Find Union Find 阅读全文
摘要:
Quick Sort Way: We can use quick sort technique to solve this. We represent nuts and bolts in character array for understanding the logic. Nuts repres 阅读全文
摘要:
跟Merge K sorted lists不同在于,从PQ里poll出来以后不知道下一个需要被加入PQ的是哪一个 所以需要写一个wrapper class 阅读全文
摘要:
方法可以是记录每次新的岛屿搜索的路径,left,right,up,down, 作为标志是否相同的key,存hashset 阅读全文
摘要:
从左从右各scan一次 阅读全文
摘要:
这道题应该是how to bipartite a graph Taken from GeeksforGeeks Following is a simple algorithm to find out whether a given graph is Birpartite or not using B 阅读全文
摘要:
Recursion: 返回的时候返回lca和depth,每个node如果有大于一个子节点的depth相同就返回这个node,如果有一个子节点depth更深就返回个子节点lca,这个o(n)就可以了 Iteration: tree的recursion换成iteration处理,一般用stack都能解决 阅读全文
摘要:
Based on bucket sort: Based on Quick Select 当然,还有的方法是维护一个size为k的最大堆 阅读全文
摘要:
Follow Up: 这个其实可以优化到O(n)时间。建从左端到每个下标的最大window数组,再建从右端到每个下标的最大window数组。再从左往右走一遍所有的size k window,将其和与上一步建的两个数组一起加起来。遍历一遍取最大值即可。 阅读全文
摘要:
BFS solution: 阅读全文
摘要:
1. 根据系统设计的Estimation of the amount of data we need to store for the next couple of years, 我们应需要6位Base62的char来encode 2. assume 避免地址爆炸,相同的longUrl得到相同的sh 阅读全文
摘要:
类似Ones and Zeroes Dp[i][j][k] = Max(dp[i-5][j][k] + 5*10, dp[i][j-5][k] + 5*5, dp[i][j][k-1] + 1*25) reduce了dimension,其实还有一个维度是前k个格子,直接用倒序省掉了这个维度 阅读全文
摘要:
解法就是在子树里面找最大的两个(或一个,如果只有一个子树的话)高度加起来。 对于每一个treenode, 维护它的最高的高度和第二高的高度,经过该点的最大路径就是: 最高高度+第二高高度,然后return 最高高度 阅读全文
摘要:
有个getFriend() API, 让你推荐你的朋友的朋友做你的朋友,当然这个新朋友不能是你原来的老朋友 1 package fb; 2 3 import java.util.*; 4 5 public class ReferFriends { 6 public List recommendation(String name) { 7 List... 阅读全文
摘要:
1. two pointers 2. hashmap 3. 如果没有额外空间,如果一个很大,一个很小,适合scan小的,并且在大的里面做binary search 阅读全文
摘要:
分析: 如果是binary search找每个char的上下界,worst case要找n次,时间复杂度O(nlogn) 所以考虑每次比较start point和start point + 2^n位置上的数,假如一样就continue,不一样就在区间里面binary search找上界,这样wors 阅读全文
摘要:
Each time, first add the direction to the path, and then go with that direction, checking for hole along the way. When hit a wall, try to turn, and go 阅读全文
摘要:
Solution of The Maze: https://discuss.leetcode.com/topic/77471/easy-understanding-java-bfs-solutionSolution of The Maze III: https://discuss.leetcode. 阅读全文
摘要:
Search in the four possible directions when coming to a stopping point (i.e. a new starting point). Keep track of places that you already started at i 阅读全文
摘要:
未研究: The idea is to keep a window [l, h] that contains at most k zero The following solution does not handle follow-up, because nums[l] will need to a 阅读全文
摘要:
还没研究: For example, given IDIIDD we start with sorted sequence 1234567Then for each k continuous D starting at index i we need to reverse [i, i+k] port 阅读全文
摘要:
简单说来PriceUpdate就是添加新的(timestamp, price), Correction是改之前的(timestamp, price), 求实现当前high(), low(), last() LZ是用的Heap + HashMap, 特别问了时间复杂度(我猜到他想考heap的remov 阅读全文
摘要:
这道题若能发现inorder traversal each node的顺序其实就是column number递增的顺序,那么就成功了一大半 维护一个global variable,colNum, 做inorder traversal 然后level order 一层一层打印出来 阅读全文
摘要:
虽然我觉得这道题用set倒着来就可以解决,但是看到一个Trie的做法也很不错 这里TrieNode.val不再是char, 而是String. children array也变成了Map<String, TrieNode> 我的做法: 阅读全文
摘要:
Follow up: 第一步完成基础上,把这个new value插入BST中,一边插入一边记录沿途的大于value的root node(即走了该root node的left child),作为inorder successor. 找到inorder successor之后,通过其prev指针找到in 阅读全文
摘要:
这是一个简单版本of LC 424 Longest Repeating Character Replacement 又是Window, 又是Two Pointers Window还是采用每次都try to update left使得window valid, 每次都检查最大window 阅读全文
摘要:
For instance, give row = 4, col = 5, print matrix in zigzag order like: [1, 8, 9, 16, 17] [2, 7, 10, 15, 18] [3, 6, 11, 14, 19] [4, 5, 12, 13, 20] 1 package GooglePhone; 2 3 import java.util... 阅读全文
摘要:
O(N) solution: 两个interval overlap条件: interval1.end >= interval2.start && interval1.start <= interval2.end 问:一个query数在不在已经加入的区间? 如果interval是sorted, 就可以 阅读全文