JeffreytheCoder / leetcode-solutions

My leetcode solutions in Python

Home Page:https://app.gitbook.com/s/-MhOBOBsETK6newht3s5

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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
*difficulty is set by personal consideration, since the default leetcode difficulty is not accurate for some problems

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

Happy leetcode!

About

My leetcode solutions in Python

https://app.gitbook.com/s/-MhOBOBsETK6newht3s5


Languages

Language:Python 100.0%