Skip to content

Latest commit

Β 

History

29 Commits

Folders and files

NameName
Last commit message
Last commit date
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

πŸš€ NeetCode 150: Complete Solutions in C & C++

#test 6

Completed 150/150 C and C++ LeetCode MIT License Maintained by baadaldev


πŸ“– About This Repository

This repository contains clean, modular, and time/space optimal solutions for all 150 problems from the standard NeetCode 150 roadmap, implemented in both C (C99/C11) and modern C++ (C++17/20).

Every problem folder contains:

  1. solution.c β€” Memory-efficient, standard C implementation.
  2. solution.cpp β€” Modern, idiomatic C++ with STL containers (vector, unordered_map, priority_queue, etc.).
  3. README.md β€” LeetCode reference, complexity analysis, and approach explanation.

πŸ“Š NeetCode 150 Roadmap Breakdown

# Topic Problems Status
1 Arrays & Hashing 9 βœ… Completed
2 Two Pointers 5 βœ… Completed
3 Sliding Window 6 βœ… Completed
4 Stack 7 βœ… Completed
5 Binary Search 7 βœ… Completed
6 Linked List 11 βœ… Completed
7 Trees 15 βœ… Completed
8 Tries 3 βœ… Completed
9 Heap / Priority Queue 7 βœ… Completed
10 Backtracking 9 βœ… Completed
11 Graphs 13 βœ… Completed
12 Advanced Graphs 6 βœ… Completed
13 1-D Dynamic Programming 12 βœ… Completed
14 2-D Dynamic Programming 11 βœ… Completed
15 Greedy 8 βœ… Completed
16 Intervals 6 βœ… Completed
17 Math & Geometry 8 βœ… Completed
18 Bit Manipulation 7 βœ… Completed
TOTAL 150 / 150 100% COMPLETE

πŸ“‘ Complete Problem List & Solutions

