An open API service indexing awesome lists of open source software.

https://github.com/terenceclzhang/leetcode-solutions

LeetCode Solutions
https://github.com/terenceclzhang/leetcode-solutions

algorithms data-structures data-structures-and-algorithms leetcode leetcode-solutions

Last synced: 9 days ago
JSON representation

LeetCode Solutions

Awesome Lists containing this project

README

          

# LeetCode Solutions

This repository contains my leetcode solutions to select questions.

## Contents

- [Algomap.io](#algomapio)
- [Arrays & Strings](#arrays--strings)
- [HashMaps & Sets](#hashmaps--sets)
- [2 Pointers](#2-pointers)
- [Stacks](#stacks)
- [Linked Lists](#linked-lists)
- [Binary Search](#binary-search)
- [Sliding Window](#sliding-window)
- [Trees](#trees)
- [Heaps](#heaps)
- [Recursive Backtracking](#recursive-backtracking)
- [Graphs](#graphs)
- [Dynamic Programming](#dynamic-programming)
- [Bit Manipulation](#bit-manipulation)


## Algomap.io

https://algomap.io/list/

### Arrays & Strings

| Problem | Solutions | Time | Space |
| ------------------------------------------------------------------------------------------------------------------------------- | ----------------------------------------------------------------- | --------------------- | -------- |
| [2239. Find Closest Number to Zero](https://leetcode.com/problems/find-closest-number-to-zero/description/) | [Link](/2239.%20Find%20Closest%20Number%20to%20Zero/) | $$O(n)$$ | $$O(1)$$ |
| [1768. Merge Strings Alternately](https://leetcode.com/problems/merge-strings-alternately/description/) | [Link](/1768.%20Merge%20Strings%20Alternately/) | $$O(n)$$ | $$O(n)$$ |
| [13. Roman to Integer](https://leetcode.com/problems/roman-to-integer/description/) | [Link](/13.%20Roman%20to%20Integer/) | $$O(n)$$ | $$O(1)$$ |
| [392. Is Subsequence](https://leetcode.com/problems/is-subsequence/description/) | [Link](/392.%20Is%20Subsequence/) | $$O(n)$$ | $$O(1)$$ |
| [121. Best Time to Buy and Sell Stock](https://leetcode.com/problems/best-time-to-buy-and-sell-stock/description/) | [Link](/121.%20Best%20Time%20to%20Buy%20and%20Sell%20Stock/) | $$O(n)$$ | $$O(1)$$ |
| [14. Longest Common Prefix](https://leetcode.com/problems/longest-common-prefix/description/) | [Link](/14.%20Longest%20Common%20Prefix/) | $$O(n \cdot m)$$ | $$O(1)$$ |
| [228. Summary Ranges](https://leetcode.com/problems/summary-ranges/description/) | [Link](/228.%20Summary%20Ranges/) | $$O(n)$$ | $$O(1)$$ |
| [228. Summary Ranges](https://leetcode.com/problems/summary-ranges/description/) | [Link](/228.%20Summary%20Ranges/) | $$O(n)$$ | $$O(1)$$ |
| [26. Remove Duplicates from Sorted Array](https://leetcode.com/problems/remove-duplicates-from-sorted-array/description/) | [Link](/26.%20Remove%20Duplicates%20from%20Sorted%20Array/) | $$O(n)$$ | $$O(1)$$ |
| [27. Remove Element](https://leetcode.com/problems/remove-element/description/) | [Link](/27.%20Remove%20Element/) | $$O(n)$$ | $$O(1)$$ |
| [88. Merge Sorted Array](https://leetcode.com/problems/merge-sorted-array/description/) | [Link](/88.%20Merge%20Sorted%20Array/) | $$O(n + m)$$ | $$O(1)$$ |
| [122. Best Time to Buy and Sell Stock II](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/description/) | [Link](/122.%20Best%20Time%20to%20Buy%20and%20Sell%20Stock%20II/) | $$O(n)$$ | $$O(1)$$ |
| [80. Remove Duplicates from Sorted Array II](https://leetcode.com/problems/remove-duplicates-from-sorted-array-ii/description/) | [Link](/80.%20Remove%20Duplicates%20from%20Sorted%20Array%20II/) | $$O(n)$$ | $$O(1)$$ |
| [75. Sort Colors (Dutch National Flag Problem)](https://leetcode.com/problems/sort-colors/description/) | [Link](/75.%20Sort%20Colors/) | $$O(n)$$ | $$O(1)$$ |
| [238. Product of Array Except Self](https://leetcode.com/problems/product-of-array-except-self/description/) | [Link](/238.%20Product%20of%20Array%20Except%20Self/) | $$O(n)$$ | $$O(1)$$ |
| [274. H-Index](https://leetcode.com/problems/h-index/description/) | [Link](/274.%20H-Index/) | $$O(n)$$ | $$O(n)$$ |
| [**56. Merge Intervals**](https://leetcode.com/problems/merge-intervals/description/) | [Link](/56.%20Merge%20Intervals/) | $$O(n \cdot \log n)$$ | $$O(n)$$ |
| [**54. Spiral Matrix**](https://leetcode.com/problems/spiral-matrix/description/) | [Link](/54.%20Spiral%20Matrix/) | $$O(m \cdot n)$$ | $$O(1)$$ |
| [**6. Zigzag Conversion**](https://leetcode.com/problems/zigzag-conversion/description/) | [Link](/6.%20Zigzag%20Conversion/) | $$O(n)$$ | $$O(n)$$ |
| [**48. Rotate Image**](https://leetcode.com/problems/rotate-image/description/) | [Link](/48.%20Rotate%20Image/) | $$O(n^2)$$ | $$O(1)$$ |

### Hashmaps & Sets

| Problem | Solutions | Time | Space |
| ---------------------------------------------------------------------------------------------------------------- | -------------------------------------------------- | ---------------- | ---------------- |
| [771. Jewels and Stones](https://leetcode.com/problems/jewels-and-stones/description/) | [Link](/771.%20Jewels%20and%20Stones/) | $$O(n)$$ | $$O(n)$$ |
| [217. Contains Duplicate](https://leetcode.com/problems/contains-duplicate/description/) | [Link](/217.%20Contains%20Duplicate/) | $$O(n)$$ | $$O(n)$$ |
| [383. Ransom Note](https://leetcode.com/problems/ransom-note/description/) | [Link](/383.%20Ransom%20Note/) | $$O(n + m)$$ | $$O(m)$$ |
| [242. Valid Anagram](https://leetcode.com/problems/valid-anagram/description/) | [Link](/242.%20Valid%20Anagram/) | $$O(n)$$ | $$O(1)$$ |
| [1189. Maximum Number of Balloons](https://leetcode.com/problems/maximum-number-of-balloons/description/) | [Link](/1189.%20Maximum%20Number%20of%20Balloons/) | $$O(n)$$ | $$O(1)$$ |
| [1. Two Sum](https://leetcode.com/problems/two-sum/description/) | [Link](/1.%20Two%20Sum/) | $$O(n)$$ | $$O(n)$$ |
| [36. Valid Sudoku](https://leetcode.com/problems/valid-sudoku/description/) | [Link](/36.%20Valid%20Sudoku/) | $$O(1)$$ | $$O(1)$$ |
| [49. Group Anagrams](https://leetcode.com/problems/group-anagrams/description/) | [Link](/49.%20Group%20Anagrams/) | $$O(n \cdot m)$$ | $$O(n \cdot m)$$ |
| [169. Majority Element](https://leetcode.com/problems/majority-element/description/) | [Link](/169.%20Majority%20Element/) | $$O(n)$$ | $$O(1)$$ |
| [**128. Longest Consecutive Sequence**](https://leetcode.com/problems/longest-consecutive-sequence/description/) | [Link](/128.%20Longest%20Consecutive%20Sequence/) | $$O(n)$$ | $$O(n)$$ |

### 2 Pointers

| Problem | Solutions | Time | Space |
| ---------------------------------------------------------------------------------------------------------------------- | ----------------------------------------------------------------- | ---------- | -------- |
| [977. Squares of a Sorted Array](https://leetcode.com/problems/squares-of-a-sorted-array/description/) | [Link](/977.%20Squares%20of%20a%20Sorted%20Array/) | $$O(n)$$ | $$O(n)$$ |
| [344. Reverse String](https://leetcode.com/problems/reverse-string/description/) | [Link](/344.%20Reverse%20String/) | $$O(n)$$ | $$O(1)$$ |
| [167. Two Sum II - Input Array Is Sorted](https://leetcode.com/problems/two-sum-ii-input-array-is-sorted/description/) | [Link](/167.%20Two%20Sum%20II%20-%20Input%20Array%20Is%20Sorted/) | $$O(n)$$ | $$O(1)$$ |
| [125. Valid Palindrome](https://leetcode.com/problems/valid-palindrome/description/) | [Link](/125.%20Valid%20Palindrome/) | $$O(n)$$ | $$O(1)$$ |
| [15. 3Sum](https://leetcode.com/problems/3sum/description/) | [Link](/15.%203Sum/) | $$O(n^2)$$ | $$O(1)$$ |
| [11. Container With Most Water](https://leetcode.com/problems/container-with-most-water/description/) | [Link](/11.%20Container%20With%20Most%20Water/) | $$O(n)$$ | $$O(1)$$ |
| [**16. 3Sum Closest**](https://leetcode.com/problems/3sum-closest/description/) | [Link](/16.%203Sum%20Closest/) | $$O(n^2)$$ | $$O(1)$$ |
| [**18. 4Sum**](https://leetcode.com/problems/4sum/description/) | [Link](/18.%204Sum/) | $$O(n^3)$$ | $$O(1)$$ |

### Stacks

| Problem | Solutions | Time | Space |
| -------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------- | -------- | -------- |
| [682. Baseball Game](https://leetcode.com/problems/baseball-game/description/) | [Link](/682.%20Baseball%20Game/) | $$O(n)$$ | $$O(n)$$ |
| [20. Valid Parentheses](https://leetcode.com/problems/valid-parentheses/description/) | [Link](/20.%20Valid%20Parentheses/) | $$O(n)$$ | $$O(n)$$ |
| [150. Evaluate Reverse Polish Notation](https://leetcode.com/problems/evaluate-reverse-polish-notation/description/) | [Link](/150.%20Evaluate%20Reverse%20Polish%20Notation/) | $$O(n)$$ | $$O(n)$$ |
| [739. Daily Temperatures](https://leetcode.com/problems/daily-temperatures/description/) | [Link](/739.%20Daily%20Temperatures/) | $$O(n)$$ | $$O(n)$$ |
| [155. Min Stack](https://leetcode.com/problems/min-stack/description/) | [Link](/155.%20Min%20Stack/) | $$O(1)$$ | $$O(n)$$ |

### Linked Lists

| Problem | Solutions | Time | Space |
| ------------------------------------------------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------- | ------------ | -------- |
| [83. Remove Duplicates from Sorted List](https://leetcode.com/problems/remove-duplicates-from-sorted-list/description/) | [Link](/83.%20Remove%20Duplicates%20from%20Sorted%20List/) | $$O(n)$$ | $$O(1)$$ |
| [2807. Insert Greatest Common Divisors in Linked List](https://leetcode.com/problems/insert-greatest-common-divisors-in-linked-list/description/) | [Link](/2807.%20Insert%20Greatest%20Common%20Divisors%20in%20Linked%20List/) | $$O(n)$$ | $$O(1)$$ |
| [206. Reverse Linked List](https://leetcode.com/problems/reverse-linked-list/description/) | [Link](/206.%20Reverse%20Linked%20List/) | $$O(n)$$ | $$O(1)$$ |
| [21. Merge Two Sorted Lists](https://leetcode.com/problems/merge-two-sorted-lists/description/) | [Link](/21.%20Merge%20Two%20Sorted%20Lists/) | $$O(n + m)$$ | $$O(1)$$ |
| [141. Linked List Cycle](https://leetcode.com/problems/linked-list-cycle/description/) | [Link](/141.%20Linked%20List%20Cycle/) | $$O(n)$$ | $$O(1)$$ |
| [876. Middle of the Linked List](https://leetcode.com/problems/middle-of-the-linked-list/description/) | [Link](/876.%20Middle%20of%20the%20Linked%20List/) | $$O(n)$$ | $$O(1)$$ |
| [19. Remove Nth Node From End of List](https://leetcode.com/problems/remove-nth-node-from-end-of-list/description/) | [Link](/19.%20Remove%20Nth%20Node%20From%20End%20of%20List/) | $$O(n)$$ | $$O(1)$$ |
| [138. Copy List with Random Pointer](https://leetcode.com/problems/copy-list-with-random-pointer/description/) | [Link](/138.%20Copy%20List%20with%20Random%20Pointer/) | $$O(n)$$ | $$O(n)$$ |

### Binary Search

| Problem | Solutions | Time | Space |
| -------------------------------------------------------------------------------------------------------------------- | --------------------------------------------------------------- | ----------------------- | -------- |
| [704. Binary Search](https://leetcode.com/problems/binary-search/description/) | [Link](/704.%20Binary%20Search/) | $$O(\log n)$$ | $$O(1)$$ |
| [35. Search Insert Position](https://leetcode.com/problems/search-insert-position/description/) | [Link](/35.%20Search%20Insert%20Position/) | $$O(\log n)$$ | $$O(1)$$ |
| [278. First Bad Version](https://leetcode.com/problems/first-bad-version/description/) | [Link](/278.%20First%20Bad%20Version/) | $$O(\log n)$$ | $$O(1)$$ |
| [367. Valid Perfect Square](https://leetcode.com/problems/valid-perfect-square/description/) | [Link](/367.%20Valid%20Perfect%20Square/) | $$O(\log n)$$ | $$O(1)$$ |
| [74. Search a 2D Matrix](https://leetcode.com/problems/search-a-2d-matrix/description/) | [Link](/367.%20Valid%20Perfect%20Square/) | $$O(\log (m \cdot n))$$ | $$O(1)$$ |
| [**153. Find Minimum in Rotated Sorted Array**](https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/) | [Link](/153.%20Find%20Minimum%20in%20Rotated%20Sorted%20Array/) | $$O(\log n)$$ | $$O(1)$$ |
| [**33. Search in Rotated Sorted Array**](https://leetcode.com/problems/search-in-rotated-sorted-array/description/) | [Link](/33.%20Search%20in%20Rotated%20Sorted%20Array/) | $$O(\log n)$$ | $$O(1)$$ |
| [**875. Koko Eating Bananas**](https://leetcode.com/problems/koko-eating-bananas/description/) | [Link](/875.%20Koko%20Eating%20Bananas/) | $$O(n \cdot \log m)$$ | $$O(1)$$ |

### Sliding Window

| Problem | Solutions | Time | Space |
| -------------------------------------------------------------------------------------------------------------------------------------- | --------------------------------------------------------------------- | -------- | -------- |
| [643. Maximum Average Subarray I](https://leetcode.com/problems/maximum-average-subarray-i/description/) | [Link](/643.%20Maximum%20Average%20Subarray%20I/) | $$O(n)$$ | $$O(1)$$ |
| [1004. Max Consecutive Ones III](https://leetcode.com/problems/max-consecutive-ones-iii/description/) | [Link](/1004.%20Max%20Consecutive%20Ones%20III/) | $$O(n)$$ | $$O(1)$$ |
| [3. Longest Substring Without Repeating Characters](https://leetcode.com/problems/longest-substring-without-repeating-characters/) | [Link](/3.%20Longest%20Substring%20Without%20Repeating%20Characters/) | $$O(n)$$ | $$O(1)$$ |
| [**424. Longest Repeating Character Replacement**](https://leetcode.com/problems/longest-repeating-character-replacement/description/) | [Link](/424.%20Longest%20Repeating%20Character%20Replacement/) | $$O(n)$$ | $$O(1)$$ |
| [**209. Minimum Size Subarray Sum**](https://leetcode.com/problems/minimum-size-subarray-sum/description/) | [Link](/209.%20Minimum%20Size%20Subarray%20Sum/) | $$O(n)$$ | $$O(1)$$ |
| [567. Permutation in String](https://leetcode.com/problems/permutation-in-string/description/) | [Link](/567.%20Permutation%20in%20String/) | $$O(n)$$ | $$O(1)$$ |

### Trees

| Problem | Solutions | Time | Space |
| ---------------------------------------------------------------------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------- | ---------------- | -------- |
| [226. Invert Binary Tree](https://leetcode.com/problems/invert-binary-tree/description/) | [Link](/226.%20Invert%20Binary%20Tree/) | $$O(n)$$ | $$O(h)$$ |
| [104. Maximum Depth of Binary Tree](https://leetcode.com/problems/maximum-depth-of-binary-tree/description/) | [Link](/104.%20Maximum%20Depth%20of%20Binary%20Tree/) | $$O(n)$$ | $$O(h)$$ |
| [**110. Balanced Binary Tree**](https://leetcode.com/problems/balanced-binary-tree/description/) | [Link](/110.%20Balanced%20Binary%20Tree/) | $$O(n)$$ | $$O(h)$$ |
| [**543. Diameter of Binary Tree**](https://leetcode.com/problems/diameter-of-binary-tree/description/) | [Link](/543.%20Diameter%20of%20Binary%20Tree//) | $$O(n)$$ | $$O(h)$$ |
| [100. Same Tree](https://leetcode.com/problems/same-tree/description/) | [Link](/100.%20Same%20Tree/) | $$O(n)$$ | $$O(h)$$ |
| [101. Symmetric Tree](https://leetcode.com/problems/symmetric-tree/description/) | [Link](/101.%20Symmetric%20Tree/) | $$O(n)$$ | $$O(h)$$ |
| [112. Path Sum](https://leetcode.com/problems/path-sum/description/) | [Link](/112.%20Path%20Sum/) | $$O(n)$$ | $$O(h)$$ |
| [572. Subtree of Another Tree](https://leetcode.com/problems/subtree-of-another-tree/description/) | [Link](/572.%20Subtree%20of%20Another%20Tree/) | $$O(n \cdot m)$$ | $$O(h)$$ |
| [102. Binary Tree Level Order Traversal (BFS)](https://leetcode.com/problems/binary-tree-level-order-traversal/description/) | [Link](/102.%20Binary%20Tree%20Level%20Order%20Traversal/) | $$O(n)$$ | $$O(n)$$ |
| [637. Average of Levels in Binary Tree](https://leetcode.com/problems/average-of-levels-in-binary-tree/description/) | [Link](/637.%20Average%20of%20Levels%20in%20Binary%20Tree/) | $$O(n)$$ | $$O(n)$$ |
| [230. Kth Smallest Element in a BST](https://leetcode.com/problems/kth-smallest-element-in-a-bst/description/) | [Link](/230.%20Kth%20Smallest%20Element%20in%20a%20BST/) | $$O(n)$$ | $$O(h)$$ |
| [**530. Minimum Absolute Difference in BST**](https://leetcode.com/problems/minimum-absolute-difference-in-bst/description/) | [Link](/530.%20Minimum%20Absolute%20Difference%20in%20BST/) | $$O(n)$$ | $$O(n)$$ |
| [**98. Validate Binary Search Tree**](https://leetcode.com/problems/validate-binary-search-tree/description/) | [Link](/98.%20Validate%20Binary%20Search%20Tree/) | $$O(n)$$ | $$O(n)$$ |
| [**235. Lowest Common Ancestor of a Binary Search Tree**](https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-search-tree/description/) | [Link](/235.%20Lowest%20Common%20Ancestor%20of%20a%20Binary%20Search%20Tree/) | $$O(\log n)$$ | $$O(1)$$ |
| [**208. Implement Trie (Prefix Tree)**](https://leetcode.com/problems/implement-trie-prefix-tree/description/) | [Link](/235.%20Lowest%20Common%20Ancestor%20of%20a%20Binary%20Search%20Tree/) | $$O(m)$$ | $$O(n)$$ |

### Heaps

_Note: No JavaScript_

| Problem | Solutions | Time | Space |
| -------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------- | --------------------- | -------- |
| [1046. Last Stone Weight](https://leetcode.com/problems/last-stone-weight/) | [Link](/1046.%20Last%20Stone%20Weight/) | $$O(n \cdot \log n)$$ | $$O(n)$$ |
| [215. Kth Largest Element in an Array](https://leetcode.com/problems/kth-largest-element-in-an-array/) | [Link](/215.%20Kth%20Largest%20Element%20in%20an%20Array/) | $$O(n \cdot \log k)$$ | $$O(k)$$ |
| [347. Top K Frequent Elements](https://leetcode.com/problems/top-k-frequent-elements/description/) | [Link](/347.%20Top%20K%20Frequent%20Elements/) | $$O(n \cdot \log k)$$ | $$O(k)$$ |
| [973. K Closest Points to Origin](https://leetcode.com/problems/k-closest-points-to-origin/description/) | [Link](/973.%20K%20Closest%20Points%20to%20Origin/) | $$O(n \cdot \log k)$$ | $$O(k)$$ |

### Recursive Backtracking

| Problem | Solutions | Time | Space |
| ----------------------------------------------------------------------------------------------------------------------------- | --------------------------------------------------------------- | -------------------------- | ---------------------- |
| [78. Subsets](https://leetcode.com/problems/subsets/description/) | [Link](/78.%20Subsets/) | $$O(2^n \cdot n)$$ | $$O(2^n \cdot n)$$ |
| [46. Permutations](https://leetcode.com/problems/permutations/description/) | [Link](/46.%20Permutations/) | $$O(n \cdot n!)$$ | $$O(n \cdot n!)$$ |
| [77. Combinations](https://leetcode.com/problems/combinations/description/) | [Link](/77.%20Combinations/) | $$O(C(n, k) \cdot k)$$ | $$O(C(n, k) \cdot k)$$ |
| [39. Combination Sum](https://leetcode.com/problems/combination-sum/description/) | [Link](/39.%20Combination%20Sum/) | $$O(2^{\frac{t}{m} + 1})$$ | $$O(\frac{t}{m})$$ |
| [17. Letter Combinations of a Phone Number](https://leetcode.com/problems/letter-combinations-of-a-phone-number/description/) | [Link](/17.%20Letter%20Combinations%20of%20a%20Phone%20Number/) | $$O(n \cdot 4^n)$$ | $$O(n)$$ |
| [**22. Generate Parentheses**](https://leetcode.com/problems/generate-parentheses/description/) | [Link](/22.%20Generate%20Parentheses/) | $$O(2^n)$$ | $$O(n)$$ |
| [**45. Jump Game II**](https://leetcode.com/problems/jump-game-ii/description/) | [Link](/45.%20Jump%20Game%20II/) | $$O(n)$$ | $$O(1)$$ |
| [**79. Word Search**](https://leetcode.com/problems/word-search/) | [Link](/79.%20Word%20Search/) | $$O(m \cdot n \cdot 3^w)$$ | $$O(w)$$ |

### Graphs

| Problem | Solutions | Time | Space |
| ------------------------------------------------------------------------------------------------------------- | -------------------------------------------------------- | --------------- | --------------- |
| [1971. Find if Path Exists in Graph](https://leetcode.com/problems/find-if-path-exists-in-graph/) | [Link](/1971.%20Find%20if%20Path%20Exists%20in%20Graph/) | $$O(V + E)$$ | $$O(V + E)$$ |
| [**200. Number of Islands**](https://leetcode.com/problems/number-of-islands/description/) | [Link](/200.%20Number%20of%20Islands/) | $$O(m\cdot n)$$ | $$O(m\cdot n)$$ |
| [695. Max Area of Island](https://leetcode.com/problems/max-area-of-island/description/) | [Link](/695.%20Max%20Area%20of%20Island/) | $$O(m\cdot n)$$ | $$O(m\cdot n)$$ |
| [207. Course Schedule (Detect Cycles in a Graph)](https://leetcode.com/problems/course-schedule/description/) | [Link](/207.%20Course%20Schedule/) | $$O(V + E)$$ | $$O(V + E)$$ |

### Dynamic Programming

### Bit Manipulation