Leetcode Python Solutions.
π Summary
This repository contains solutions to Leetcode problems. It will be updated regularly(Daily/Weekly). The primary reason for this repository is because I believe the best way to solve these problems is by dividing them into topics, each topic into difficulties, and solving at least one problem within each difficulty(quality > quantity). Such repositories are hard to find on the internet; this is the first one most likely. Therefore, I will gradually build this place to make it easier for beginners to learn algorithms by solving problems on leetcode by topics.
Donβt forget to slap that β button an odd number of times ;-)
Currently maintained by
Mahmoud Harmouch.
Prerequisites
Basic understanding of the core language. If you are new to python, I highly recommend checking out the free learning material I compiled in the awesome-python repository.
python -c "import this" | grep Simple
Simple is better than complex.
π Table Of Content (TOC).
Two Pointers
π Go To TOC.
Easy
| # | Problem Statement | Notes | Solution | Time Complexity | Space Complexity |
|---|---|---|---|---|---|
| 283. | Move Zeroes |
|
file
|
O(n). The right pointer does not visit the same element twice. | O(1). All operations are made in-place. |
| 1. | Two Sum |
|
file
|
O(n logn). because of Timsort | O(n). n is the length of the tmp List. |
| 977. | Squares of a Sorted Array |
|
file
|
O(n). A linear scan, n is the length of the results List | O(n). n is the length of the results List. |
Medium
| # | Problem Statement | Notes | Solution | Time Complexity | Space Complexity |
|---|---|---|---|---|---|
| 167. | Two Sum II - Input Array Is Sorted |
|
file
|
O(n). The right and left pointers do not visit the same element twice. | O(1). All operations are made in-place. |
Hash Table
π Go To TOC.
Easy
| # | Problem Statement | Notes | Solution | Time Complexity | Space Complexity |
|---|---|---|---|---|---|
| 1. | Two Sum |
|
file
|
O(n). One for loop, hashmap lookup ~ O(1) | O(n). Hash Table. |
Medium
| # | Problem Statement | Notes | Solution | Time Complexity | Space Complexity |
|---|---|---|---|---|---|
| 12. | Integer to Roman |
|
file
|
O(k). k: length of the hash table. | O(n). n: length of the hash table + list. |
Binary Tree
π Go To TOC.
Easy
| # | Problem Statement | Notes | Solution | Time Complexity | Space Complexity |
|---|---|---|---|---|---|
| 144. | Recursive Preorder Traversal |
|
file
|
O(n), n number of nodes. | O(log n) ~ height/depth of the tree. |
| 144. | Iterative Preorder Traversal |
|
file
|
O(n), n number of nodes. | O(log n) ~ height/depth of the tree. |
| 144. | Morris Traversal |
|
file
|
O(n), n number of nodes. | O(1). |
| 94. | Iterative Inorder Traversal |
|
file
|
O(n), n number of nodes. | O(n), n number of nodes. |
| 94. | Recursive Inorder Traversal |
|
file
|
O(n), n number of nodes. | O(log n) ~ height/depth of the tree. |
| 145 | Iterative Postorder Traversal |
|
file
|
O(n), n number of nodes. | O(n), n number of nodes. |
| 145. | Recursive Postorder Traversal |
|
file
|
O(n), n number of nodes. | O(log n) ~ height/depth of the tree. |
| 637. | Iterative Average of Levels in Binary Tree |
|
file
|
O(n), n number of nodes. | O(n), n number of nodes. |
Medium
| # | Problem Statement | Notes | Solution | Time Complexity | Space Complexity |
|---|---|---|---|---|---|
| 102. | Level Order Traversal |
|
file
|
O(n). | O(n) |
| 102. | Recursive Level Order Traversal |
|
file
|
O(n). | O(logn) |
| 103. | Iterative Zigzag Level Order Traversal |
|
file
|
O(n). | O(n) |
| 103. | Recursive Zigzag Level Order Traversal |
|
file
|
O(n). | O(logn) |
| 116. | Populating Next Right Pointers in Each Node |
|
file
|
O(n). | O(1) by using pointers |
Binary Search
π Go To TOC.
Easy
| # | Problem Statement | Notes | Solution | Time Complexity | Space Complexity |
|---|---|---|---|---|---|
| 744. | Find Smallest Letter Greater Than Target |
|
file
|
O(log n). n: length of the list. | O(1). pointers. |
| 704. | Iterative Binary Search |
|
file
|
O(log n). n: length of the list. | O(1). pointers. |
| 704. | Recursive Binary Search |
|
file
|
O(log n). n: length of the list. | O(log n). log n: The length of the binary tree. |
| 278. | Iterative First Bad Version |
|
file
|
O(log n). n: length of the list. | O(1). pointers. |
| 278. | Recursive First Bad Version |
|
file
|
O(log n). n: length of the list. | O(log n). log n: The length of the binary tree. |
| 35. | Iterative Search Insert Position |
|
file
|
O(log n). n: length of the list. | O(1). pointers. |
| 35. | Recursive Search Insert Position |
|
file
|
O(log n). n: length of the list. | O(log n). log n: The length of the binary tree. |
| 69. | Sqrt(x) |
|
file
|
O(log n). n: given number. | O(1). pointers. |
Medium
| # | Problem Statement | Notes | Solution | Time Complexity | Space Complexity |
|---|---|---|---|---|---|
| 153. | Find Minimum in Rotated Sorted Array |
|
file
|
O(log n). n: length of the list. | O(1). pointers. |
| 162. | Find Peak Element |
|
file
|
O(log n). n: length of the list. | O(1). pointers. |
| 729. | My Calendar I |
|
file
|
O(log n). n: length of the list. | O(1). pointers. |
Hard
| # | Problem Statement | Notes | Solution | Time Complexity | Space Complexity |
|---|---|---|---|---|---|
| 154. | Find Minimum in Rotated Sorted Array II |
|
file
|
O(log n). n: length of the list. | O(1). pointers. |
| 4. | Median of Two Sorted Arrays |
|
file
|
O(log(min(m,n))). m, n: length of both arrays. | O(1). Operations are made in place. |
String
π Go To TOC.
Easy
| # | Problem Statement | Notes | Solution | Time Complexity | Space Complexity |
|---|---|---|---|---|---|
| 14. | Longest Common Prefix |
|
file
|
O(n), n is the length of the array. | O(1). constant length of the prefix. |
| 14. | Reorder Data in Log Files |
|
file
|
O(m n logn). because of Timsort. m the for the comparison between two keys. | O(m n) to keep the keys for the log. |
| 205. | Isomorphic Strings |
|
file
|
O(n), n: length of the input string. | O(1), because the number of ASCII chars is constant, man ascii |
| 205. | Is Subsequence |
|
file
|
O(n). | O(1). |
Medium
| # | Problem Statement | Notes | Solution | Time Complexity | Space Complexity |
|---|---|---|---|---|---|
| 49. | Group Anagrams |
|
file
|
O(N K logβ‘ K), N is the length of strs, and K is the maximum length of a string in strs. | O(N K), N strings, K is the maximum length of a string in strs. |
Linked List
π Go To TOC.
Easy
| # | Problem Statement | Notes | Solution | Time Complexity | Space Complexity |
|---|---|---|---|---|---|
| 206. | Iterative Reverse Linked List |
|
file
|
O(n), n denotes the size of the linked list. | O(1), Operations are made in-place. |
| 206. | Recursive Reverse Linked List |
|
file
|
O(n), n denotes the size of the linked list. | O(n), n denotes the size of the linked list. |
| 206. | Stack Iterative Reverse Linked List |
|
file
|
O(n), n denotes the size of the linked list. | O(n), n denotes the size of the linked list. |
| 21. | Iterative Merge Two Sorted Lists |
|
file
|
O(n), n denotes the size of a linked list. | O(n), n denotes the size of a linked list. |
| 21. | Recursive Merge Two Sorted Lists |
|
file
|
O(n), n denotes the size of a linked list. | O(n), n denotes the size of a linked list. |
Medium
| # | Problem Statement | Notes | Solution | Time Complexity | Space Complexity |
|---|---|---|---|---|---|
| 2. | Add Two Numbers |
|
file
|
O(maxβ‘(m,n)) where m and n represents the length of l1 and l2 respectively. | O(maxβ‘(m,n)) The length of the new list is at most maxβ‘(m,n)+1. |
| 143. | Reorder List |
|
file
|
O(n), n denotes the size of the linked list. | O(1). |
Array
π Go To TOC.
Easy
| # | Problem Statement | Notes | Solution | Time Complexity | Space Complexity |
|---|---|---|---|---|---|
| 1480. | Running Sum of 1d Array |
|
file
|
O(n), n is the length of the array. | O(1). Operations are made in place. |
| 724. | Find Pivot Index |
|
file
|
O(n), n is the length of the array. | O(1). Operations are made in place. |
Medium
| # | Problem Statement | Notes | Solution | Time Complexity | Space Complexity |
|---|---|---|---|---|---|
| 240. | Search a 2D Matrix II |
|
file
|
O(m+n), m, n are the number of columns and rows in the array. | O(1). Operations are made in place. |
| 378. | Kth Smallest Element in a Sorted Matrix |
|
file
|
O(m+n), m, n are the number of columns and rows in the array. | O(1). Operations are made in place. |
Math
π Go To TOC.
Easy
| # | Problem Statement | Notes | Solution | Time Complexity | Space Complexity |
|---|---|---|---|---|---|
| 858. | Mirror Reflection |
|
file
|
O(1). | O(1). |
Contributing
You can open a pull request if you want!
License
This project is licensed under the MIT License.