My solutions for LeetCode problems.
First, install dependencies:
npm install
To run tests with jest:
npm run test
To run tests and generate code coverage report:
npm run test:coverage
- Find your problem in the problems folder.
- Copy the solution.
- Paste the code into LeetCode.
- Hit the Run or Submit button.
The folder structure in this repository resembles the URL for LeetCode's problems, e.g.:
LeetCode URL for Two Sum problem: https://leetcode.com/problems/two-sum
Folder structure in this repository: https://github.com/ecgan/leetcode/tree/master/problems/two-sum
The listing below is sorted based on LeetCode #. If you are interested to see my latest solutions in chronological order, check out the releases page, or my code commits.
LeetCode # | Title | Difficulty | Topics |
---|---|---|---|
1 | Two Sum | Easy | Array, hash table |
33 | Search in Rotated Sorted Array | Medium | Array, Binary Search |
35 | Search Insert Position | Easy | Array, binary search |
49 | Group Anagrams | Medium | Hash Table, String |
53 | Maximum Subarray | Easy | Array, Divide and Conquer, Dynamic Programming |
55 | Jump Game | Medium | Array, Greedy |
60 | Permutation Sequence | Medium | Math, Backtracking |
62 | Unique Paths | Medium | Array, Dynamic Programming |
64 | Minimum Path Sum | Medium | Array, Dynamic Programming |
70 | Climbing Stairs | Easy | Dynamic Programming |
72 | Edit Distance | Hard | String, Dynamic Programming |
75 | Sort Colors | Medium | Array, Two Pointers, Sort |
96 | Unique Binary Search Trees | Medium | Dynamic Programming, Tree |
121 | Best Time to Buy and Sell Stock | Easy | Array, Dynamic Programming |
122 | Best Time to Buy and Sell Stock II | Easy | Array, Greedy |
124 | Binary Tree Maximum Path Sum | Hard | Tree, Depth-first Search |
129 | Sum Root to Leaf Numbers | Medium | Tree, Depth-first Search |
130 | Surrounded Regions | Medium | Depth-first Search, Breadth-first Search, Union Find |
136 | Single Number | Easy | Hash table, bit manipulation |
137 | Single Number II | Medium | Bit Manipulation |
146 | LRU Cache | Medium | Design |
155 | Min Stack | Easy | Stack, Design |
169 | Majority Element | Easy | Array, Divide and Conquer, Bit Manipulation |
189 | Rotate Array | Easy | Array |
198 | House Robber | Easy | Dynamic Programming |
200 | Number of Islands | Medium | Depth-first Search, Breadth-first Search, Union Find |
201 | Bitwise AND of Numbers Range | Medium | Bit Manipulation |
202 | Happy Number | Easy | Hash Table, Math |
207 | Course Schedule | Medium | Depth-first Search, Breadth-first Search, Graph, Topological Sort |
208 | Implement Trie (Prefix Tree) | Medium | Design, Trie |
212 | Word Search II | Hard | Backtracking, Trie |
221 | Maximal Square | Medium | Dynamic Programming |
222 | Count Complete Tree Nodes | Medium | Binary Search, Tree |
226 | Invert Binary Tree | Easy | Tree |
230 | Kth Smallest Element in a BST | Medium | Binary Search, Tree |
231 | Power of Two | Easy | Math, Bit Manipulation |
237 | Delete Node in a Linked List | Easy | Linked List |
238 | Product of Array Except Self | Medium | Array |
275 | H-Index II | Medium | Binary Search |
278 | First Bad Version | Easy | Binary Search |
279 | Perfect Squares | Medium | Math, Dynamic Programming, Breadth-first Search |
283 | Move Zeroes | Easy | Array, Two Pointers |
287 | Find the Duplicate Number | Medium | Array, Two Pointers, Binary Search |
303 | Range Sum Query - Immutable | Easy | Dynamic Programming |
328 | Odd Even Linked List | Medium | Linked List |
332 | Reconstruct Itinerary | Medium | Depth-first Search, Graph |
338 | Counting Bits | Medium | Dynamic Programming, Bit Manipulation |
344 | Reverse String | Easy | Two Pointers, String |
349 | Intersection of Two Arrays | Easy | Hash table, two pointers, binary search, sort, set |
350 | Intersection of Two Arrays II | Easy | Hash Table, two pointers, binary search, sort |
367 | Valid Perfect Square | Easy | Math, Binary Search |
368 | Largest Divisible Subset | Medium | Math, Dynamic Programming |
380 | Insert Delete GetRandom O(1) | Medium | Array, Hash Table, Design |
383 | Ransom Note | Easy | String |
387 | First Unique Character in a String | Easy | Hash Table, String |
392 | Is Subsequence | Easy | Binary Search, Dynamic Programming, Greedy |
402 | Remove K Digits | Medium | Stack, Greedy |
406 | Queue Reconstruction by Height | Medium | Greedy |
438 | Find All Anagrams in a String | Medium | Hash Table |
451 | Sort Characters By Frequency | Medium | Hash Table, Heap |
468 | Validate IP Address | Medium | String |
476 | Number Complement | Easy | Bit Manipulation |
518 | Coin Change 2 | Medium | - |
525 | Contiguous Array | Medium | Hash Table |
528 | Random Pick with Weight | Medium | Binary Search, Random |
540 | Single Element in a Sorted Array | Medium | - |
543 | Diameter of Binary Tree | Easy | Tree |
560 | Subarray Sum Equals K | Medium | Array, Hash Table |
567 | Permutation in String | Medium | Two Pointers, Sliding Window |
678 | Valid Parenthesis String | Medium | String |
700 | Search in a Binary Search Tree | Easy | Tree |
714 | Dungeon Game | Hard | Binary Search, Dynamic Programming |
733 | Flood Fill | Easy | Depth-first Search |
746 | Min Cost Climbing Stairs | Easy | Array, Dynamic Programming |
771 | Jewels and Stones | Easy | Hash Table |
787 | Cheapest Flights Within K Stops | Medium | Dynamic Programming, Heap, Breadth-first Search |
844 | Backspace String Compare | Easy | Two Pointers, Stack |
876 | Middle of the Linked List | Easy | Linked List |
886 | Possible Bipartition | Medium | Depth-first Search |
901 | Online Stock Span | Medium | Stack |
918 | Maximum Sum Circular Subarray | Medium | Array |
973 | K Closest Points to Origin | Medium | Divide and Conquer, Heap, Sort |
986 | Interval List Intersections | Medium | Two Pointers |
993 | Cousins in Binary Tree | Easy | Tree, Breadth-first Search |
997 | Find the Town Judge | Easy | Graph |
1008 | Construct Binary Search Tree from Preorder Traversal | Medium | Tree |
1025 | Divisor Game | Easy | Math, Dynamic Programming |
1029 | Two City Scheduling | Easy | Greedy |
1035 | Uncrossed Lines | Medium | Array |
1044 | Longest Duplicate Substring | Hard | Hash Table, Binary Search |
1046 | Last Stone Weight | Easy | Heap, Greedy |
1143 | Longest Common Subsequence | Medium | Dynamic Programming |
1144 | Decrease Elements To Make Array Zigzag | Medium | Array |
1145 | Binary Tree Coloring Game | Medium | Tree, depth-first search |
1150 | Check If a Number Is Majority Element in a Sorted Array | Easy | Array, binary search |
1153 | String Transforms Into Another String | Hard | Graph |
1154 | Day of the Year | Easy | Math |
1160 | Find Words That Can Be Formed by Characters | Easy | Array, hash table |
1161 | Maximum Level Sum of a Binary Tree | Medium | Graph |
1162 | As Far from Land as Possible | Medium | Breadth-first search, graph |
1163 | Last Substring in Lexicographical Order | Hard | String, suffix array |
1165 | Single-Row Keyboard | Easy | String |
1167 | Minimum Cost to Connect Sticks | Medium | Greedy |
1170 | Compare Strings by Frequency of the Smallest Character | Easy | Array, string |
1177 | Can Make Palindrome from Substring | Medium | Array, string |
1184 | Distance Between Bus Stops | Easy | Array |
1185 | Day of the Week | Easy | Array |
1207 | Unique Number of Occurrences | Easy | Hash table |
1208 | Get Equal Substrings Within Budget | Medium | Array, sliding window |
1213 | Intersection of Three Sorted Arrays | Easy | Hash table, two pointers |
1214 | Two Sum BSTs | Medium | Binary search tree |
1217 | Play with Chips | Easy | Array, math, greedy |
1232 | Check If It Is a Straight Line | Easy | Array, Math, Geometry |
1243 | Array Transformation | Easy | Array |
1244 | Design A Leaderboard | Medium | Hash table, sort, design |
1247 | Minimum Swaps to Make Strings Equal | Medium | String, greedy |
1256 | Encode Number | Medium | Math, bit manipulation |
1260 | Shift 2D Grid | Easy | Array |
1261 | Find Elements in a Contaminated Binary Tree | Medium | Hash table, tree |
1271 | Hexspeak | Easy | Math, string |
1272 | Remove Interval | Medium | Math, line sweep |
1275 | Find Winner on a Tic Tac Toe Game | Easy | Array |
1276 | Number of Burgers with No Waste of Ingredients | Medium | Math, greedy |
1277 | Count Square Submatrices with All Ones | Medium | Array, Dynamic Programming |
1313 | Decompress Run-Length Encoded List | Easy | Array |
1314 | Matrix Block Sum | Medium | Dynamic programming |
1317 | Convert Integer to the Sum of Two No-Zero Integers | Easy | Math |
1342 | Number of Steps to Reduce a Number to Zero | Easy | Bit manipulation |
1343 | Number of Sub-arrays of Size K and Average Greater than or Equal to Threshold | Medium | Array |
1344 | Angle Between Hands of a Clock | Medium | Math |
1346 | Check If N and Its Double Exist | Easy | Array |
1347 | Minimum Number of Steps to Make Two Strings Anagram | Medium | String |
1348 | Tweet Counts Per Frequency | Medium | Design |
1356 | Sort Integers by The Number of 1 Bits | Easy | Sort, bit manipulation |
1357 | Apply Discount Every n Orders | Medium | Design |
1358 | Number of Substrings Containing All Three Characters | Medium | String |
1360 | Number of Days Between Two Dates | Easy | - |
1361 | Validate Binary Tree Nodes | Medium | Graph |
1362 | Closest Divisors | Medium | Math |
1370 | Increasing Decreasing String | Easy | String, sort |
1374 | Generate a String With Characters That Have Odd Counts | Easy | String |
1385 | Find the Distance Value Between Two Arrays | Easy | Array |
1386 | Cinema Seat Allocation | Medium | Array, Greedy |
1387 | Sort Integers by The Power Value | Medium | Sort, Graph |
1389 | Create Target Array in the Given Order | Easy | Array |
1390 | Four Divisors | Medium | Math |
1401 | Circle and Rectangle Overlapping | Medium | Geometry |
1404 | Number of Steps to Reduce a Number in Binary Representation to One | Medium | String, Bit Manipulation |
1413 | Minimum Value to Get Positive Step by Step Sum | Easy | Array |
1417 | Reformat The String | Easy | String |
1418 | Display Table of Food Orders in a Restaurant | Medium | Hash Table |
1419 | Minimum Number of Frogs Croaking | Medium | String |
1426 | Counting Elements | - | - |
1427 | Perform String Shifts | - | - |
1428 | Leftmost Column with at Least a One | - | - |
1429 | First Unique Number | - | - |
1430 | Check If a String Is a Valid Sequence from Root to Leaves Path in a Binary Tree | - | - |
1475 | Final Prices With a Special Discount in a Shop | Easy | Array |
1476 | Subrectangle Queries | Medium | Array |
1480 | Running Sum of 1d Array | Easy | Array |
1481 | Least Number of Unique Integers after K Removals | Medium | Array, Sort |
1491 | Average Salary Excluding the Minimum and Maximum Salary | Easy | Array, Sort |
1492 | The kth Factor of n | Medium | Math |
1493 | Longest Subarray of 1's After Deleting One Element | Medium | Array |
1572 | Matrix Diagonal Sum | Easy | Array |
1574 | Shortest Subarray to be Removed to Make Array Sorted | Medium | Array, Binary Search |
1576 | Replace All ?'s to Avoid Consecutive Repeating Characters | Easy | String |
1577 | Number of Ways Where Square of Number Is Equal to Product of Two Numbers | Medium | Hash Table, Math |
1578 | Minimum Deletion Cost to Avoid Repeating Letters | Medium | Greedy |
1603 | Design Parking System | Easy | Design |
1604 | Alert Using Same Key-Card Three or More Times in a One Hour Period | Medium | String, Ordered Map |
1608 | Special Array With X Elements Greater Than or Equal X | Easy | Array |
1609 | Even Odd Tree | Medium | Tree |
If you have any question, feel free to open a GitHub issue and reach out to me.