Skip to content

DmitryNaimark/leetcode-solutions

Repository files navigation

LeetCode Solutions (JS)

This repository contains solutions for LeetCode problems I've solved(using JS), links to official(and clever unofficial) solutions.

Solutions are grouped by Topics(see topics folder).

Feel free to Fork, Clone or Star this repository.

Also, check out my Python Repository, which contains solutions in Python.

Difficulty Title Tags My Solutions LeetCode Solution Other cool solutions Solved on my own? Date
1. Two Sum HashTable, Array Yes 2018-10-01
7. Reverse Integer Reverse, Math, String Using string Using Math Yes 2018-10-02
9. Palindrome Number Reverse, Math, Palindrome Yes 2018-10-02
13. Roman to Integer HashTable, Math - Yes 2018-10-03
14. Longest Common Prefix String Yes 2018-10-03
914. X of a Kind in a Deck of cards GCD, Math, HashTable No, almost (couldn't figure good GCD formula) 2018-10-07
2. Add Two Numbers LinkedList Yes 2018-10-07
3. Longest Substring Without Repeating Characters String, HashTable Yes, but suboptimal 2018-10-11
4. Median of Two Sorted Arrays Array, Median [YouTube] Median of two sorted arrays - standard solution Yes, but suboptimal 2018-10-12
5. Longest Palindromic Substring String, Palindrome Manacher's Algorithm (O(N)) Yes 2018-10-13
6. ZigZag Conversion String Yes 2018-10-14
8. String to Integer (atoi) String, Number - Yes 2018-10-14
925. Long Pressed Name String Yes 2018-10-21
926. Flip String to Monotone Increasing Math, String 1) "Sliding" + counting Prefix Sums,

2) Counting while iterating
No, wrongly discarded thought to try all possible solutions 2018-10-23
771. Jewels and Stones HashTable - Yes 2018-11-19
933. Number of Recent Calls Array, Queue Naive, slow solution Using Queue - much better Yes, but subopt., it's better to use Queue 2018-11-20
937. Reoder Log Files String Official solution is pretty slow Yes 2018-11-24
938. Range Sum of BST Tree, BST Using Outside "sum" variable Without Outside "sum" variable Interesting iterative solution using Stack Yes 2018-11-24
941. Valid Mountain Array Array Yes 2018-11-24
944. Delete Columns to Make Sorted Array Yes 2018-11-24
945. Minimum Increment to Make Array Unique Array Yes, but there is faster solution (see Sol. 1) 2018-12-06
946. Validate Stack Sequences Stack, Simulation No, didn't get how to simulate the process 2018-12-07
949. Largest Time for Given Digits Array, Permu-
tations
1) Generating permut-s in descending order,

2) Using 3 for loops instead of permut-s,

3) Interesting idea with limits in separate array
Yes, but with hardcoded permut-s 2018-12-08
950. Reveal Cards In Increasing Order Stack, Simulation Slightly optimized Almost, still didn't figure how to simulate the process 2018-12-10
951. Flip Equivalent Binary Trees Tree Yes 2018-12-12
226. Invert Binary Tree Tree Using Recursion Using Queue Yes 2018-12-13
953. Verifying an Alien Dictionary HashTable Yes 2018-12-15
954. Array of Doubled Pairs HashTable Yes 2018-12-15
955. Delete Columns to Make Sorted II HashTable Yes 2018-12-15
20. Valid Parentheses Stack, String, HashTable Yes 2019-08-15
26. Remove Duplicates from Sorted Array Array Yes 2019-08-15
27. Remove Element Array, Two Pointers Copy from the end Yes 2019-08-15
28. Implement strStr() String, Search, Rabin-Karp, KMP Brute-force Knuth-Morris-Pratt - KMP Rabin-Karp - KMP, Rabin-Karp 2019-08-20
453. Minimum Moves to Equal Array Elements Array, Math - Yes 2019-08-21
342. Power of Four Bitwise - Bitwise and radix 4 2019-08-21
326. Power of Three Bitwise 1) List of several solutions Bitwise, radix of 3 2019-08-21
557. Reverse Words in a String III String Yes 2019-08-21
434. Number of Segments in a String String, Regex Manual for loop to search for non-space after space .trim() or .replace() to remove spaces, split by regex (1 or more spaces) Yes 2019-08-21
1052. Grumpy Bookstore Owner Array, Sliding Window 2 for loops, first one calculates initial sliding window value(clumsy) No calculation for initial sliding window value(improved), but still 2 for loops(which is ok) Improved Sliding Window - only 1 for loop, logic for calculating happy sum and max window sum is mixed. - 1) Good example of not calculating initial window sum Sliding Window - w/o initial calculation 2019-08-21
238. Product of Array Except Self Array, Prefix Product, Suffix Product Two arrays: prefix products and suffix products Calculate prefix and suffix products in place Prefix Product, Suffix Product 2019-08-22
746. Min Cost Climbing Stairs DP, Sliding min Sum, Recursion O(1) Space, but we're modifying original array Recursive with memoization, O(N) Space O(1) Space without changing original array, by using Sliding sum 1) Changing array in place Interesting sliding min sum, recursion 2019-08-22
70. Climbing Stairs DP, Sliding Sum, Recursion Brute-force recursion in 2^N Brute-force recursion with memoization - O(N) DP with Space O(N) array Sliding sum for O(1) space with O(N) Runtime Sliding Sum, DP 2019-08-22
559. Maximum Depth of N-ary Tree Tree, BFS, DFS, Queue Single queue Two queues - one for current level, one for next level DFS DFS - 1) Using single queue for BFS DFS with single queue 2019-08-22
104. Maximum Depth of Binary Tree Tree, BFS, DFS, Queue - 1) Shoft recursive DFS Shoft DFS, BFS with single queue 2019-08-22
278. First Bad Version Array, Binary Search With additional var Without additional var, different while condition and return. Binary search(no overflow), w/o additional var 2019-08-23
38. Count and Say String - 1) Commenters discuss Runtime Complexity Yes 2019-08-27
35. Search Insert Position Array, Binary Search - 1) Simply return low at the end Array Binary Search 2019-08-28
203. Remove Linked List Elements LinkedList - 1) Short recursive solution,

2) Short iterative without dummy head,

3) Iterative with Dummy head
Dummy head, Recursive, Iterative 2019-08-28
141. Linked List Cycle LinkedList, Tortoise and Hare Tortoise and Hare 2019-08-29
206. Reverse Linked List LinkedList, Reverse Iterative, Recursive 2019-08-29
83. Remove Duplicates from Sorted List LinkedList Hanlde memory leak 2019-08-29
160. Intersection of Two Linked Lists LinkedList, Two pointers 1. Compare lengths. 2. Go till the end, then start from other list. 2019-08-29
67. Add Binary Math, Binary - Binary 2019-08-29
171. Excel Sheet Column Number Math, Base 26 - Base 26 to Base 10 2019-08-29
728. Self Dividing Numbers Math Brute-force is the only way 2019-08-29
485. Max Consecutive Ones Array - Simple counting 2019-08-29
997. Find the Town Judge Graph - Graph 2019-08-29
543. Diameter of Binary Tree Tree, DFS, BFS 1) Iterative BFS solution Height is not Depth (see comments in off. solution) 2019-08-30
136. Single Number Bitwise, XOR, Math XOR 2019-08-30
101. Symmetric Tree Tree, DFS, BFS, Queue BFS - extracting 2 elements from the queue 2019-08-30
121. Best Time to Buy and Sell Stock Array, DP (kind of), Rolling DP with O(1) space, rolling min 2019-08-30
66. Plus One Math - Can be done without carry 2019-08-30
581. Shortest Unsorted Continuous Subarray Array, Stack, Peaks and Falls 1) Short 1 loop solution (more readable 2 loops in the comments) Stack, Rolling min/max, Tricky 2019-09-03
169. Majority Element Array, HashTable, Divide and Conquer, Bitwise, Boyer-Moore Voting Algorithm 1) Bit manipulation solution Divide and Conquer, Boyer-Moore Voting Algorithm, Bitwise majority mask, Sorting trick 2019-09-03
198. House Robber DP - 1) Recursive, with Memo, DP with array, DP with vars DP, many ways of starting and ending iteraction, vars only 2019-09-03
412. Fizz Buzz String Divisible by 3 and 5 is divisible by 15 (prime numbers) 2019-09-04
437. Path Sum III Tree, Prefix Sum, DFS, Paths Sums - 1) Most optimal solution with Prefix Sums,

2) Good Space/Runtime estimations explanations,

3) Very suboptimal solution, beware
Prefix Sums for paths sums in Tree, while DFS-ing 2019-09-04
1171. Remove Zero Sum Consecutive Nodes from Linked List LinkedList, Prefix Sums, HashTable, Removal - 1) Solution using HashTable,

2) Good illustration
1. Prefix Sums + HashTable in Linked List 2. Removal in Linked List 2019-09-04
88. Merge Sorted Array Array - Simple 3 pointers 2019-09-05
268. Missing Number Math, Binary Search, Bitwise XOR, Arithmetic Progr. 1) Interesting Binary Search idea (suboptimal) 1. Arithmetic progr., 2. Bitwise XOR, 3. Binary Search 2019-09-05
237. Delete Node in a Linked List LinkedList Brain teaser - copy value to the left instead of removing 2019-09-05
94. Binary Tree Inorder Traversal Tree, BFS with Stack, BFS, Morris Traversal 1. BFS with Stack, 2. DFS, 3. Morris Traversal 2019-09-06
387. First Unique Character in a String String, HashTable Iterate 2nd time over HashTable or original string 2019-09-06
118. Pascal's Triangle DP Simple DP 2019-09-06
680. Valid Palindrome II String, Palindrome Palindrome (not scrambled) 2019-09-09
1122. Relative Sort Array Array, Sorting, HashTable, Count Array - 1) L1 log(L1) + L2 sorting solution,

2) Helper count array
1. Helper count array, 2. Sorting by key from HashTable 2019-09-09
108. Convert Sorted Array to Binary Search Tree Tree, BST, Binary Search - 1) Iterative solution with 3 stacks BST combined with tree generation, interesting iterative solution 2019-09-09
172. Factorial Trailing Zeroes Math, Factors, divisors - 1) Good explanation - why looking for 5 works Interesting brainstorm math, factors 2019-09-10
189. Rotate Array Array, Shift, swap, pop, push Many ways to solve: reverse, jump and swap, slice, pop and push 2019-09-10
350. Intersection of Two Arrays II Array, Intersect. - Simple HashTable 2019-09-10
560. Subarray Sum Equals K Array, HashTable, Rolling Sums, Math 1) Why 2 pointers won't work in this case (negative numbers) HashTable with Rolling Sums, tricky idea to get number of rolling sums 2019-09-11
190. Reverse Bits Bitwise, Binary - 1) Good JS explanation, "res >>> 0" at the end. Logical >>> and Arithmetic >> shifts, Bitwise operations 2019-09-11
122. Best Time to Buy and Sell Stock II Array, Peak and Valleys Interesting "Next Peak and Valley" approach 2019-09-11
53. Maximum Subarray Array, DP, Recrursion, Memo, Rolling Sum - Rolling Sum, DP, Recursion, Memo 2019-09-12
152. Maximum Product Subarray Array, Rolling Products - 1) Swap min and max,

2) Iterate left to right and right to left
Rolling min/max products, min/max swap 2019-09-12
110. Balanced Binary Tree Tree, BFS, DFS - 1) (!) Tricky Iterative BFS,

2) Suboptimal DFS, 2 different funcs
(!) Tricky Iterative BFS, DFS with return -1 or res obj 2019-09-14
263. Ugly Number Math, Prime factors - 1) Good use of JS "for of" loop to iterate over array [2, 3, 5] Getting Prime Factors 2019-09-16
1160. Find Words That Can Be Formed by Characters String, HashTable - Simple HashTable and counting 2019-09-17
134. Gas Station Array, Brain Teaser, Tricky 1) Answer proof Very tricky, Brain Teaser 2019-09-17
704. Binary Search Array, Binary Search Simple binary search, 2 ways to solve using different Invariants 2019-09-17
153. Find Minimum in Rotated Sorted Array Array, Binary Search Interesting Invariants in Binary Search 2019-09-17
345. Reverse Vowels of a String String, Two Pointers, Stack - Two pointers or less optimal trick with Stack 2019-09-17
292. Nim Game Math, Brain Teaser Brain Teaser 2019-09-17
589. N-ary Tree Preorder Traversal Tree, DFS, Iterative DFS Iterative DFS 2019-09-18
112. Path Sum Tree, DFS, Iterative, Recursive, Stack Iterative traversals using stack 2019-09-19
530. Minimum Absolute Difference in BST Tree, In-order Traversal - 1) Iterative In-order Traversal in BT Iterative In-order Traversal 2019-09-19
744. Find Smallest Letter Greater Than Target String, Binary Search Simple Binary Search 2019-09-19
107. Binary Tree Level Order Traversal II Tree, Iterative BFS, Recursive DFS - Recursive DFS passing level, to make BFS-like processing 2019-09-19
102. Binary Tree Level Order Traversal Tree, Iterative BFS, Recursive DFS Recursive DFS passing level, to make BFS-like processing 2019-09-19
709. To Lower Case String, RegExp - Simple RegExp or char from/to code 2019-09-20
119. Pascal's Triangle II Math, DP - 1) Math formula for specific row/cell in Pascal's triangle 1. Iterating back, not to store the previous original value 2. Pascal's Triangle Math Formula 2019-09-21
669. Trim a Binary Search Tree Tree, BST, Deleting, DFS, Recursive, Iterative 1) Iterative solution Deleting nodes inbetween, interesting Recursive and Iterative solutions 2019-09-23
836. Rectangle Overlap Math, Geometry, Rect, Intersect., Area Check if it's outside or find intersection area (square) 2019-09-23
111. Minimum Depth of Binary Tree Tree, Iterative BFS, Iterative DFS, Recursive DFS Iterative DFS, BFS is more efficient for this problem 2019-09-24
33. Search in Rotated Sorted Array Array, Binary Search Find min in rotated array, then binary or combined 1 pass 2019-09-24
716. Max Stack Stack, TreeMap, Doubly Linked List 1. Additional Max Stack 2. Interesting Doubly Linked List + TreeMap official solution 2019-09-25
875. Koko Eating Bananas Array, Binary Search, Trial and Error Trial and Error using Binary Search 2019-09-25
811. Subdomain Visit Count HashTable, String, Substring .indexOf is better than .split 2019-09-25
1110. Delete Nodes And Return Forest Tree, DFS, Disjoint, Delete, Subtree - 1) Good explanation about passing info from parent to child and vice versa Communic. between child and parent to make code more concise 2019-09-26
221. Maximal Square DP, Matrix 1) Good optimizing process, using vector instead of sinle row "2D" DP in matrix 2019-09-26
346. Moving Average from Data Stream Queue, Array, Linked List - Queue using Array or LinkedList 2019-09-28
204. Count Primes Math, Primes, Sieve of Eratos. - 1) Clean Sieve of Eratos. solution,

