Skip to content
Priyanka Khire edited this page May 14, 2024 · 65 revisions

Logo


Basic Algorithms

Problem Solution with time and space complexity Resources
DFS
Solution Time Complexity Space Complexity
Graph DFS O(n) O(n)
To keep track
of visited nodes
Graph and Tree DFS O(n) O(n)
To keep track
of visited nodes
YouTube Video
BFS
Solution Time Complexity Space Complexity
Graph BFS O(n) O(n)
To keep track
of visited nodes
Graph and Tree BFS O(n) O(n)
To keep track
of visited nodes
YouTube Video
Topological Sort
Solution Time Complexity Space Complexity
TopologicalSort.py
Topological Sort.py
Topological Sort DFS.py
Topological Sort
Using Matrix Graph.py
O(MN^2) Total number
of nodes
The provided approaches
only work if the graph is
directed and acyclic.
YouTube Video
Heap Sort
Solution Time Complexity Space Complexity

Arrays

Arrays

Problem Solution with time and space complexity Resources
Two Sum
Solution Time Complexity Space Complexity
One pass HashTable O(n) O(n)
Two pass HashTable
and Two Pointers approach
HashTable Approach: O(2n)
Two Pointer: O(n log n)
HashTable Approach: O(n)
Two Pointer: O(1)
Resources
Group Anagrams
Solution Time Complexity Space Complexity
Pt1
Pt2
Sorting and character
counting approach
Sorting: O(KN log N)
Character Counting: O(KN)
K = total words
N = length of longest word
Sorting: O(K)
Character Counting: O(K)
Resources
Merge Sorted Arrays
Solution Time Complexity Space Complexity
Merge Sorted Arrays.py O(n) Length of first Array
+
Length of second array
Remove Element
Solution Time Complexity Space Complexity
Remove Element Pt1.py O(n) O(1)
Remove Element Pt2.py O(n) O(1)
Contains Duplicate
Solution Time Complexity Space Complexity
Pt1
Pt2 HashTable Approach: O(n)
Sorting Approach: O(n log n)
Hash Approach: O(n)
Sorting Approach: O(1)
Resources
Valid Anagram
Solution Time Complexity Space Complexity
Pt1
Pt2 Sorting approach: O(n log n)
HashTable approach: O(n)
Sorting approach: O(n)
HashTable approach: O(26)
Resources
Top K Frequent Elements
Solution Time Complexity Space Complexity
Heap approach
Heap approach pt2
Resources
Product of Array Except Self
Solution Time Complexity Space Complexity
Solution link
Solution link O(3n) O(2n)
Resources
Pascal's Triangle
Solution Time Complexity Space Complexity
Pt1
Pt2
Pt3 O(n) O(n)
Resources
Pascal's Triangle II
Solution Time Complexity Space Complexity
Pt1
Pt2 Exponential O(n)
Resources

Backtracking

Backtracking

Problem Solution with time and space complexity Resources
Generate all permutations of a string
Solution Time Complexity Space Complexity
Python Solutions Factorial Time Number of
Unique characters
in the string
X
Total number of
characters
in the string
Python Solution Pt2 Factorial Time Number of
Unique characters
in the string
X
Total number of
characters
in the string
YouTube Video
Given an array with unique elements, generate all sub arrays from that array
Solution Time Complexity Space Complexity
Array Permutations.py Exponential O(n)
Word Search
Solution Time Complexity Space Complexity
Word Search.py
Word Search Speed Coding.py
Word Search Pt2.py O(MN) O(MN)
when we get
next moves
Don't forget to mark cells visited
Word Search II
Solution Time Complexity Space Complexity
Word Search II.py
Word Search II pt2.py
Word Search II Pt3.py O(total number of words
X
MN)
Total number
of words
Word Search II Speed Coding.py
Use Tries if you want to reduce the time complexity
Vowels of All Substrings
Solution Time Complexity Space Complexity
Solution link O(n^2) O(1)

