Intro

  • Distilled set of LeetCode problems for learning data structures and algorithms solved using Python 3.
  • Refer Python Tips and Tricks for an review of data structure concepts and their implementations with Python 3.
  • Note that recursive (top-down) implementations store call-stacks (and hence are not \(O(1)\) space complexity). For \(O(1)\) space complexity, the computations must be performed iteratively (bottom-up) without using call-stacks.
  • Here’s a list of the full problem set on LeetCode.

General Tips

  • These tips are from Sean Prasad’s LeetCode Patterns Github repo.
If input array is sorted, then
    - Binary search

If input array is not necessarily sorted, then
    - Sliding window
    - Two pointers

If asked for all permutations/combinations/subsets, then
    - Backtracking

If given a tree/graph/grid, then
    - DFS
    - BFS

If given a linked list then
    - Two pointers

If recursion is banned then
    - Stack

If must solve in-place then
    - Swap corresponding values
    - Store one or more different values in the same pointer

If asked for maximum/minimum subarray/subset/options then
    - Dynamic programming

If asked for top/least K items then
    - Heap

If asked for common strings then
    - Map
    - Trie

Else
    - Map/Set for O(1) time & O(n) space
    - Sort input for O(nlogn) time and O(1) space
    
- A subarray or substring will always be contiguous, but a subsequence need not be contiguous, i.e., subsequences are not required to occupy consecutive positions within the original sequences.
  • Check out LeetCode patterns sorted by companies/difficulty level/patterns here.

Patterns

Concepts

LeetCode Design Pattern Articles

Further Reading

References