Coding Patterns

P1 : Sliding Window
Template:

  1. iterate
  2. constraint/pattern/K/sum(S)
  3. output

P2: Two Pointers

P3: Fast and Slow Pointers

P4: Merge Intervals

P13: Top 'K' elements

P10: Subsets

P5: Cyclic Sort

P9: Two Heaps

P6: In-place Reversal of a Linked List

P7: Tree - BFS

P11: Modified Binary Search

P8: Trees - DFS

Bit Manipulation

P14: K-way merge

P15: 0/1 Knapsack problem

click to edit

Graphs

P16.0: Topological Sort / BFS

  1. Course Scheduling Order (medium)
  1. Tasks Scheduling (medium)

click to edit

  1. Topological Sort (medium)

click to edit

  1. Course Scheduling (medium)

click to edit

  1. Tasks Scheduling Order (medium)

click to edit

  1. Alien Dictionary (Hard)

click to edit

  1. PC1: Reconstructing a Sequence (Hard)

Time Complexity: O(V + E)
Space Complexity: O(V + E)

  1. PC2: Minimum Height Tree (Hard)

Time Complexity: O(V + E)
Space Complexity: O(V + E)

  1. All Tasks Scheduling

Backtracking

Time Complexity: O(V! E)
Space Complexity: O(V!
E)

click to edit

P 16.1 DFS

Course Scheduling (P16.0)

Possible Bipartition

Number of connected components in an undirected graph

Minimum Height Tree (P16.0)

click to edit

click to edit

click to edit

click to edit

  1. Return from Recursive function

click to edit

click to edit

Single Root to Leaf Path and tracing back

  1. Deleting currentPath [-1]
  2. Return output from main function not recursive function
  1. All Paths for a Sum (medium)
  1. Sum of Path Numbers (medium)
  1. Binary Tree Path Sum (easy)
  1. All root-to-leaf paths

Algorithm

  1. Start DFS with the root of the tree.
  1. If the current node is not a leaf node, do two things:
  1. At every step, see if the current node being visited is a leaf node and if its value is equal to the given number ‘S’. If both these conditions are true, we have found the required root-to-leaf path, therefore return true.

If the current node is a leaf but its value is not equal to the given number ‘S’, return false.

2.2 Make two recursive calls for both the children of the current node with the new number calculated in the previous step.

2.1 Subtract the value of the current node from the given number to get a new sum => S = S - node.value

Time Complexity: O(N)
Space Complexity: O(N)

Time Complexity: O(N^2)
Space Complexity: O(V! * E)

Time Complexity: O(V! E)
Space Complexity: O(V!
E)

Time Complexity: O(V! E)
Space Complexity: O(V!
E)

Refer EIO Explanation

click to edit

Facts:
Total Root-to-leaf paths can't be more than Number of leaves

No. of leaves in Binary Tree:
(N+1)/2

Depth/height of Balanced Binary Tree:
O(log N)

Algorithm

G(U,V): U comes before V in the ordering.

  1. Source : Vertices with no incoming edges {i:[] for i in range(nodes)}
  2. Indegrees : Number of incoming edges {i:0 for i in range(nodes)}

Facts:

  1. Topological Sort works on no cycles ie. works on Directed Acyclic graph (DAG)

Types
Arrays: [1,2,3,5,8],
Strings: [4,7,9,12]

Dynamic Sliding Window

click to edit

Standard Sliding Window

click to edit

String_Standard SW

  • 5 step process

Array_Standard SW

  1. Smallest Subarray with a given sum
  • Array
  1. Longest substring with K distinct characters
  1. Fruits into Basket
    (Longest substring with at most 2 distinct characters)
  • Array
  1. Maximum Sum Subarray of Size 'K'
  1. PC1: Permutations in a String

Time Complexity: O(N)
Space Complexity: O(N)

Time Complexity: O(N)
Space Complexity: O(K)

Time Complexity: O(N)
Space Complexity: O(K)

Partial_Dynamic_SW

  1. Longest substring with same letters after replacement
  • String

Time Complexity: O(N)
Space Complexity: O(K)/O(1) 26 lowercase letters

  1. Longest substring with Ones after replacement
  • Array

Time Complexity: O(N)
Space Complexity: O(1) (no dict)

6.

  1. Average Subarray of Size 'K'

Time Complexity: O(N)
Space Complexity: O(N)

Time Complexity: O(N)
Space Complexity: O(1)

Time Complexity: O(N+K)
Space Complexity: O(K)

  1. String Anagrams

Time Complexity: O(N+K)
Space Complexity: O(N+K)

Coding Patterns

Reverse

Mirror Image

Two Pinters

Recursion

Sort

Binary Search

Linked List

Two Pointers

Recursion

  1. Smallest window containing substring

P12: Bitwise [XOR]

Blind 75

Sum of two integers

click to edit