Skip to content

Leetcode problems I've been through, with detail problem solving strategy

License

Notifications You must be signed in to change notification settings

han8909227/leetcode

Repository files navigation

LeetCode Marathon

To aid my coding skill and to succeed in coding interview I am working on a collection of LeetCode problems.

Goal of this repository

My goal is to help other Python developer who are studying for coding interviews to have a better reference and insight to Leet Code problems, since many of the article/blog on Leet Code problems are written in Java.

Want to contribute?

  • First fork it !
  • Add problem and your solution to the appropriate classifier folder
  • Submit a pull request to this main repository
  • Upon review your contribution will be showing in this repo that will help many others!

Further explainations

I have constructed a blog site to post my thought process on solving some of the problems listed below It can be linked to HERE

Solved LeetCode Problems

# Title Solution Time Space Notes
2 Add Two Numbers Python O(n) O(1)
13 Roman to Integer Python O(n) O(1)
17 Letter Combinations of a Phone Number Python O(n * 4^n) O(n)
20 Valid Parentheses Python O(n) O(n)
24 Swap Nodes in Pairs Python O(n) O(1)
33 Search in Rotated Sorted Array Python O(logn) O(1)
34 Search for a Range Python O(logn) O(1)
45 Jump Game II Python O(n) O(1)
55 Jump Game Python O(n) O(1)
61 Rotate List Python O(n) O(1)
62 Unique Paths Python O(m * n) O(m + \n)
63 Unique Paths II Python O(m * n) O(m + n)
74 Search a 2D Matrix Python O(logm + logn) O(1)
78 Subsets Python O(n * 2^n) O(1)
92 Reverse Linked List II Python O(n) O(1)
96 Unique Binary Search Trees Python O(n) O(1)
98 Validate Binary Search Tree Python O(n) O(1)
102 Binary Tree Level Order Traversal Python O(n) O(n)
103 Binary Tree Zigzag Level Order Traversal Python O(n) O(n)
111 Minimum Depth of Binary Tree Python O(n) O(h)
112 Path Sum Python O(n) O(h)
113 Path Sum II Python O(n) O(h) Asked with LC112
116 Populating Next Right Pointers in Each Node Python O(n) O(1) Asked with LC117
117 Populating Next Right Pointers in Each Node II Python O(n) O(1) Asked with LC116
121 Best Time to Buy and Sell Stock Python O(n) O(1) Easy
122 Best Time to Buy and Sell Stock II Python O(n) O(1)
124 Binary Tree Maximum Path Sum Python O(n) O(h)
127 Word Ladder Python O(n * d) O(d)
141 Linked List Cycle Python O(n) O(1)
142 Linked List Cycle II Python O(n) O(1)
150 Evaluate Reverse Polish Notation Python O(n) O(n)
155 Min Stack Python O(n) O(1)
160 Intersection of Two Linked Lists Python O(n + m) O(1)
200 Number of Islands Python O(m * n) O(m * n) Matrix, LinkedList
206 Reverse Linked List Python O(n) O(1)
232 Implement Queue using Stacks Python O(1), amortized O(n)
236 Lowest Common Ancestor of a Binary Tree Python O(n) O(h)
257 Binary Tree Paths Python O(n * h) O(h)
283 Move Zeroes Python O(n) O(1)
380 Insert Delete GetRandom O(1) Python O(1) O(n)
416 Partition Equal Subset Sum Python O(n * s) O(s)
437 Path Sum III Python O(n) O(h)
449 Serialize and Deserialize BST Python O(n) O(h)
496 Next Greater Element Python O(n) O(1) Asked with LC503
503 Next Greater Element II Python O(n) O(1) Asked with LC496
545 Boundary of Binary Tree Python O(n) O(h)
547 Friend Circles Python O(n^2) O(n) Union Find
746 Min Cost Climbing Stairs Python O(n) O(1)

Clarify questions to ask at interview:

  1. Array:

    • Sorted or not?
    • How many elements?
    • Element type? Int, float, double?
    • What's the range of those numbers? Positive or negative?
    • Contain duplicates or not?
    • Subsequence: adjacent or not?
  2. Binary tree:

    • Binary search tree or normal binary tree?
    • Balanced or not?
    • Complete or not?
    • Has parent pointer or not?
  3. Linked list:

    • Singly or doubly linked list?
    • Has duplicated nodal value or not?
  4. String:

    • Need to remove white spaces? Tab and newline?
    • Only has digits? English letters? Upper or lower case?
  5. Graph:

    • How many nodes and edges?
    • Directed or undirected?
    • Edges have weights? If so, what's the range?
    • Has loops? Negative sum loops?
  6. Return value:

    • What should my method return?
    • If there are multiple solutions to the problem, which one should be returned?
    • If it should return multiple values, do you have any preference on what to return?
    • What should I do/return if the input is invalid / does not match the constraints?