Skip to content

About

My Python solutions to LeetCode problems, organized by topic along the NeetCode roadmap, with tests and complexity notes.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

47 Commits

Folders and files

Repository files navigation

Data Structures & Algorithms

CI Python 3.10+

My Python solutions to LeetCode problems, organized by topic along the NeetCode roadmap. Solutions are kept exactly as submitted to LeetCode, and every one is tested against the problem's examples.

Solutions

Space complexity excludes the output. h is the height of the tree.

Arrays & Hashing

# Problem Difficulty Solution Time Space
1 Two Sum Easy Python O(n log n) O(n)
13 Roman to Integer Easy Python O(n) O(1)
14 Longest Common Prefix Easy Python O(n * m) O(m)

Two Pointers

# Problem Difficulty Solution Time Space
26 Remove Duplicates from Sorted Array Easy Python O(n) O(1)
27 Remove Element Easy Python O(n) O(n)
88 Merge Sorted Array Easy Python O(m + n) O(1)
125 Valid Palindrome Easy Python O(n) O(1)

Sliding Window

# Problem Difficulty Solution Time Space
3 Longest Substring Without Repeating Characters Medium Python O(n) O(min(n, k))
121 Best Time to Buy and Sell Stock Easy Python O(n) O(1)

Stack

# Problem Difficulty Solution Time Space
20 Valid Parentheses Easy Python O(n) O(n)

Binary Search

# Problem Difficulty Solution Time Space
35 Search Insert Position Easy Python O(log n) O(1)
69 Sqrt(x) Easy Python O(1) O(1)

Linked List

# Problem Difficulty Solution Time Space
2 Add Two Numbers Medium Python O(max(n, m)) O(1)
21 Merge Two Sorted Lists Easy Python O(n + m) O(1)
83 Remove Duplicates from Sorted List Easy Python O(n) O(1)
141 Linked List Cycle Easy Python O(n) O(1)

Trees

# Problem Difficulty Solution Time Space
94 Binary Tree Inorder Traversal Easy Python O(n) O(h)
100 Same Tree Easy Python O(n) O(h)
101 Symmetric Tree Easy Python O(n) O(h)
104 Maximum Depth of Binary Tree Easy Python O(n) O(h)
108 Convert Sorted Array to Binary Search Tree Easy Python O(n) O(log n)
110 Balanced Binary Tree Easy Python O(n) O(h)
111 Minimum Depth of Binary Tree Easy Python O(n) O(h)
112 Path Sum Easy Python O(n) O(h)

Dynamic Programming

# Problem Difficulty Solution Time Space
70 Climbing Stairs Easy Python O(n) O(n)
118 Pascal's Triangle Easy Python O(n^2) O(1)
119 Pascal's Triangle II Easy Python O(n^2) O(1)

Greedy

# Problem Difficulty Solution Time Space
455 Assign Cookies Easy Python O(n log n + m log m) O(n + m)

Math & Geometry

# Problem Difficulty Solution Time Space
7 Reverse Integer Medium Python O(log x) O(1)
9 Palindrome Number Easy Python O(log x) O(1)

Bit Manipulation

# Problem Difficulty Solution Time Space
67 Add Binary Easy Python O(n) O(n)
136 Single Number Easy Python O(n) O(1)

Repository layout

.
├── solutions/   # one folder per topic, e.g. solutions/trees/0104_maximum_depth_of_binary_tree.py
└── tests/       # pytest cases for every solution, one file per topic

Running the tests

Requires Python 3.10+.

pip install -r requirements-dev.txt
pytest

The tests run each solution with the names LeetCode provides without imports (List, Optional, deque, ListNode, TreeNode, ...), so solution files don't need any changes to run locally.

Adding a solution

  1. Paste the accepted submission, unchanged, into solutions/<topic>/<number>_<slug>.py. Pad the number to four digits and take the slug from the LeetCode URL, for example solutions/arrays-and-hashing/0217_contains_duplicate.py. New topic folders follow NeetCode's category names, such as heap-priority-queue, backtracking, or graphs.
  2. Add a row for it to the matching table above.
  3. Add a test_<number>_<slug> test to tests/test_<topic>.py.

CI fails if a solution is missing from the README, a row's link doesn't match its file name, or a solution has no test.

About

My Python solutions to LeetCode problems, organized by topic along the NeetCode roadmap, with tests and complexity notes.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages