Javascript Problems & Solutions
A collection of JavaScript problems and solutions for studying algorithms.
Table of Content
- Data Structures
- Array
- Matrix
- String
- Sorting
- Heap
- Binary Search
- Binary Indexed Tree
- Tree
- Graph
- Trie (Prefix Tree)
- Backtracking
- Breadth First Search
- Depth First Search
- Divide and Conquer
- Linked List
- Dynamic Programming
- Greedy
- Math
- Sampling
- Design
Data Structures
Problems
Array
- Two Sum
- Three Sum
- Median of Two Sorted Arrays
- Largest Rectangle in Histogram
- Plus One
- Trapping Rain Water
- Merge Intervals
- Spiral Matrix
- Summary Ranges
- Find All Numbers Disappeared in an Array
- Game of Life
- Next Permutation
- Find Peak Element
- Wiggle Sort
- Wiggle Sort II
- Valid Triangle Number
- Find Anagram Mappings
- K Empty Slots
- Flatten Nested List Iterator
- Daily Temperatures
- Sliding Window Maximum
- Subarray Sum Equals K
- Subarray Product Less Than K
- Maximum Product of Three Numbers
- Maximum Product of Word Lengths
- Kth Largest Element in an Array
- Insert Interval
- Toeplitz Matrix
- Max Consecutive Ones
- Max Consecutive Ones II
- Flippling an Image
- Number of Boomerangs
- Beautiful Arrangement II
- Non-decreasing Array
- Largest Number At Least Twice of Others
- Maximize Distance to Closest Person
- Positions of Large Groups
- Maximum Average Subarray I
- Shortest Unsorted Continuous Subarray
- Relative Ranks
- Sentence Similarity
- Sentence Similarity II
- Magic Squares In Grid
- Range Addition
- Increasing Triplet Subsequence
- Valid Tic-Tac-Toe State
- Maximum Size Subarray Sum Equals k
- Longest Mountain in Array
- Continuous Subarray Sum
- Contiguous Array
- Merge Sorted Array
- Product of Array Except Self
- Missing Number
- Maximum Sum Subarray
- Maximum Product Subarray
- Evaluate Reverse Polish Notation
- Shortest Word Distance
- Shortest Word Distance II
- Shortest Word Distance III
- Sort Colors
- Find the Celebrity
- Can Place Flowers
- K-diff Pairs in an Array
- Container With Most Water
- First Missing Positive
- Missing Ranges
- Majority Elements
- Find All Duplicates in an Array
- Third Maximum Number
- Split Array with Equal Sum
- Maximum Swap
- Candy Crush
- Pascal's Triangle
- Pascal's Triangle II
- Contains Duplicate II
- Contains Duplicate III
- H-Index
- H-Index II
- Pour Water
- Sort a stack using a temporary stack
Matrix
- Rotate Matrix
- Island Perimeter
- Search a 2D Matrix II
- Diagonal Traverse
- Number Of Corner Rectangles
- Lonely Pixel I
- Lonely Pixel II
- Sparse Matrix Multiplication
- Escape The Ghosts
- Brick Wall
String
- Implement strStr()
- Regular Expression Matching
- Letter Combinations of a Phone Number
- Valid Parentheses
- Generate Parentheses
- Generate Parentheses II
- Shortest Palindrome
- Palindrome Pairs
- First Unique Character in a String
- Judge Route Circle
- Reverse Vowels of a String
- Add Bold Tag in String
- Minimum Window Substring
- Longest Absolute File Path
- Basic Calculator
- Buddy Strings
- Find the Difference
- Bulls and Cows
- Add Strings
- Reverse String II
- Sort Characters By Frequency
- Binary Watch
- Magical String
- Rotated Digits
- Text Justification
- Bold Words in String
- Valid Word Abbreviation
- Backspace String Compare
- Repeated String Match
- Repeated DNA Sequences
- Longest Palindrome
- Valid Word Square
- Detect Capital
- Repeated Substring Pattern
- License Key Formatting
- Similar RGB Color
- Valid Palindrome II
- Output Contest Matches
- Shortest Completing Word
- Count and Say
- Multiply Strings
- Add Binary
- Minimum Window Substring
- Longest Substring with At Most Two Distinct Characters
- Longest Substring Without Repeating Characters
- Substring with Concatenation of All Words
- Group Anagrams
- Palindromic Substrings
- Integer to English Words
- Robot Return to Origin
- Valid Number
- Isomorphic Strings
- Find All Anagrams in a String
- Longest Common Prefix
- Compare Version Numbers
- Display Pages
- CSV Parser
- One Edit Distance
Sorting
Heap
- Find Median from Data Stream
- Kth Smallest Element in a Sorted Matrix
- Kth Largest Element in a Stream
- Employee Free Time
Binary Search
- Binary Search
- Search Insert Position
- Search in Rotated Sorted Array
- Search in Rotated Sorted Array II
- Find First and Last Position of Element in Sorted Array
- Divide Two Integers
- Power(x, n)
- Sqrt(x)
- Valid Perfect Square
- Heaters
- Find K Closest Elements
- Sum of Square Numbers
Binary Indexed Tree
Tree
- Add One Row to Tree
- Balanced Binary Tree
- Binary Search Tree I - Search
- Binary Search Tree II - Insert
- Binary Search Tree II - Delete
- Binary Search Tree Iterator
- Binary Tree Inorder Traversal
- Binary Tree Level Order Traversal
- Binary Tree Longest Consecutive Sequence
- Binary Tree Longest Consecutive Sequence II
- Binary Tree Maximum Path Sum
- Binary Tree Paths
- Binary Tree Postorder Traversal
- Binary Tree Preorder Traversal
- Binary Tree Upside Down
- Binary Tree Zigzag Level Order Traversal
- Closest Binary Search Tree Value
- Closest Binary Search Tree Value II
- Construct Binary Tree From Preorder and Inorder Traversal
- Construct Binary Tree From Postorder and Inorder Traversal
- Convert BST to Greater Tree
- Convert Sorted Array to Binary Search Tree
- Count Univalue Subtrees
- Count of Smaller Numbers After Self
- Delete Node in a BST
- Diameter of Binary Tree
- Find Bottom Left Tree Value
- Find Largest Value in Each Tree Row
- Find Leaves of Binary Tree
- Find Mode in Binary Search Tree
- Flatten Binary Tree to Linked List
- House Robber III
- Inorder Predecessor in BST
- Inorder Successor in BST
- K-th ancestor of a node in Binary Tree
- Kth Smallest Element in a BST
- Kill Process
- Longest Univalue Path
- Lowest Common Ancestor of A Binary Search Tree
- Lowest Common Ancestor of a Binary Tree
- Maximum Binary Tree
- Maximum Depth of Binary Tree
- Maximum Width of Binary Tree
- Minimum Depth of Binary Tree
- Minimum Distance Between BST Nodes
- Most Frequent Subtree Sum
- Path Sum
- Path Sum II
- Path Sum III
- Path Sum IV
- Populating Next Right Pointers in Each Node
- Populating Next Right Pointers in Each Node II
- Recover Binary Search Tree
- Same Tree
- Second Minimum Node In a Binary Tree
- Serialize and Deserialize BST
- Serialize and Deserialize Binary Tree
- Subtree of Another Tree
- Sum Root to Leaf Numbers
- Symmetric Tree
- Smallest Subtree with all the Deepest Nodes
- Trim a Binary Search Tree
- Unique Binary Search Trees
- Unique Binary Search Trees II
- Univalue Tree
- Validate Binary Search Tree
- Verify Preorder Sequence in Binary Search Tree
- Verify Preorder Serialization of a Binary Tree
- Serialize and Deserialize N-ary Tree
Graph
- Clone Graph
- Course Schedule
- Course Schedule II
- Alien Dictionary
- Alien Dictionary II
- Graph Valid Tree
- Number of Connected Components in an Undirected Graph
- Reconstruct Itinerary
- Evaluate Division
- Sequence Reconstruction
- Minimum Height Trees
- Redundant Connection
- Redundant Connection II
- Is Graph Bipartite?
- Minimum Vertices to Traverse Directed Graph
Trie (Prefix Tree)
- Implement Trie (Prefix Tree)
- Add and Search Word
- Word Search II
- Longest Word in Dictionary
- Map Sum Pairs
- Implement Magic Dictionary
- Replace Words
- Top K Frequent Words
- Concatenated Words
- Palindrome Pairs
- Design Search Autocomplete System
- Word Squares
- Maximum XOR of Two Numbers in an Array
- Add Bold Tag in String
- K Edit Distance
Backtracking
- Subsets
- Subsets II
- Combinations
- Combination Sum
- Combination Sum II
- Combination Sum III
- Combination Sum IV
- Permutations
- Permutations II
- N-Queens
- N-Queens II
- Generate Parentheses
- Generate Parentheses II
- Word Search
- Word Search II (Boggle Game)
- Sudoku Solver
- Regular Expression Matching
- Wildcard Matching
- Letter Combinations of a Phone Number
- Letter Case Permutation
- Count Numbers with Unique Digits
- Restore IP Addresses
- Expression Add Operators
- Keys and Rooms
- Beautiful Arrangement
- Generalized Abbreviation
- Partition to K Equal Sum Subsets
- Additive Number
- Gray Code
- Find Case Combination of a String
Breadth First Search
- Shortest Path in Maze
- Shortest Path in Maze II
- Number of Islands
- Pacific Atlantic Water Flow
- 01 Matrix
- Minesweeper
- Word Ladder
- Word Ladder II
- The Maze
- The Maze II
- Surrounded Regions
- Trapping Rain Water II
Depth First Search
- Number of Islands
- Max Area of Island
- Next Closest Time
- Decode String
- Pacific Atlantic Water Flow
- Minesweeper
- The Maze
- The Maze II
- Pyramid Transition Matrix
- Nested List Weight Sum
- Nested List Weight Sum II
Divide and Conquer
Linked List
- Reverse Linked List
- Add Two Numbers
- Merge Two Sorted Lists
- Merge K Sorted Lists
- Plus One Linked List
- Linked List Components
- Remove Nth Node From End of List
- Swap Nodes in Pairs
- Reverse Nodes in k-Group
- Rotate List
- Remove Duplicates from Sorted List
- Remove Duplicates from Sorted List II
- Partition List
- Convert Sorted List to Binary Search Tree
- Copy List with Random Pointer
- Linked List Cycle
- Linked List Cycle II
- Reorder List
- Insertion Sort List
- Intersection of Two Linked Lists
- Remove Linked List Elements
- Palindrome Linked List
- Odd Even Linked List
- Split Linked List in Parts
- Convert Binary Search Tree to Sorted Doubly Linked List
- Add Two Numbers II
- Insert into a Cyclic Sorted List
Dynamic Programming
- Longest Continuous Increasing Subsequence
- Longest Increasing Subsequence
- Longest Palindromic Subsequence
- Longest Palindromic Substring
- Longest Consecutive Sequence
- Count Palindromic Substrings
- Regular Expression Matching
- Regular Expression Matching II
- Wildcard Matching
- Word Break
- Word Break II
- Best Time to Buy and Sell Stock
- Best Time to Buy and Sell Stock II
- Best Time to Buy and Sell Stock III
- Best Time to Buy and Sell Stock IV
- Best Time to Buy and Sell Stock with Cooldown
- Best Time to Buy and Sell Stock with Transaction Fee
- Gas Station III
- Perfect Squares
- Subset Sum Problem
- Matrix Chain Multiplication
- Burst Balloons
- Guess Number Higher or Lower II
- 01 Matrix
- 4 Keys Keyboard
- Coin Change
- Coin Change II
- Target Sum
- Longest Line of Consecutive One in Matrix
- Delete Operation for Two Strings
- Largest Sum of Averages
- Maximum Sum of 3 Non-Overlapping Subarrays
- Maximal Rectangle
- Min Cost Climbing Stairs
- House Robber
- House Robber II
- Paint Fence
- Paint House
- Paint House II
- Partition Equal Subset Sum
- Partition to K Equal Sum Subsets
- Can I Win
- Unique Paths
- Decode Ways
- Max Wages
- Dungeon Game
- Triangle
- Maximal Square
- Ugly Number II
Greedy
- Candy
- Gas Station
- Gas Station II
- Queue Reconstruction by Height
- K Workers
- Remove Duplicate Letters
- Meeting Rooms II
- Task Scheduler
- Find Permutation
- Jump Game
- Minimum Number of Arrows to Burst Balloons
- Non-overlapping Intervals
Math
- Count Primes
- Power of Two
- Power of Three
- Power of Four
- Fraction to Recurring Decimal
- Largest Triangle Area
- Hamming Distance
- Total Hamming Distance
- Nth Digit
- Roman to Integer
- Excel Sheet Column Title
- Sum of Square Numbers
- Reverse Integer
- Single Number II
- Factorial Trailing Zeroes
- Strobogrammatic Number
- Strobogrammatic Number II
- Collatz Conjecture
- Number of Intersected Rectangles
Sampling
- Reservoir Sampling
- Select Random Number From Stream
- Linked List Random Node
- Random Pick Index
- Random Pick with Weight
Design
- Encode and Decode TinyURL
- LRU Cache
- LFU Cache
- Min Stack
- Max Stack
- Max Stack II
- Exam Room
- Insert Delete GetRandom O(1)
- Moving Average from Data Stream
- Flatten 2D Vector
- Logger Rate Limiter
- Design Hit Counter
- Design Compressed String Iterator
- Design Tic-Tac-Toe
- My Calendar I
- My Calendar II
- My Calendar III
- Design Phone Directory
- Shortest Word Distance II
- Shuffle an Array
- Read N Characters Given Read4
- Read N Characters Given Read4 II - Call multiple times
- File System
- Implement a Queue with Fixed-Size Array
Backers
Support us with a monthly donation and help us continue our activities. [Become a backer]
Sponsors
Become a sponsor and get your logo on our README on Github with a link to your site. [Become a sponsor]
Support
LICENSE
MIT License
Copyright (c) 2018 Yong Su
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.