Coding Symmary

Coding Summary by Keywords and Type

Original from posts ###Permutation & Combination Type




####总结 Permutations II, Combinations, Combinations Sum IISubset II都是DFS, 区别在于:

  1. res放入ret的条件不一样
    • Permu - len(res) = len(S)
    • Combin - len(res) == k
    • Combin Sum - target == 0
    • Subsets - 只要生成新的res就要存一次
      前三种题存过结果只后程序应该return
  2. 循环内call recursion时的输入变量不一样
    • Permu - permu_helper(num[:i] + num[i+i:], res, ret)(除了S[i])
    • Combin - comb_helper(i+1, n, k, res, ret)(S[i+1:])
    • Combin Sum - comb_sum_helper(num[i:], target - n, res, ret)(S[i:])
    • Subsets - sub_helper(S[i+1:], res, ret)(S[i+1:])
      S[i+1:]``决定了res内是不会有重复项的(除非S本身就有重复),S[i:]```让当前元素可以重复使用

#####Note

#####复杂度O(n)




###Tree Traversal



###Binary Search Tree

###类Tree(以tree作为Data Structure的题目)

###Array(意义不大)

###List(意义不大)

######Dup with tree

###Matrix

###Play With Math


###Dynamic Programming





##From NC DP Class

###模板

###Clues

  1. Cannot sort, or swap
  2. Satisfy:
    • Find a maximum/minimum result
    • Decide whether something is possible or not
    • Count all possible solutions(Doesn’t care about solution details, only care about the count or possibility)

###Types of DP

####1. Matrix DP 20% (Triangle, Unique Path, …)

#####Triangle

#####Unique Path | Unique Path II

#####Minimum Path Sum


####2. One Sequence DP 40%

######Climbing Stairs

######Jump Game | Jump Game II

######Palindrome Partitioning II

######Word Break

######Longest Increasing Subsequence 最长上升子序列 (Not in Leetcode)

######Decode Ways


####3. Two Sequences DP 40%

######Longest Common Subsequence (Not in Leetcode)

######Longest Common Substring (Not in Leetcode)

######Edit Distance

######Distinct Subsequence(需要再领会一下)

######Interleaving String


####4. Interval DP

######Merge Stone 石子归并


####5. Tree DP ######Binary Tree Maximum Path Sum


####6. States Compressing DP(不需要知道) ####7. Knapsack

###总结

####复杂度 直接看循环嵌套层数

####关于取dp[N]还是dp[N-1]还有dp[N]-1

  1. 首先先分析dp维度,Matrix和Two Sequence dp都是二维,One Sequence是一维
  2. Matrix dp一般都是初始(0,0)跳到(M-1,N-1)所以取的是dp[M-1][N-1]
  3. 如果dp[i]或者dp[i][j]表示前i个什么的时候,需要以N/MN作为结尾,主要原因是这种情况下前0个字符串是没有意义的,至少从1开始,所以取dp的时候也是从dp[1]开始才有意义,所以dp[i]的含义是前i-1个东西的性质,而dp[0] or dp[0][0]需要强制赋值
  4. 至于dp[N] - 1纯粹是因为Palindrome题目比较特殊,实际我们算的cut-1才是结果

####已知dp问题然后回问满足dp条件的结果 一般这种情况就是根据已知的dp matrix和结论,从最后开始往前回溯,满足的就挑进去,不满足的就不放来解决.


Array Two Pointers

The basic template of doing ‘Sums’

####Cannot Classify(记住思路)

##From Class ###Binary Search

######From zz

####Three steps reverse

def binary_search(target, A):
    if len(A) == 0:
        return -1
    start = 0
    end = len(A) - 1
    while start + 1 < end:
        mid = start + (end - start) / 2    # Avoid stack overflow
        if A[mid] == target:
            end = mid
        elif A[mid] > target:
            start = mid
        else:
            end = mid
    # Check start and end
    if A[start] == target:
        return start
    if A[end] == target:
        return end
    return -1

###Divide & Conquer (Most BT Problem)

This will require extra space

把一个任务划分成几个小任务 一般来说分治是有return的,但是Recursion一般是没有的

Binary Tree Level Order Traversal 3 ways

Check BFS and DFS template

####Not in Leetcode

###DFS 主要想法是先搜索到不能再底层然后再往上走 #####复杂度问题

#####Word Ladder II

  1. 最短的是什么
  2. 所有最短的是啥

  3. 对所有点进行分层BFS
  4. 对DFS层进行搜索

###Graph

#####Clone Graph

#####拓扑排序Topological sorting 主要是入度为零

Q.offer(....)
while (!Q.empty()) {
    Node head = Q.poll();
    for (int i = 0; i < head.neighbors.size(); i++) {
        inDegree[neighbor] --;
        if ( .. == 0) {
            Q.offer(.xxx)
        }
    }
}

###DFS vs BFS #####DFS - O(2^n), O(n!)

  1. Find all solutions
  2. Permutations / Subsets

#####BFS - O(m) O(n)

  1. Graph Traversal(每个点都遍历一次)
  2. Find shorted path in a simple graph

###Data Structure

####Stack implement

####Heap #####Median Number(应该是CC150)里的题

#####Majority Number 去想关于数据结构的题目的时候, 只需要考虑数据结构里处理的次数就行了

##Definetly Redo

##记忆思考方式

##Need Understand

##New

##Some Note

  1. 一定要看清题,比如这次就被问了find all palindrome,但是理解成palindrome partitioning了,所以错了
  2. 再仔细确认下怎么算recursion的big O