Graph

  • What are some ways of representing graph in code ?

    • Matrix
    A B C
    A 0 1 0
    B 0 0 1
    C 1 0 0
    • Adjacency List
      Using Hash Map: {
      A:B,
      B:C,
      C:A }
      Visit here to understand better
    • Linked List
Problem Solution with time and space complexity Resources
Course Schedule
Solution Time Complexity Space Complexity
Using graph
represented as matrix
O(n^2) O(n^2)
Where n
is number
of courses
Solution link
Use Topological Sorting

Hash Table

Problem Solution with time and space complexity Resources
Logger Rate Limiter
Solution Time Complexity Space Complexity
Logger Rate Limiter.py O(1) O(n)
n is number of messages
Subdomain Visit Count
Solution Time Complexity Space Complexity
Speed coding
Midnight Coding O(total domains
*
total subdomains)
O(n)
Resources

Intervals

Problem Solution with time and space complexity Resources
Minimum Number of
Taps to Open to
Water a Garden
Solution Time Complexity Space Complexity
Recursive approach Exponential O(n)
Resources
Minimum number of intervals to cover the target interval
Solution Time Complexity Space Complexity
Minimum number
of intervals.py
O(n log n) O(1)
Resources
Meeting Rooms II
Solution Time Complexity Space Complexity
Pt1
Pt2
Pt3 O(n log n) O(n/2)
Use Min Heap

Math

Problem Solution with time and space complexity Resources
Print all prime
factors of a number
Solution Time Complexity Space Complexity
Print All
Prime Factors.py
O(sqrt(n)) O(1)
Resources

Matrix

Problem Solution with time and space complexity Resources
Valid Sudoku
Solution Time Complexity Space Complexity
Valid Sudoku.Py O(n^3) O(1)
Valid Sudoku Pt2.Py O(n^3) O(n)
Number of Islands
Solution Time Complexity Space Complexity
Number Of Islands.py
Number of Islands
(LeetCode Accepted).py
Number of Islands pt3.py
Number of Islands pt4.py
Number of Islands Pt5.py O(mn* number of islands) ~= O(mn) O(1)
Resources
Max Area of Island
Solution Time Complexity Space Complexity
Max Area of Island.py Time complexity Space complexity
Max Area of Island pt2.py Time complexity Space complexity
Max Area of Island Pt3.py O(mn) O(1)
Resources

Sorting

Problem Solution with time and space complexity Resources
Rank Teams by Votes
Solution Time Complexity Space Complexity
Solution link O(n) O(n)
Took help of chatGPT and algo monster

Stack

stack

Problem Solution with time and space complexity Resources
Minimum Remove to Make Valid Parentheses
Solution Time Complexity Space Complexity
Minimum Remove
to Make Valid
Parentheses.py
Minimum Remove
to Make Valid
Parentheses pt2.py
Minimum Remove
to Make Valid
Parentheses Pt3.py
O(n) O(n)
Resources

Tree

Tree

Problem Solution with time and space complexity Resources
Maximum Average Subtree
Solution Time Complexity Space Complexity
Maximum Average Subtree.py Traverses the
whole tree once
O(1)
Resources
Serialize and Deserialize N-ary Tree
Solution Time Complexity Space Complexity
Pt1
Pt2
Pt3 Serialize: O(n)
Deserialize: O(n)
Serialize: O(2n) at max
Deserialize:O(n)
Geeks for Geeks
Construct Binary Tree
from Preorder and
Inorder Traversal
Solution Time Complexity Space Complexity
Solution link O(n) O(n)
If you count
tree nodes
Resources

Trie

Problem Solution with time and space complexity Resources
Implement Trie (Prefix Tree)
Solution Time Complexity Space Complexity
Trie.py
Trie using Hash Map
Trie Pt2 O(word length) O(word length)
Leetcode editorial

Two Pointers

Problem Solution with time and space complexity Resources
Valid Palindrome
Solution Time Complexity Space Complexity
Valid Palindrome Pt2 O(n/2) O(1)
Is Subsequence
Solution Time Complexity Space Complexity
Is Subsequence.py O(long string) O(1)