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

https://github.com/ecgan/leetcode

My JavaScript solutions for LeetCode problems. Tests with 100% code coverage.
https://github.com/ecgan/leetcode

algorithms data-structures javascript leetcode leetcode-solutions

Last synced: about 2 months ago
JSON representation

My JavaScript solutions for LeetCode problems. Tests with 100% code coverage.

Awesome Lists containing this project

README

          

# leetcode

[![Build Status](https://travis-ci.org/ecgan/leetcode.svg?branch=master)](https://travis-ci.org/ecgan/leetcode) [![codecov](https://codecov.io/gh/ecgan/leetcode/branch/master/graph/badge.svg)](https://codecov.io/gh/ecgan/leetcode) [![JavaScript Style Guide](https://img.shields.io/badge/code_style-standard-brightgreen.svg)](https://standardjs.com)

My solutions for [LeetCode problems](https://leetcode.com/problemset/all/).

## Usage

### Running and testing locally in your machine

First, install dependencies:

```shell
npm install
```

To run tests with jest:

```shell
npm run test
```

To run tests and generate code coverage report:

```shell
npm run test:coverage
```

### Submitting / Running in LeetCode

1. Find your problem in the [problems](/problems) folder.
2. Copy the solution.
3. Paste the code into LeetCode.
4. Hit the Run or Submit button.

### Pro-tip: URL / Folder Structure

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**

## Solutions

The listing below is sorted based on LeetCode #. If you are interested to see my latest solutions in chronological order, check out the [releases](https://github.com/ecgan/leetcode/releases) page, or my [code commits](https://github.com/ecgan/leetcode/commits/master).

| LeetCode # | Title | Difficulty | Topics |
|-----------:|:------|:-----------|:-------|
| 1 | [Two Sum](/problems/two-sum) | Easy | Array, hash table |
| 33 | [Search in Rotated Sorted Array](/problems/search-in-rotated-sorted-array) | Medium | Array, Binary Search |
| 35 | [Search Insert Position](/problems/search-insert-position) | Easy | Array, binary search |
| 49 | [Group Anagrams](/problems/group-anagrams) | Medium | Hash Table, String |
| 53 | [Maximum Subarray](/problems/maximum-subarray) | Easy | Array, Divide and Conquer, Dynamic Programming |
| 55 | [Jump Game](/problems/jump-game) | Medium | Array, Greedy |
| 60 | [Permutation Sequence](/problems/permutation-sequence) | Medium | Math, Backtracking |
| 62 | [Unique Paths](/problems/unique-paths) | Medium | Array, Dynamic Programming |
| 64 | [Minimum Path Sum](/problems/minimum-path-sum) | Medium | Array, Dynamic Programming |
| 70 | [Climbing Stairs](/problems/climbing-stairs) | Easy | Dynamic Programming |
| 72 | [Edit Distance](/problems/edit-distance) | Hard | String, Dynamic Programming |
| 75 | [Sort Colors](/problems/sort-colors) | Medium | Array, Two Pointers, Sort |
| 96 | [Unique Binary Search Trees](/problems/unique-binary-search-trees) | Medium | Dynamic Programming, Tree |
| 121 | [Best Time to Buy and Sell Stock](/problems/best-time-to-buy-and-sell-stock) | Easy | Array, Dynamic Programming |
| 122 | [Best Time to Buy and Sell Stock II](/problems/best-time-to-buy-and-sell-stock-ii) | Easy | Array, Greedy |
| 124 | [Binary Tree Maximum Path Sum](/problems/binary-tree-maximum-path-sum) | Hard | Tree, Depth-first Search |
| 129 | [Sum Root to Leaf Numbers](/problems/sum-root-to-leaf-numbers) | Medium | Tree, Depth-first Search |
| 130 | [Surrounded Regions](/problems/surrounded-regions) | Medium | Depth-first Search, Breadth-first Search, Union Find |
| 136 | [Single Number](/problems/single-number) | Easy | Hash table, bit manipulation |
| 137 | [Single Number II](/problems/single-number-ii) | Medium | Bit Manipulation |
| 146 | [LRU Cache](/problems/lru-cache) | Medium | Design |
| 155 | [Min Stack](/problems/min-stack) | Easy | Stack, Design |
| 169 | [Majority Element](/problems/majority-element) | Easy | Array, Divide and Conquer, Bit Manipulation |
| 189 | [Rotate Array](/problems/rotate-array) | Easy | Array |
| 198 | [House Robber](/problems/house-robber) | Easy | Dynamic Programming |
| 200 | [Number of Islands](/problems/number-of-islands) | Medium | Depth-first Search, Breadth-first Search, Union Find |
| 201 | [Bitwise AND of Numbers Range](/problems/bitwise-and-of-numbers-range) | Medium | Bit Manipulation |
| 202 | [Happy Number](/problems/happy-number) | Easy | Hash Table, Math |
| 207 | [Course Schedule](/problems/course-schedule) | Medium | Depth-first Search, Breadth-first Search, Graph, Topological Sort |
| 208 | [Implement Trie (Prefix Tree)](/problems/implement-trie-prefix-tree) | Medium | Design, Trie |
| 212 | [Word Search II](/problems/word-search-ii) | Hard | Backtracking, Trie |
| 221 | [Maximal Square](/problems/maximal-square) | Medium | Dynamic Programming |
| 222 | [Count Complete Tree Nodes](/problems/count-complete-tree-nodes) | Medium | Binary Search, Tree |
| 226 | [Invert Binary Tree](/problems/invert-binary-tree) | Easy | Tree |
| 230 | [Kth Smallest Element in a BST](/problems/kth-smallest-element-in-a-bst) | Medium | Binary Search, Tree |
| 231 | [Power of Two](/problems/power-of-two) | Easy | Math, Bit Manipulation |
| 237 | [Delete Node in a Linked List](/problems/delete-node-in-a-linked-list) | Easy | Linked List |
| 238 | [Product of Array Except Self](/problems/product-of-array-except-self) | Medium | Array |
| 275 | [H-Index II](/problems/h-index-ii) | Medium | Binary Search |
| 278 | [First Bad Version](/problems/first-bad-version) | Easy | Binary Search |
| 279 | [Perfect Squares](/problems/perfect-squares) | Medium | Math, Dynamic Programming, Breadth-first Search |
| 283 | [Move Zeroes](/problems/move-zeroes) | Easy | Array, Two Pointers |
| 287 | [Find the Duplicate Number](/problems/find-the-duplicate-number) | Medium | Array, Two Pointers, Binary Search |
| 303 | [Range Sum Query - Immutable](/problems/range-sum-query-immutable) | Easy | Dynamic Programming |
| 328 | [Odd Even Linked List](/problems/odd-even-linked-list) | Medium | Linked List |
| 332 | [Reconstruct Itinerary](/problems/reconstruct-itinerary) | Medium | Depth-first Search, Graph |
| 338 | [Counting Bits](/problems/counting-bits) | Medium | Dynamic Programming, Bit Manipulation |
| 344 | [Reverse String](/problems/reverse-string) | Easy | Two Pointers, String |
| 349 | [Intersection of Two Arrays](/problems/intersection-of-two-arrays) | Easy | Hash table, two pointers, binary search, sort, set |
| 350 | [Intersection of Two Arrays II](/problems/intersection-of-two-arrays-ii) | Easy | Hash Table, two pointers, binary search, sort |
| 367 | [Valid Perfect Square](/problems/valid-perfect-square) | Easy | Math, Binary Search |
| 368 | [Largest Divisible Subset](/problems/largest-divisible-subset) | Medium | Math, Dynamic Programming |
| 380 | [Insert Delete GetRandom O(1)](/problems/insert-delete-getrandom-o1) | Medium | Array, Hash Table, Design |
| 383 | [Ransom Note](/problems/ransom-note) | Easy | String |
| 387 | [First Unique Character in a String](/problems/first-unique-character-in-a-string) | Easy | Hash Table, String |
| 392 | [Is Subsequence](/problems/is-subsequence) | Easy | Binary Search, Dynamic Programming, Greedy |
| 402 | [Remove K Digits](/problems/remove-k-digits) | Medium | Stack, Greedy |
| 406 | [Queue Reconstruction by Height](/problems/queue-reconstruction-by-height) | Medium | Greedy |
| 438 | [Find All Anagrams in a String](/problems/find-all-anagrams-in-a-string) | Medium | Hash Table |
| 451 | [Sort Characters By Frequency](/problems/sort-characters-by-frequency) | Medium | Hash Table, Heap |
| 468 | [Validate IP Address](/problems/validate-ip-address) | Medium | String |
| 476 | [Number Complement](/problems/number-complement) | Easy | Bit Manipulation |
| 518 | [Coin Change 2](/problems/coin-change-2) | Medium | - |
| 525 | [Contiguous Array](/problems/contiguous-array) | Medium | Hash Table |
| 528 | [Random Pick with Weight](/problems/random-pick-with-weight) | Medium | Binary Search, Random |
| 540 | [Single Element in a Sorted Array](/problems/single-element-in-a-sorted-array) | Medium | - |
| 543 | [Diameter of Binary Tree](/problems/diameter-of-binary-tree) | Easy | Tree |
| 560 | [Subarray Sum Equals K](/problems/subarray-sum-equals-k) | Medium | Array, Hash Table |
| 567 | [Permutation in String](/problems/permutation-in-string) | Medium | Two Pointers, Sliding Window |
| 678 | [Valid Parenthesis String](/problems/valid-parenthesis-string) | Medium | String |
| 700 | [Search in a Binary Search Tree](/problems/search-in-a-binary-search-tree) | Easy | Tree |
| 714 | [Dungeon Game](/problems/dungeon-game) | Hard | Binary Search, Dynamic Programming |
| 733 | [Flood Fill](/problems/flood-fill) | Easy | Depth-first Search |
| 746 | [Min Cost Climbing Stairs](/problems/min-cost-climbing-stairs) | Easy | Array, Dynamic Programming |
| 771 | [Jewels and Stones](/problems/jewels-and-stones) | Easy | Hash Table |
| 787 | [Cheapest Flights Within K Stops](/problems/cheapest-flights-within-k-stops) | Medium | Dynamic Programming, Heap, Breadth-first Search |
| 844 | [Backspace String Compare](/problems/backspace-string-compare) | Easy | Two Pointers, Stack |
| 876 | [Middle of the Linked List](/problems/middle-of-the-linked-list) | Easy | Linked List |
| 886 | [Possible Bipartition](/problems/possible-bipartition) | Medium | Depth-first Search |
| 901 | [Online Stock Span](/problems/online-stock-span) | Medium | Stack |
| 918 | [Maximum Sum Circular Subarray](/problems/maximum-sum-circular-subarray) | Medium | Array |
| 973 | [K Closest Points to Origin](/problems/k-closest-points-to-origin) | Medium | Divide and Conquer, Heap, Sort |
| 986 | [Interval List Intersections](/problems/interval-list-intersections) | Medium | Two Pointers |
| 993 | [Cousins in Binary Tree](/problems/cousins-in-binary-tree) | Easy | Tree, Breadth-first Search |
| 997 | [Find the Town Judge](/problems/find-the-town-judge) | Easy | Graph |
| 1008 | [Construct Binary Search Tree from Preorder Traversal](/problems/construct-binary-search-tree-from-preorder-traversal) | Medium | Tree |
| 1025 | [Divisor Game](/problems/divisor-game) | Easy | Math, Dynamic Programming |
| 1029 | [Two City Scheduling](/problems/two-city-scheduling) | Easy | Greedy |
| 1035 | [Uncrossed Lines](/problems/uncrossed-lines) | Medium | Array |
| 1044 | [Longest Duplicate Substring](/problems/longest-duplicate-substring) | Hard | Hash Table, Binary Search |
| 1046 | [Last Stone Weight](/problems/last-stone-weight) | Easy | Heap, Greedy |
| 1143 | [Longest Common Subsequence](/problems/longest-common-subsequence) | Medium | Dynamic Programming |
| 1144 | [Decrease Elements To Make Array Zigzag](/problems/decrease-elements-to-make-array-zigzag) | Medium | Array |
| 1145 | [Binary Tree Coloring Game](/problems/binary-tree-coloring-game/) | Medium | Tree, depth-first search |
| 1150 | [Check If a Number Is Majority Element in a Sorted Array](/problems/is-a-a-majority-element) | Easy | Array, binary search |
| 1153 | [String Transforms Into Another String](/problems/string-transforms-into-another-string) | Hard | Graph |
| 1154 | [Day of the Year](problems/day-of-the-year) | Easy | Math |
| 1160 | [Find Words That Can Be Formed by Characters](/problems/find-words-that-can-be-formed-by-characters) | Easy | Array, hash table |
| 1161 | [Maximum Level Sum of a Binary Tree](/problems/maximum-level-sum-of-a-binary-tree) | Medium | Graph |
| 1162 | [As Far from Land as Possible](/problems/as-far-from-land-as-possible) | Medium | Breadth-first search, graph |
| 1163 | [Last Substring in Lexicographical Order](/problems/last-substring-in-lexicographical-order) | Hard | String, suffix array |
| 1165 | [Single-Row Keyboard](/problems/single-row-keyboard) | Easy | String |
| 1167 | [Minimum Cost to Connect Sticks](/problems/minimum-cost-to-connect-sticks) | Medium | Greedy |
| 1170 | [Compare Strings by Frequency of the Smallest Character](/problems/compare-strings-by-frequency-of-the-smallest-character) | Easy | Array, string |
| 1177 | [Can Make Palindrome from Substring](/problems/can-make-palindrome-from-substring) | Medium | Array, string |
| 1184 | [Distance Between Bus Stops](/problems/distance-between-bus-stops) | Easy | Array |
| 1185 | [Day of the Week](/problems/day-of-the-week) | Easy | Array |
| 1207 | [Unique Number of Occurrences](/problems/unique-number-of-occurrences) | Easy | Hash table |
| 1208 | [Get Equal Substrings Within Budget](/problems/get-equal-substrings-within-budget) | Medium | Array, sliding window |
| 1213 | [Intersection of Three Sorted Arrays](/problems/intersection-of-three-sorted-arrays) | Easy | Hash table, two pointers |
| 1214 | [Two Sum BSTs](/problems/two-sum-bsts) | Medium | Binary search tree |
| 1217 | [Play with Chips](/problems/play-with-chips) | Easy | Array, math, greedy |
| 1232 | [Check If It Is a Straight Line](/problems/check-if-it-is-a-straight-line) | Easy | Array, Math, Geometry |
| 1243 | [Array Transformation](/problems/array-transformation) | Easy | Array |
| 1244 | [Design A Leaderboard](/problems/design-a-leaderboard) | Medium | Hash table, sort, design |
| 1247 | [Minimum Swaps to Make Strings Equal](/problems/minimum-swaps-to-make-strings-equal) | Medium | String, greedy |
| 1256 | [Encode Number](/problems/encode-number) | Medium | Math, bit manipulation |
| 1260 | [Shift 2D Grid](/problems/shift-2d-grid) | Easy | Array |
| 1261 | [Find Elements in a Contaminated Binary Tree](/problems/find-elements-in-a-contaminated-binary-tree) | Medium | Hash table, tree |
| 1271 | [Hexspeak](/problems/hexspeak) | Easy | Math, string |
| 1272 | [Remove Interval](/problems/remove-interval) | Medium | Math, line sweep |
| 1275 | [Find Winner on a Tic Tac Toe Game](/problems/find-winner-on-a-tic-tac-toe-game) | Easy | Array |
| 1276 | [Number of Burgers with No Waste of Ingredients](/problems/number-of-burgers-with-no-waste-of-ingredients) | Medium | Math, greedy |
| 1277 | [Count Square Submatrices with All Ones](/problems/count-square-submatrices-with-all-ones) | Medium | Array, Dynamic Programming |
| 1313 | [Decompress Run-Length Encoded List](/problems/decompress-run-length-encoded-list) | Easy | Array |
| 1314 | [Matrix Block Sum](/problems/matrix-block-sum) | Medium | Dynamic programming |
| 1317 | [Convert Integer to the Sum of Two No-Zero Integers](/problems/convert-integer-to-the-sum-of-two-no-zero-integers) | Easy | Math |
| 1342 | [Number of Steps to Reduce a Number to Zero](/problems/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](/problems/number-of-sub-arrays-of-size-k-and-average-greater-than-or-equal-to-threshold) | Medium | Array |
| 1344 | [Angle Between Hands of a Clock](/problems/angle-between-hands-of-a-clock) | Medium | Math |
| 1346 | [Check If N and Its Double Exist](/problems/check-if-n-and-its-double-exist) | Easy | Array |
| 1347 | [Minimum Number of Steps to Make Two Strings Anagram](/problems/minimum-number-of-steps-to-make-two-strings-anagram) | Medium | String |
| 1348 | [Tweet Counts Per Frequency](/problems/tweet-counts-per-frequency) | Medium | Design |
| 1356 | [Sort Integers by The Number of 1 Bits](/problems/sort-integers-by-the-number-of-1-bits) | Easy | Sort, bit manipulation |
| 1357 | [Apply Discount Every n Orders](/problems/apply-discount-every-n-orders) | Medium | Design |
| 1358 | [Number of Substrings Containing All Three Characters](/problems/number-of-substrings-containing-all-three-characters) | Medium | String |
| 1360 | [Number of Days Between Two Dates](/problems/number-of-days-between-two-dates) | Easy | - |
| 1361 | [Validate Binary Tree Nodes](/problems/validate-binary-tree-nodes) | Medium | Graph |
| 1362 | [Closest Divisors](/problems/closest-divisors) | Medium | Math |
| 1370 | [Increasing Decreasing String](/problems/increasing-decreasing-string) | Easy | String, sort |
| 1374 | [Generate a String With Characters That Have Odd Counts](/problems/generate-a-string-with-characters-that-have-odd-counts) | Easy | String |
| 1385 | [Find the Distance Value Between Two Arrays](/problems/find-the-distance-value-between-two-arrays) | Easy | Array |
| 1386 | [Cinema Seat Allocation](/problems/cinema-seat-allocation) | Medium | Array, Greedy |
| 1387 | [Sort Integers by The Power Value](/problems/sort-integers-by-the-power-value) | Medium | Sort, Graph |
| 1389 | [Create Target Array in the Given Order](/problems/create-target-array-in-the-given-order) | Easy | Array |
| 1390 | [Four Divisors](/problems/four-divisors) | Medium | Math |
| 1401 | [Circle and Rectangle Overlapping](/problems/circle-and-rectangle-overlapping) | Medium | Geometry |
| 1404 | [Number of Steps to Reduce a Number in Binary Representation to One](/problems/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](/problems/minimum-value-to-get-positive-step-by-step-sum) | Easy | Array |
| 1417 | [Reformat The String](/problems/reformat-the-string) | Easy | String |
| 1418 | [Display Table of Food Orders in a Restaurant](/problems/display-table-of-food-orders-in-a-restaurant) | Medium | Hash Table |
| 1419 | [Minimum Number of Frogs Croaking](/problems/minimum-number-of-frogs-croaking) | Medium | String |
| 1426 | [Counting Elements](/problem/counting-elements) | - | - |
| 1427 | [Perform String Shifts](/problems/perform-string-shifts) | - | - |
| 1428 | [Leftmost Column with at Least a One](/problems/leftmost-column-with-at-least-a-one) | - | - |
| 1429 | [First Unique Number](/problems/first-unique-number) | - | - |
| 1430 | [Check If a String Is a Valid Sequence from Root to Leaves Path in a Binary Tree](/problems/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](/problems/final-prices-with-a-special-discount-in-a-shop) | Easy | Array |
| 1476 | [Subrectangle Queries](/problems/subrectangle-queries) | Medium | Array |
| 1480 | [Running Sum of 1d Array](/problems/running-sum-of-1d-array) | Easy | Array |
| 1481 | [Least Number of Unique Integers after K Removals](/problems/least-number-of-unique-integers-after-k-removals) | Medium | Array, Sort |
| 1491 | [Average Salary Excluding the Minimum and Maximum Salary](/problems/average-salary-excluding-the-minimum-and-maximum-salary) | Easy | Array, Sort |
| 1492 | [The kth Factor of n](/problems/the-kth-factor-of-n) | Medium | Math |
| 1493 | [Longest Subarray of 1's After Deleting One Element](/problems/longest-subarray-of-1s-after-deleting-one-element) | Medium | Array |
| 1572 | [Matrix Diagonal Sum](/problems/matrix-diagonal-sum) | Easy | Array |
| 1574 | [Shortest Subarray to be Removed to Make Array Sorted](/problems/shortest-subarray-to-be-removed-to-make-array-sorted) | Medium | Array, Binary Search |
| 1576 | [Replace All ?'s to Avoid Consecutive Repeating Characters](/problems/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](/problems/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](/problems/minimum-deletion-cost-to-avoid-repeating-letters) | Medium | Greedy |
| 1603 | [Design Parking System](/problems/design-parking-system) | Easy | Design |
| 1604 | [Alert Using Same Key-Card Three or More Times in a One Hour Period](/problems/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](/problems/special-array-with-x-elements-greater-than-or-equal-x) | Easy | Array |
| 1609 | [Even Odd Tree](/problems/even-odd-tree) | Medium | Tree |

## Questions / Issues

If you have any question, feel free to open a GitHub issue and reach out to me.