leetcode-solutions
My leetcode solutions in Python, updating ~10 problems per week 🚀
Solution Format
Each solution contains
- category [difficulty out of 5]
- key: one to two important key concepts to understand the approach of this solution
- solution code with different approaches, e.g. iterative & recursive
- time & space complexity
Example
# hashmap [1/5]
# key: to achieve single loop, find another value that adds up to target with iterated value
# use hashmap to store {num in nums: its idx}
# for each (num, idx) in nums:
# if (target - num) is in hashmap, return idx and hashmap[target - num]
# else store {num: idx} to hashmap
class Solution:
def twoSum(self, nums: List[int], target: int) -> List[int]:
dict = {}
for idx, val in enumerate(nums):
if (target - val) in dict:
return [idx, dict[target - val]]
else:
dict[val] = idx
# time: O(n)
# space: O(n)
Categories of Solutions
Category | # of problems |
---|---|
Array | 1 |
String | 2 |
Linked List | 1 |
Stack & Queue | 1 |
Hashmap | 3 |
Binary Tree | 20 |
Binary Search Tree | 6 |
Backtracking | 0 |
Greedy | 0 |
Dynammic Programming | 1 |