O(mklog(min(n, k))) time O(min(n, k)) space
373. Find K Pairs with Smallest Sums的followup
不要想复杂了!!!直接做m - 1遍就行了
利用这种原理还可以做并行的两两merge
1 | class Solution { |
并行merge版
跟23. Merge k Sorted Lists思路一样
1 | class Solution { |
O(mklog(min(n, k))) time O(min(n, k)) space
373. Find K Pairs with Smallest Sums的followup
不要想复杂了!!!直接做m - 1遍就行了
利用这种原理还可以做并行的两两merge
1 | class Solution { |
并行merge版
跟23. Merge k Sorted Lists思路一样
1 | class Solution { |
O(C) time O(log(max(m, n))) space
当一个binary tree来level order traverse
1 | class Solution { |
O(n) time O(logn) space
采用中序遍历思想,先确定链表长度,然后二分递归进行中序遍历
是108. Convert Sorted Array to Binary Search Tree的升级版
1 | /** |
1 | /** |
O(nlogn) time O(logn) space
1 | /** |
O(n) time O(n) space
先dfs扫一遍得到所有的node然后二分构造bst
1 | /** |
DSW O(n) time O(1) space
解法
先通过多次右旋变成right-skew的单链,再按照树高(以及子树高)多次左旋
1 | /** |
O(n) manacher’s algorithm不会
brute force O(n2) time O(1) space
跟5. Longest Palindromic Substring一样,直接数数即可
1 | class Solution { |
划分型dp O(n2) time O(n2) space
isPalin[i][j] = s[i] == s[j] && isPalin[i + 1][j - 1]
f[i][j] = f[i][j - 1] + f[i + 1][j] - f[i + 1][j - 1] + isPalin[i][j]
1 | class Solution { |
其实没有必要维护一个计数的矩阵,直接用一个cnt就可以了
1 | class Solution { |
dfs + memo O(mn) time O(mn) space
1 | class Solution { |
1 | /** |
1 | /** |
amortized O(1)
stack类似BST iterator
1 | /** |
1 | /** |
iterator版
1 | /** |
O(n) BFS
1 | /** |
stack O(n) time O(n) space
维护一个<字符+频数>的stack 累计连续频数 每次连续频数达到k就出栈 最后把所有字符按频数拼接即可
1 | class Solution { |
1 | class Solution { |