Complete-Preparation

πŸŽ‰ One-stop destination for all your technical interview Preparation πŸŽ‰

View the Project on GitHub

Leetcode Problem 001-099

1. Two Sum 🌟

O(N^2) Time Constant space

O(N) Time and O(N) space


2. Add Two Numbers 🌟🌟

This question only have optimal solution


3. Longest Substring Without Repeating Characters 🌟🌟

O(N^3) Time, O(N) space

O(N^2) Time O(N) space, Sliding window

O(N) Time O(N) space, Sliding window


8. String to Integer (atoi) 🌟🌟

Solution

We only need to handle four cases:


11. Container With Most Water 🌟🌟

Brute force (TLE)

Two pointer approach


12. Integer to Roman 🌟🌟

Greedy solution

Fun solution


15. 3Sum 🌟🌟

O(N^3 Log m) Brute force

    for(i=0,n-1)
        for(j=i+1,n-1)
            for(k=j+1,n-1)
                a+b+c==0, cnt++

O(N^2 Log M) Time and O(N)+O(M) Space

Two pointers.


18. 4Sum 🌟🌟

O(N^4) brute force

Sort + 3ptr + BinarySearch (int overflow)

TIP:

Sort + 2ptr + BinarySearch


19. Remove Nth Node From End of List 🌟🌟

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

O(N) Time and O(1) Space Complexity, two pointers


20. Valid Parentheses 🌟

O(N) Time and O(N) Space, straightforward solution

Some slight simplification


21. Merge Two Sorted Lists 🌟

O(N+M) Time and O(N+M) space

O(N+M) Time and O(1) space, in-place

O(N+M) Time and O(1) Space, Recursive


22. Generate Parentheses 🌟🌟

Backtracking


24. Swap Nodes in Pairs 🌟🌟

By swapping values

By swapping Nodes


26. Remove Duplicates from Sorted Array 🌟

Naive Solution

2 pointer solution


31. Next Permutation 🌟🌟

Brute Force

O(N) Time solution.

Inbuilt next_permutation


35. Search Insert Position

O(log N) Time solution


36. Valid Sudoku 🌟🌟

Implementation


37. Sudoku Solver 🌟🌟🌟

Backtracking (16ms-AC)


39. Combination Sum 🌟🌟


42. Trapping Rain Water 🌟🌟🌟

Brute force

PrefixSum Optimized

2-pointer


46. Permutations 🌟🌟

Backtracking


48. Rotate Image 🌟🌟

O(N^2) Time and O(N^2) space Solution

O(N^2) Time O(1) Space Solution

 clockwise rotate
 first reverse up to down, then swap the symmetry
 1 2 3     7 8 9     7 4 1
 4 5 6  => 4 5 6  => 8 5 2
 7 8 9     1 2 3     9 6 3
Anticlockwise rotate
 first swap the symmetry, then reverse up to down
 1 2 3     1 4 7     3 6 9
 4 5 6  => 2 5 8  => 2 5 8
 7 8 9     3 6 9     1 4 7

MUST READ:


49. Group Anagrams 🌟🌟

Hash map + sorting


50. Pow(x, n) 🌟🌟

O(N) Time Brute force

O(log2_N) optimized

Recursive


51. N-Queens 🌟🌟🌟

Brute force backtracking (8ms-AC)

3 vectors optimized backtracking (3ms-AC)

1 vector optimized backtracking (7ms-AC)


52. N-Queens II 🌟🌟🌟

Same problem as 51. N-Queens, Just need to change result vector with count variable.

For explanation refer previous question nQueens-1


53. Maximum Subarray 🌟

O(N) time constant space(DP)


56. Merge Intervals 🌟🌟

O(N^2) Time Solution

O(NlogN) Time O(N) Space Solution

O(NlogN) Time O(1) Space Solution


61. Rotate List 🌟🌟

Brute force

Optimized


62. Unique Paths 🌟🌟

Recursive Solution (TLE)

Recursive + Memoization

Tabulation Solution

Combinatorics Solution

READ


63. Unique Paths II 🌟🌟

Recursion (TLE)

Memoization (AC)

Tabulation (AC)


64. Minimum Path Sum 🌟🌟

Recursion (TLE)

Memoization (AC)

Tabulation (AC)


70. Climbing Stairs 🌟

Dynamic Programming


71. Simplify Path 🌟🌟

Using stack


73. Set Matrix Zeroes 🌟🌟

Brute Force

O(m+n) space optimization

O(1) Space Optimization


74. Search a 2D Matrix 🌟🌟

O(N*M) Time and constant space solution

O(N*logM) Time and constant space solution

O(log(N*M)) Time and O(1) space


75. Sort Colors 🌟🌟

O(N log N) Time Complexity with sort function.

O(N)+O(N) Time using counting sort.

O(N) Time, 3 Pointers, dutch national flag algorithm.


76. Minimum Window Substring 🌟🌟🌟

Sliding Window + Hashmap


77. Combinations 🌟🌟

Backtracking: solution-1 (28ms-AC)

Solution-2 (10ms-AC)


78. Subsets 🌟🌟

Recursive solution


82. Remove Duplicates from Sorted List II 🌟🌟

Solution


83. Remove Duplicates from Sorted List 🌟

O(N) Time and O(1) Space


86. Partition List 🌟🌟


88. Merge Sorted Array 🌟

O(M+N) Time and O(M+N) space

O(M*N) without using extra Space

O(M+N) Time and O(1) Space

Using GAP algorithm


91. Decode Ways 🌟🌟

Recursive solution (TLE)

Memoization (AC)

Tabulation (AC)


94. Binary Tree Inorder Traversal 🌟

O(N) Time and O(N) auxillary space, recursive

O(N) Time and O(N) Space, iterative

O(N) Time and O(1) Space, Morris Traversal

Soon…


96. Unique Binary Search Trees 🌟🌟

MUST READ πŸ‘‡:

  1. Brute-Force                 ( Time: O(3^N), Space: O(N) )
  2. Dynamic Programming - Memoization    ( Time: O(N^2), Space: O(N) )
  3. Dynamic Programming - Tabulation     ( Time: O(N^2), Space: O(N) )
  4. Catalan Numbers             ( Time: O(N), Space: O(1) )
  5. Catalan Numbers (2nd Approach)      ( Time: O(N), Space: O(1) )

DP Solution(Tabulation)


97. Interleaving String 🌟🌟


98. Validate Binary Search Tree 🌟🌟

O(N) Time and O(N) space

O(N) Time solution


99. Recover Binary Search Tree 🌟🌟

Recursive using inorder traversal