# Topic Problem LeetCode Difficulty C Solution C++ Solution Time Space
1 Arrays & Hashing Contains Duplicate #217 🟒 Easy C C++ O(n) O(n)
2 Arrays & Hashing Valid Anagram #242 🟒 Easy C C++ O(n) O(1)
3 Arrays & Hashing Two Sum #1 🟒 Easy C C++ O(n) O(n)
4 Arrays & Hashing Group Anagrams #49 🟑 Medium C C++ O(n * k log k) O(n * k)
5 Arrays & Hashing Top K Frequent Elements #347 🟑 Medium C C++ O(n) O(n)
6 Arrays & Hashing Product of Array Except Self #238 🟑 Medium C C++ O(n) O(1)
7 Arrays & Hashing Valid Sudoku #36 🟑 Medium C C++ O(1) O(1)
8 Arrays & Hashing Encode and Decode Strings #271 🟑 Medium C C++ O(n) O(n)
9 Arrays & Hashing Longest Consecutive Sequence #128 🟑 Medium C C++ O(n) O(n)
10 Two Pointers Valid Palindrome #125 🟒 Easy C C++ O(n) O(1)
11 Two Pointers Two Sum II - Input Array Is Sorted #167 🟑 Medium C C++ O(n) O(1)
12 Two Pointers 3Sum #15 🟑 Medium C C++ O(n^2) O(1) auxiliary
13 Two Pointers Container With Most Water #11 🟑 Medium C C++ O(n) O(1)
14 Two Pointers Trapping Rain Water #42 πŸ”΄ Hard C C++ O(n) O(1)
15 Sliding Window Best Time to Buy and Sell Stock #121 🟒 Easy C C++ O(n) O(1)
16 Sliding Window Longest Substring Without Repeating Characters #3 🟑 Medium C C++ O(n) O(1)
17 Sliding Window Longest Repeating Character Replacement #424 🟑 Medium C C++ O(n) O(1)
18 Sliding Window Permutation in String #567 🟑 Medium C C++ O(n) O(1)
19 Sliding Window Minimum Window Substring #76 πŸ”΄ Hard C C++ O(n + m) O(1)
20 Sliding Window Sliding Window Maximum #239 πŸ”΄ Hard C C++ O(n) O(k)
21 Stack Valid Parentheses #20 🟒 Easy C C++ O(n) O(n)
22 Stack Min Stack #155 🟑 Medium C C++ O(1) all ops O(n)
23 Stack Evaluate Reverse Polish Notation #150 🟑 Medium C C++ O(n) O(n)
24 Stack Generate Parentheses #22 🟑 Medium C C++ O(4^n / sqrt(n)) O(n)
25 Stack Daily Temperatures #739 🟑 Medium C C++ O(n) O(n)
26 Stack Car Fleet #853 🟑 Medium C C++ O(n log n) O(n)
27 Stack Largest Rectangle in Histogram #84 πŸ”΄ Hard C C++ O(n) O(n)
28 Binary Search Binary Search #704 🟒 Easy C C++ O(log n) O(1)
29 Binary Search Search a 2D Matrix #74 🟑 Medium C C++ O(log(m * n)) O(1)
30 Binary Search Koko Eating Bananas #875 🟑 Medium C C++ O(n log(max(P))) O(1)
31 Binary Search Find Minimum in Rotated Sorted Array #153 🟑 Medium C C++ O(log n) O(1)
32 Binary Search Search in Rotated Sorted Array #33 🟑 Medium C C++ O(log n) O(1)
33 Binary Search Time Based Key-Value Store #981 🟑 Medium C C++ Set: O(1), Get: O(log n) O(n)
34 Binary Search Median of Two Sorted Arrays #4 πŸ”΄ Hard C C++ O(log(min(m, n))) O(1)
35 Linked List Reverse Linked List #206 🟒 Easy C C++ O(n) O(1)
36 Linked List Merge Two Sorted Lists #21 🟒 Easy C C++ O(n + m) O(1)
37 Linked List Reorder List #143 🟑 Medium C C++ O(n) O(1)
38 Linked List Remove Nth Node From End of List #19 🟑 Medium C C++ O(n) O(1)
39 Linked List Copy List with Random Pointer #138 🟑 Medium C C++ O(n) O(1)
40 Linked List Add Two Numbers #2 🟑 Medium C C++ O(max(m, n)) O(1)
41 Linked List Linked List Cycle #141 🟒 Easy C C++ O(n) O(1)
42 Linked List Find the Duplicate Number #287 🟑 Medium C C++ O(n) O(1)
43 Linked List LRU Cache #146 🟑 Medium C C++ O(1) get & put O(capacity)
44 Linked List Merge K Sorted Lists #23 πŸ”΄ Hard C C++ O(n log k) O(k)
45 Linked List Reverse Nodes in k-Group #25 πŸ”΄ Hard C C++ O(n) O(1)
46 Trees Invert Binary Tree #226 🟒 Easy C C++ O(n) O(h)
47 Trees Maximum Depth of Binary Tree #104 🟒 Easy C C++ O(n) O(h)
48 Trees Diameter of Binary Tree #543 🟒 Easy C C++ O(n) O(h)
49 Trees Balanced Binary Tree #110 🟒 Easy C C++ O(n) O(h)
50 Trees Same Tree #100 🟒 Easy C C++ O(n) O(h)
51 Trees Subtree of Another Tree #572 🟒 Easy C C++ O(s * t) O(h)
52 Trees Lowest Common Ancestor of a BST #235 🟑 Medium C C++ O(h) O(1)
53 Trees Binary Tree Level Order Traversal #102 🟑 Medium C C++ O(n) O(n)
54 Trees Binary Tree Right Side View #199 🟑 Medium C C++ O(n) O(h)
55 Trees Count Good Nodes in Binary Tree #1448 🟑 Medium C C++ O(n) O(h)
56 Trees Validate Binary Search Tree #98 🟑 Medium C C++ O(n) O(h)
57 Trees Kth Smallest Element in a BST #230 🟑 Medium C C++ O(h + k) O(h)
58 Trees Construct Binary Tree from Preorder and Inorder Traversal #105 🟑 Medium C C++ O(n) O(n)
59 Trees Binary Tree Maximum Path Sum #124 πŸ”΄ Hard C C++ O(n) O(h)
60 Trees Serialize and Deserialize Binary Tree #297 πŸ”΄ Hard C C++ O(n) O(n)
61 Tries Implement Trie (Prefix Tree) #208 🟑 Medium C C++ O(L) per op O(total characters)
62 Tries Design Add and Search Words Data Structure #211 🟑 Medium C C++ Add: O(L), Search: O(26^d) O(total chars)
63 Tries Word Search II #212 πŸ”΄ Hard C C++ O(m * n * 4^L) O(total characters in words)
64 Heap / Priority Queue Kth Largest Element in a Stream #703 🟒 Easy C C++ O(n log k) O(k)
65 Heap / Priority Queue Last Stone Weight #1046 🟒 Easy C C++ O(n log n) O(n)
66 Heap / Priority Queue K Closest Points to Origin #973 🟑 Medium C C++ O(n log k) O(k)
67 Heap / Priority Queue Kth Largest Element in an Array #215 🟑 Medium C C++ O(n) average QuickSelect or O(n log k) Min-Heap O(1)
68 Heap / Priority Queue Task Scheduler #621 🟑 Medium C C++ O(n) O(1)
69 Heap / Priority Queue Design Twitter #355 🟑 Medium C C++ O(k log k) O(users + tweets)
70 Heap / Priority Queue Find Median from Data Stream #295 πŸ”΄ Hard C C++ Add: O(log n), Find: O(1) O(n)
71 Backtracking Subsets #78 🟑 Medium C C++ O(n * 2^n) O(n)
72 Backtracking Combination Sum #39 🟑 Medium C C++ O(2^target) O(target)
73 Backtracking Permutations #46 🟑 Medium C C++ O(n! * n) O(n)
74 Backtracking Subsets II #90 🟑 Medium C C++ O(n * 2^n) O(n)
75 Backtracking Combination Sum II #40 🟑 Medium C C++ O(2^n) O(n)
76 Backtracking Word Search #79 🟑 Medium C C++ O(m * n * 4^L) O(L)
77 Backtracking Palindrome Partitioning #131 🟑 Medium C C++ O(n * 2^n) O(n)
78 Backtracking Letter Combinations of a Phone Number #17 🟑 Medium C C++ O(4^n * n) O(n)
79 Backtracking N-Queens #51 πŸ”΄ Hard C C++ O(n!) O(n)
80 Graphs Number of Islands #200 🟑 Medium C C++ O(m * n) O(m * n)
81 Graphs Clone Graph #133 🟑 Medium C C++ O(V + E) O(V)
82 Graphs Max Area of Island #695 🟑 Medium C C++ O(m * n) O(m * n)
83 Graphs Pacific Atlantic Water Flow #417 🟑 Medium C C++ O(m * n) O(m * n)
84 Graphs Surrounded Regions #130 🟑 Medium C C++ O(m * n) O(m * n)
85 Graphs Rotting Oranges #994 🟑 Medium C C++ O(m * n) O(m * n)
86 Graphs Walls and Gates #286 🟑 Medium C C++ O(m * n) O(m * n)
87 Graphs Course Schedule #207 🟑 Medium C C++ O(V + E) O(V + E)
88 Graphs Course Schedule II #210 🟑 Medium C C++ O(V + E) O(V + E)
89 Graphs Redundant Connection #684 🟑 Medium C C++ O(n * alpha(n)) O(n)
90 Graphs Number of Connected Components in an Undirected Graph #323 🟑 Medium C C++ O(V + E) O(V)
91 Graphs Graph Valid Tree #261 🟑 Medium C C++ O(V + E) O(V)
92 Graphs Word Ladder #127 πŸ”΄ Hard C C++ O(M^2 * N) O(M^2 * N)
93 Advanced Graphs Reconstruct Itinerary #332 πŸ”΄ Hard C C++ O(E log E) O(V + E)
94 Advanced Graphs Min Cost to Connect All Points #1584 🟑 Medium C C++ O(V^2) O(V)
95 Advanced Graphs Network Delay Time #743 🟑 Medium C C++ O(E log V) O(V + E)
96 Advanced Graphs Swim in Rising Water #778 πŸ”΄ Hard C C++ O(n^2 log n) O(n^2)
97 Advanced Graphs Alien Dictionary #269 πŸ”΄ Hard C C++ O(C) O(1)
98 Advanced Graphs Cheapest Flights Within K Stops #787 🟑 Medium C C++ O(K * E) O(V)
99 1-D Dynamic Programming Climbing Stairs #70 🟒 Easy C C++ O(n) O(1)
100 1-D Dynamic Programming Min Cost Climbing Stairs #746 🟒 Easy C C++ O(n) O(1)
101 1-D Dynamic Programming House Robber #198 🟑 Medium C C++ O(n) O(1)
102 1-D Dynamic Programming House Robber II #213 🟑 Medium C C++ O(n) O(1)
103 1-D Dynamic Programming Longest Palindromic Substring #5 🟑 Medium C C++ O(n^2) O(1)
104 1-D Dynamic Programming Palindromic Substrings #647 🟑 Medium C C++ O(n^2) O(1)
105 1-D Dynamic Programming Decode Ways #91 🟑 Medium C C++ O(n) O(1)
106 1-D Dynamic Programming Coin Change #322 🟑 Medium C C++ O(amount * n) O(amount)
107 1-D Dynamic Programming Maximum Product Subarray #152 🟑 Medium C C++ O(n) O(1)
108 1-D Dynamic Programming Word Break #139 🟑 Medium C C++ O(n * m * k) O(n)
109 1-D Dynamic Programming Longest Increasing Subsequence #300 🟑 Medium C C++ O(n log n) O(n)
110 1-D Dynamic Programming Partition Equal Subset Sum #416 🟑 Medium C C++ O(n * sum) O(sum)
111 2-D Dynamic Programming Unique Paths #62 🟑 Medium C C++ O(m * n) O(n)
112 2-D Dynamic Programming Longest Common Subsequence #1143 🟑 Medium C C++ O(m * n) O(min(m, n))
113 2-D Dynamic Programming Best Time to Buy and Sell Stock with Cooldown #309 🟑 Medium C C++ O(n) O(1)
114 2-D Dynamic Programming Coin Change II #518 🟑 Medium C C++ O(amount * n) O(amount)
115 2-D Dynamic Programming Target Sum #494 🟑 Medium C C++ O(n * sum) O(sum)
116 2-D Dynamic Programming Interleaving String #97 🟑 Medium C C++ O(m * n) O(n)
117 2-D Dynamic Programming Longest Increasing Path in a Matrix #329 πŸ”΄ Hard C C++ O(m * n) O(m * n)
118 2-D Dynamic Programming Distinct Subsequences #115 πŸ”΄ Hard C C++ O(m * n) O(n)
119 2-D Dynamic Programming Edit Distance #72 🟑 Medium C C++ O(m * n) O(n)
120 2-D Dynamic Programming Burst Balloons #312 πŸ”΄ Hard C C++ O(n^3) O(n^2)
121 2-D Dynamic Programming Regular Expression Matching #10 πŸ”΄ Hard C C++ O(m * n) O(m * n)
122 Greedy Maximum Subarray #53 🟑 Medium C C++ O(n) O(1)
123 Greedy Jump Game #55 🟑 Medium C C++ O(n) O(1)
124 Greedy Jump Game II #45 🟑 Medium C C++ O(n) O(1)
125 Greedy Gas Station #134 🟑 Medium C C++ O(n) O(1)
126 Greedy Hand of Straights #846 🟑 Medium C C++ O(n log n) O(n)
127 Greedy Merge Triplets to Form Target Triplet #1899 🟑 Medium C C++ O(n) O(1)
128 Greedy Partition Labels #763 🟑 Medium C C++ O(n) O(1)
129 Greedy Valid Parenthesis String #678 🟑 Medium C C++ O(n) O(1)
130 Intervals Insert Interval #57 🟑 Medium C C++ O(n) O(n)
131 Intervals Merge Intervals #56 🟑 Medium C C++ O(n log n) O(n)
132 Intervals Non-overlapping Intervals #435 🟑 Medium C C++ O(n log n) O(1)
133 Intervals Meeting Rooms #252 🟒 Easy C C++ O(n log n) O(1)
134 Intervals Meeting Rooms II #253 🟑 Medium C C++ O(n log n) O(n)
135 Intervals Minimum Interval to Include Each Query #1851 πŸ”΄ Hard C C++ O(n log n + q log q) O(n + q)
136 Math & Geometry Rotate Image #48 🟑 Medium C C++ O(n^2) O(1)
137 Math & Geometry Spiral Matrix #54 🟑 Medium C C++ O(m * n) O(1) auxiliary
138 Math & Geometry Set Matrix Zeroes #73 🟑 Medium C C++ O(m * n) O(1)
139 Math & Geometry Happy Number #202 🟒 Easy C C++ O(log n) O(1)
140 Math & Geometry Plus One #66 🟒 Easy C C++ O(n) O(1) auxiliary
141 Math & Geometry Pow(x, n) #50 🟑 Medium C C++ O(log n) O(1)
142 Math & Geometry Multiply Strings #43 🟑 Medium C C++ O(m * n) O(m + n)
143 Math & Geometry Detect Squares #2013 🟑 Medium C C++ Add: O(1), Count: O(N) O(N)
144 Bit Manipulation Single Number #136 🟒 Easy C C++ O(n) O(1)
145 Bit Manipulation Number of 1 Bits #191 🟒 Easy C C++ O(1) O(1)
146 Bit Manipulation Counting Bits #338 🟒 Easy C C++ O(n) O(1) auxiliary
147 Bit Manipulation Reverse Bits #190 🟒 Easy C C++ O(1) O(1)
148 Bit Manipulation Missing Number #268 🟒 Easy C C++ O(n) O(1)
149 Bit Manipulation Sum of Two Integers #371 🟑 Medium C C++ O(1) O(1)
150 Bit Manipulation Reverse Integer #7 🟑 Medium C C++ O(log x) O(1)

πŸ› οΈ How to Compile & Run Locally

Compiling C Solutions

# Compile with GCC
gcc -Wall -Wextra -O2 01-Arrays-and-Hashing/01-contains-duplicate/solution.c -o solution
./solution

Compiling C++ Solutions

# Compile with G++ (C++17)
g++ -std=c++17 -Wall -Wextra -O2 01-Arrays-and-Hashing/01-contains-duplicate/solution.cpp -o solution
./solution

πŸ‘€ Author


πŸ“„ License

This project is licensed under the MIT License.

πŸ“Œ Note: All problem solutions include comprehensive Big-O time and space complexity analysis.

Releases

Packages

Contributors

Languages