coding_patterns_brief Flashcards
Sliding Window
Handle input data in a specific window size
Islands (Matrix Traversal) /Matrices
Matrix Traversing (2D Array)
Two Pointers
iterate input data , pointers move in opposite direction
Fast & Slow Pointers
Two Pointers to traverse the input data at different speeds
Merge Intervals
Overlaping Intervals
Cyclic Sort
Solve array problems where the input datra lies within a fixed range
In-place Reversal of a LinkedList
reverse a linkedlist without using extra memory
Tree Breadth-First Search
traverse a tree, each level fully
Tree Depth First Search
traverse a tree, going down child nodes until the end and then go back up
Two Heaps
In many problems, we are given a set of elements that can be divided into two parts. We are interested in knowing the smallest element in one part and the biggest element in the other part. Uses a Min-Heap to find the smallest element and a Max Heap to find the biggest element
Subsets
Permutations or Combinations of a set elements
Modified Binary Search
Search a sorted set of elements efficiently
Bitwise XOR / Bitwise Manipulation
Bit manipulations
Top ‘K’ Elements
Find the top/smallest/frequent ‘K’ elements in a set
K-way Merge
Solve problems that involve a list of sorted array
Topological Sort
Find a linear ordering of elements that have dependencies on each other
0/1 Knapsack
Used to solve optimization problems. Use this technique to select elements that give maximum profit from a given set with limitation on capacity and that each element can only be picked once
Fibonacci Numbers
Solve problems that follow the fibonacci numbers
Palindromic Subsequence
This techniew is used to solve optimization problems related to palindromic sequences or strings
Longest Common Substring
Use this technique to find the optimal part of a string/sequence or set of strings/sequences
Stacks
Use this technique if you want to get elements out in the reverse order in which you inserted them (LIFO)
Hash Maps
Use this when repeated fast access to data during the exection of algorithm. If you need to store the releationship between two sets of data in order to compute the required result. This is achieved through the mechanism of a key-value pair, where we stored the hashed version of the key to enable fast look-ups.
Knowing What To Track
problem seems hard with a naive approach of at least
O(n2) time complexity, if not worse. Yet, on careful inspection, we realize that there are certain key characteristics of the input data that, if tracked, allow us to solve the problem far more efficiently
Generally speaking, this approach is worth trying when we are required to choose from a fixed set of possibilities: Yes / No, True / False, Valid / Invalid, Player 1 / Player 2.
Key characteristics could include the frequency of characters in a string, the pattern of generation of a sequence of permutations, or the implications of a given move in a game like tic-tac-toe or chess.
Greedy Techniques
if selecting a series of local optima allows us to construct or identify the globally optimum solution.