Skip to content

Instantly share code, notes, and snippets.

@balloonio
Last active May 11, 2020 01:28
Show Gist options
  • Save balloonio/ae4664fbeeac082ef6ff4d2582076f49 to your computer and use it in GitHub Desktop.
Save balloonio/ae4664fbeeac082ef6ff4d2582076f49 to your computer and use it in GitHub Desktop.
Some notes for leetcode

Palindrome

  • dynamic programing with f[i][j] = whether s[i:j+1] aka s[i]..s[j] is palindrome or its length
  • enumerate pivots, and expand from each pivot

Subsequence

  • if using dynamic programing, in general f[i][j] = f[i+1][j-1] || f[i][j-1] || f[i+1]f[j] (meaning you can either take both end, left only, or right only

DP

  • be careful with the initial state at the time of initialization
  • be careful to explicitly set the default state or negative state while iterating through f

2D Board Traversal

  • Two level nested loop; start a BFS each time when it's an island. Board level visited set. Boundary check on nx ny. 200

Binary Tree DFS

Four cases always to consider: 1) node == None 2) leaf node (this one is not necessary but safe to consider) 3) missing left branch 4) missing right branch

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment