Skip to content

Repository files navigation

leetcode

Build StatuscodecovJavaScript Style Guide

My solutions for LeetCode problems.

Usage

Running and testing locally in your machine

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

Submitting / Running in LeetCode

  1. Find your problem in the 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 page, or my code commits.

LeetCode #TitleDifficultyTopics
1Two SumEasyArray, hash table
33Search in Rotated Sorted ArrayMediumArray, Binary Search
35Search Insert PositionEasyArray, binary search
49Group AnagramsMediumHash Table, String
53Maximum SubarrayEasyArray, Divide and Conquer, Dynamic Programming
55Jump GameMediumArray, Greedy
60Permutation SequenceMediumMath, Backtracking
62Unique PathsMediumArray, Dynamic Programming
64Minimum Path SumMediumArray, Dynamic Programming
70Climbing StairsEasyDynamic Programming
72Edit DistanceHardString, Dynamic Programming
75Sort ColorsMediumArray, Two Pointers, Sort
96Unique Binary Search TreesMediumDynamic Programming, Tree
121Best Time to Buy and Sell StockEasyArray, Dynamic Programming
122Best Time to Buy and Sell Stock IIEasyArray, Greedy
124Binary Tree Maximum Path SumHardTree, Depth-first Search
129Sum Root to Leaf NumbersMediumTree, Depth-first Search
130Surrounded RegionsMediumDepth-first Search, Breadth-first Search, Union Find
136Single NumberEasyHash table, bit manipulation
137Single Number IIMediumBit Manipulation
146LRU CacheMediumDesign
155Min StackEasyStack, Design
169Majority ElementEasyArray, Divide and Conquer, Bit Manipulation
189Rotate ArrayEasyArray
198House RobberEasyDynamic Programming
200Number of IslandsMediumDepth-first Search, Breadth-first Search, Union Find
201Bitwise AND of Numbers RangeMediumBit Manipulation
202Happy NumberEasyHash Table, Math
207Course ScheduleMediumDepth-first Search, Breadth-first Search, Graph, Topological Sort
208Implement Trie (Prefix Tree)MediumDesign, Trie
212Word Search IIHardBacktracking, Trie
221Maximal SquareMediumDynamic Programming
222Count Complete Tree NodesMediumBinary Search, Tree
226Invert Binary TreeEasyTree
230Kth Smallest Element in a BSTMediumBinary Search, Tree
231Power of TwoEasyMath, Bit Manipulation
237Delete Node in a Linked ListEasyLinked List
238Product of Array Except SelfMediumArray
275H-Index IIMediumBinary Search
278First Bad VersionEasyBinary Search
279Perfect SquaresMediumMath, Dynamic Programming, Breadth-first Search
283Move ZeroesEasyArray, Two Pointers
287Find the Duplicate NumberMediumArray, Two Pointers, Binary Search
303Range Sum Query - ImmutableEasyDynamic Programming
328Odd Even Linked ListMediumLinked List
332Reconstruct ItineraryMediumDepth-first Search, Graph
338Counting BitsMediumDynamic Programming, Bit Manipulation
344Reverse StringEasyTwo Pointers, String
349Intersection of Two ArraysEasyHash table, two pointers, binary search, sort, set
350Intersection of Two Arrays IIEasyHash Table, two pointers, binary search, sort
367Valid Perfect SquareEasyMath, Binary Search
368Largest Divisible SubsetMediumMath, Dynamic Programming
380Insert Delete GetRandom O(1)MediumArray, Hash Table, Design
383Ransom NoteEasyString
387First Unique Character in a StringEasyHash Table, String
392Is SubsequenceEasyBinary Search, Dynamic Programming, Greedy
402Remove K DigitsMediumStack, Greedy
406Queue Reconstruction by HeightMediumGreedy
438Find All Anagrams in a StringMediumHash Table
451Sort Characters By FrequencyMediumHash Table, Heap
468Validate IP AddressMediumString
476Number ComplementEasyBit Manipulation
518Coin Change 2Medium-
525Contiguous ArrayMediumHash Table
528Random Pick with WeightMediumBinary Search, Random
540Single Element in a Sorted ArrayMedium-
543Diameter of Binary TreeEasyTree
560Subarray Sum Equals KMediumArray, Hash Table
567Permutation in StringMediumTwo Pointers, Sliding Window
678Valid Parenthesis StringMediumString
700Search in a Binary Search TreeEasyTree
714Dungeon GameHardBinary Search, Dynamic Programming
733Flood FillEasyDepth-first Search
746Min Cost Climbing StairsEasyArray, Dynamic Programming
771Jewels and StonesEasyHash Table
787Cheapest Flights Within K StopsMediumDynamic Programming, Heap, Breadth-first Search
844Backspace String CompareEasyTwo Pointers, Stack
876Middle of the Linked ListEasyLinked List
886Possible BipartitionMediumDepth-first Search
901Online Stock SpanMediumStack
918Maximum Sum Circular SubarrayMediumArray
973K Closest Points to OriginMediumDivide and Conquer, Heap, Sort
986Interval List IntersectionsMediumTwo Pointers
993Cousins in Binary TreeEasyTree, Breadth-first Search
997Find the Town JudgeEasyGraph
1008Construct Binary Search Tree from Preorder TraversalMediumTree
1025Divisor GameEasyMath, Dynamic Programming
1029Two City SchedulingEasyGreedy
1035Uncrossed LinesMediumArray
1044Longest Duplicate SubstringHardHash Table, Binary Search
1046Last Stone WeightEasyHeap, Greedy
1143Longest Common SubsequenceMediumDynamic Programming
1144Decrease Elements To Make Array ZigzagMediumArray
1145Binary Tree Coloring GameMediumTree, depth-first search
1150Check If a Number Is Majority Element in a Sorted ArrayEasyArray, binary search
1153String Transforms Into Another StringHardGraph
1154Day of the YearEasyMath
1160Find Words That Can Be Formed by CharactersEasyArray, hash table
1161Maximum Level Sum of a Binary TreeMediumGraph
1162As Far from Land as PossibleMediumBreadth-first search, graph
1163Last Substring in Lexicographical OrderHardString, suffix array
1165Single-Row KeyboardEasyString
1167Minimum Cost to Connect SticksMediumGreedy
1170Compare Strings by Frequency of the Smallest CharacterEasyArray, string
1177Can Make Palindrome from SubstringMediumArray, string
1184Distance Between Bus StopsEasyArray
1185Day of the WeekEasyArray
1207Unique Number of OccurrencesEasyHash table
1208Get Equal Substrings Within BudgetMediumArray, sliding window
1213Intersection of Three Sorted ArraysEasyHash table, two pointers
1214Two Sum BSTsMediumBinary search tree
1217Play with ChipsEasyArray, math, greedy
1232Check If It Is a Straight LineEasyArray, Math, Geometry
1243Array TransformationEasyArray
1244Design A LeaderboardMediumHash table, sort, design
1247Minimum Swaps to Make Strings EqualMediumString, greedy
1256Encode NumberMediumMath, bit manipulation
1260Shift 2D GridEasyArray
1261Find Elements in a Contaminated Binary TreeMediumHash table, tree
1271HexspeakEasyMath, string
1272Remove IntervalMediumMath, line sweep
1275Find Winner on a Tic Tac Toe GameEasyArray
1276Number of Burgers with No Waste of IngredientsMediumMath, greedy
1277Count Square Submatrices with All OnesMediumArray, Dynamic Programming
1313Decompress Run-Length Encoded ListEasyArray
1314Matrix Block SumMediumDynamic programming
1317Convert Integer to the Sum of Two No-Zero IntegersEasyMath
1342Number of Steps to Reduce a Number to ZeroEasyBit manipulation
1343Number of Sub-arrays of Size K and Average Greater than or Equal to ThresholdMediumArray
1344Angle Between Hands of a ClockMediumMath
1346Check If N and Its Double ExistEasyArray
1347Minimum Number of Steps to Make Two Strings AnagramMediumString
1348Tweet Counts Per FrequencyMediumDesign
1356Sort Integers by The Number of 1 BitsEasySort, bit manipulation
1357Apply Discount Every n OrdersMediumDesign
1358Number of Substrings Containing All Three CharactersMediumString
1360Number of Days Between Two DatesEasy-
1361Validate Binary Tree NodesMediumGraph
1362Closest DivisorsMediumMath
1370Increasing Decreasing StringEasyString, sort
1374Generate a String With Characters That Have Odd CountsEasyString
1385Find the Distance Value Between Two ArraysEasyArray
1386Cinema Seat AllocationMediumArray, Greedy
1387Sort Integers by The Power ValueMediumSort, Graph
1389Create Target Array in the Given OrderEasyArray
1390Four DivisorsMediumMath
1401Circle and Rectangle OverlappingMediumGeometry
1404Number of Steps to Reduce a Number in Binary Representation to OneMediumString, Bit Manipulation
1413Minimum Value to Get Positive Step by Step SumEasyArray
1417Reformat The StringEasyString
1418Display Table of Food Orders in a RestaurantMediumHash Table
1419Minimum Number of Frogs CroakingMediumString
1426Counting Elements--
1427Perform String Shifts--
1428Leftmost Column with at Least a One--
1429First Unique Number--
1430Check If a String Is a Valid Sequence from Root to Leaves Path in a Binary Tree--
1475Final Prices With a Special Discount in a ShopEasyArray
1476Subrectangle QueriesMediumArray
1480Running Sum of 1d ArrayEasyArray
1481Least Number of Unique Integers after K RemovalsMediumArray, Sort
1491Average Salary Excluding the Minimum and Maximum SalaryEasyArray, Sort
1492The kth Factor of nMediumMath
1493Longest Subarray of 1's After Deleting One ElementMediumArray
1572Matrix Diagonal SumEasyArray
1574Shortest Subarray to be Removed to Make Array SortedMediumArray, Binary Search
1576Replace All ?'s to Avoid Consecutive Repeating CharactersEasyString
1577Number of Ways Where Square of Number Is Equal to Product of Two NumbersMediumHash Table, Math
1578Minimum Deletion Cost to Avoid Repeating LettersMediumGreedy
1603Design Parking SystemEasyDesign
1604Alert Using Same Key-Card Three or More Times in a One Hour PeriodMediumString, Ordered Map
1608Special Array With X Elements Greater Than or Equal XEasyArray
1609Even Odd TreeMediumTree

Questions / Issues

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

About

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

Topics

Resources

Stars

16 stars

Watchers

2 watching

Forks

Releases

Packages

Used by

Contributors

Languages