2) Sieve of Eratos. explained
Primes, Sieve of Eratos. 2019-09-30
538. Convert BST to Greater Tree Tree, BST, Reverse Morris In-Order Traversal, DFS Reverse DFS Traversal, Iterative, Recursive with sharedObj and parameters 2019-09-30
819. Most Common Word String, RegEx, HashTable RegEx (split by "inverted" RegEx), simple HashTable count 2019-09-30
293. Flip Game String, Substring - 1) .indexOf in for loop - interesting idea Simple substring problem 2019-09-30
383. Ransom Note HashTable, Chars array counter - Prefix decrement in if for array item 2019-10-01
461. Hamming Distance Bitwise, Brian Kernighan bit counting, XOR - 1) XOR and Brian Kernighan's bit counting XOR, Brian Kernighan's bit counting 2019-10-01
243. Shortest Word Distance Array, Min Dist Simple min dist 2019-10-01
1119. Remove Vowels from a String String, RegEx - Replace RegEx or HashSet 2019-10-02
616. Add Bold Tag in String Intervals, Intervals Merge, String, Substring, Mask, indexOf, `` 1. String mask, 2. trick "while ((i = indexOf(...)) >= 0)", 3. Merging intervals 2019-10-03
15. 3Sum Array, Sorting, Two Pointers, HashTable - 1) Sorting, two pointers solution explained Sorting -> two pointers, a lot of tricks to stop/break earlier and skip 2019-10-03
11. Container With Most Water Array, Two Pointers Interesting Two Pointers application 2019-10-03
371. Sum of Two Integers Bitwise, Addition - 1) A lot of Bitwise techniques Addition using &, ^, << 2019-10-04
908. Smallest Range I Array, Math Simple math, slightly confusing question 2019-10-04
1215. Stepping Numbers Tree, Digits, BFS, Queue - Iterative BFS - sorting isn't needed (in contrast to DFS) 2019-10-07
883. Projection Area of 3D Shapes Array, 3D, Projection Trick with swapping r, c, since matrix is NxN 2019-10-08
338. Counting Bits Bitwise, DP, Least signif. bit, Most signif. bit DP and bitwise, bits rules 2019-10-10
276. Paint Fence DP, Probability, Math - 1) Decent explanation Probability, DP 2019-10-16
496. Next Greater Element I Stack, Monotonic Stack Monotonic Stack 2019-10-17
1232. Check If It Is a Straight Line Geometry, Math - 1) Check Slopes,

2) Is Triangle area == 0
Formulas: Slopes, Triangle area 2019-10-21
1233. Remove Sub-Folders from the Filesystem String, Substring, HashSet, Sorting - 1) 1. Sort by length -> use HashSet 2. Sort lexic. -> compare only with previous potential parent Difficult time/space estim., interesting solutions 2019-10-22
232. Implement Queue using Stacks Stack, Queue 2 Stacks(1 reversed) to get amortized O(1) pop 2019-10-22
674. Longest Continuous Increasing Subseq. Array, Subseq. - Anchor technique, Sliding Window 2019-10-22
120. Triangle DP, Bottom-Up DP - 1) Bottom-up explanation DP Bottom-Up, In-place 2019-10-24
1022. Sum of Root To Leaf Binary Numbers Tree, Recursive, Iterative, Stack - Stack with Tuple or 2 values, Iterative DFS 2019-10-24
129. Sum Root to Leaf Numbers Tree, Recursive, Iterative, Stack - Same as #1022 2019-10-24
124. Binary Tree Maximum Path Sum Tree, Recursive, Path Max gain idea: left_sum = max (left_sum, 0), to shorten code 2019-10-25
1171. Remove Zero Sum Consecutive Nodes from Linked List LinkedList, HashTable, Prefix, Sum, Delete - 1) Smart two-pass solution (last),

2) Explanation picture
No memory leak in GC, smart two pass 2019-10-29
733. Flood Fill Matrix, DFS, BFS, Iterative, Recursive Good template for DFS, BFS problems 2019-10-29
443. String Compress. String, Two Pointers - Iterate and check the next to handle last char, Iterate over digits without split 2019-10-29
138. Copy List with Random Pointer LinkedList, HashTable, Recursive 1) Great one pass, two pass code,

2) O(1) official solution explained
No need for new List, store nodes only in Map. 2019-10-31
594. Longest Harmonious Subseq. Array, HashTable, Sorting Tricky iteration after Sorting 2019-11-04
548. Split Array with Equal Sum Array, HashTable, Prefix Sum Prefix sums, HashTable, Skip recalc. Zeros 2019-11-06

About

JavaScript Solutions for LeetCode problems.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages