leetcode icon indicating copy to clipboard operation
leetcode copied to clipboard

Leetcode solutions

Leetcode solutions in Java

My accepted leetcode solutions to some of the common interview problems.

Array

  • Pascals Traiangle II (Easy)
  • Product Of Array Except Self (Medium)
  • Rotate Matrix (Medium)
  • Set Matrix Zeroes (Medium)
  • Third Maximum Number (Easy)
  • Two Sum (Easy)
  • TwoSum II (Easy)
  • Can Place Flowers (Easy)
  • Merge Intervals (Medium)
  • #f03c15 First Missing Positive (Hard)
  • Fruit Into Baskets (Medium)
  • MaxProduct Of Three Numbers (Easy)
  • Missing Number (Easy)
  • Merge Sorted Array (Easy)
  • Rotate Array (Easy)
  • Sort Colors (Medium)
  • Battleships in a Board (Medium)
  • Find the Celebrity (Medium)
  • Meeting Rooms (Easy)
  • Longest Continuous Increasing Subsequence (Easy)
  • Sparse Matrix Multiplication (Medium)
  • Read N Characters Given Read4 (Easy)
  • Maximum Swap (Medium)
  • H-Index (Medium)
  • #f03c15 Insert Interval (Hard)
  • Increasing Triplet Subsequence (Medium)
  • #f03c15 K Empty Slots (Hard)
  • Subarray Sum Equals K (Medium)
  • Pour Water (Medium)
  • Relative Ranks (Easy)
  • Next Greater Element I (Easy)
  • Largest Number At Least Twice of Others (Easy)
  • Minimum Moves to Equal Array Elements II (Median)
  • Image Smoother (Easy)
  • Minimum Index Sum of Two Lists (Easy)
  • Card Flipping Game (Medium)
  • #f03c15 Employee Free Time (Hard)
  • #f03c15 Best Meeting Point (Hard)
  • #f03c15 My Calendar III (Hard)
  • Champagne Tower (Medium)
  • Valid Tic-Tac-Toe State (Medium)
  • Number of Subarrays with Bounded Maximum (Medium)
  • Surface Area of 3D Shapes (Easy)
  • Max Consecutive Ones (Easy)
  • Max Consecutive Ones II (Medium)
  • Add to Array-Form of Integer (Easy)
  • Find Pivot Index (Easy)
  • Largest Time for Given Digits (Easy)
  • Minimum Time Difference (Medium)
  • Reveal Cards In Increasing Order (Medium)
  • Sort Array By Parity II (Easy)
  • Matrix Cells in Distance Order (Easy)
  • Maximum Sum of Two Non-Overlapping Subarrays (Medium)
  • Longest Line of Consecutive One in Matrix (Medium)
  • Array Partition I (Easy)
  • Relative Sort Array (Easy)
  • Meeting Scheduler (Medium)
  • Minimum Swaps to Group All 1's Together (Medium)
  • Array Nesting (Medium)

Backtracking

  • Combinations (Medium)
  • Combinations Sum (Medium)
  • Combinations Sum II (Medium)
  • Letter Phone Number (Medium)
  • Paliandrome Partitioning (Medium)
  • Permutations (Medium)
  • Permutations II (Medium)
  • SubSets (Medium)
  • SubSet II (Medium)
  • Word Search (Medium)
  • #f03c15 Word Search II (Hard)
  • Generate Parentheses (Medium)
  • #f03c15 Remove Invalid Parentheses (Hard)
  • #f03c15 Regular Expression Matching (Hard)
  • #f03c15 Expression Add Operators (Hard)
  • #f03c15 Wildcard Matching (Hard)
  • Letter Case Permutation (Easy)
  • #f03c15 Zuma Game (Hard)
  • Matchsticks to Square (Medium)

Binary Search

  • Minimum Sorted Rotated Array (Medium)
  • Search in a Rotated Sorted Array (Medium)
  • Search for a Range (Medium)
  • Sqrt(x) (Easy)
  • Search Insert Position (Easy)
  • #f03c15 Median of Two Sorted Arrays (Hard)
  • Pow(x, n) (Medium)
  • Find Peak Element (Medium)
  • Target Sum (Medium)
  • H-Index II (Medium)
  • #f03c15 Swim in Rising Water (Hard)
  • Time Based Key-Value Store (Medium)
  • #f03c15 Minimum Window Subsequence (Hard)
  • #f03c15 Koko Eating Bananas (Hard)
  • Single Element in a Sorted Array (Medium)

Bit Manipulation

  • Gray Code (Medium)
  • Hamming Distance (Easy)
  • Total Hamming Distance (Medium)
  • Divide Two Integers (Medium)
  • Binary Number with Alternating Bits (Easy)
  • Binary Watch (Easy)

Breadth First Search

  • Binaray Tree Level Order Traversal (Medium)
  • Word Ladder (Medium)
  • #f03c15 Word Ladder II (Hard)
  • Walls and Gates (Medium)
  • Open the lock (Medium)
  • #f03c15 Cut Off Trees for Golf Event (Hard)
  • #f03c15 Race Car (Hard)
  • #f03c15 Bus Routes (Hard)
  • #f03c15 Sliding Puzzle (Hard)
  • Matrix (Medium)
  • Rotting Oranges (Medium)

Depth First Search

  • Minesweeper (Medium)
  • Movie Recommend (Medium)
  • Number Of Islands (Medium)
  • Course Schedule (Medium)
  • Course Schedule II (Medium)
  • #f03c15 Alien Dictionary (Hard)
  • Graph Valid Tree (Medium)
  • #f03c15 Longest Consecutive Sequence (Hard)
  • Accounts Merge (Medium)
  • CloneGraph (Medium)
  • Island Perimeter (Easy)
  • Number of Distinct Islands (Medium)
  • #f03c15 Number of Distinct Islands II (Hard)
  • #f03c15 Smallest Rectangle Enclosing Black Pixels (Hard)
  • #f03c15 Bricks Falling When Hit (Hard)
  • #f03c15 Robot Room Cleaner (Hard)
  • #f03c15 Cracking the Safe (Hard)
  • All Paths From Source to Target (Medium)
  • Max Area of Island (Medium)
  • Satisfiability of Equality Equations (Medium)
  • Number of Enclaves (Medium)
  • As Far from Land as Possible (Medium)
  • #f03c15 Minimize Malware Spread (Hard)
  • #f03c15 Parallel Courses (Hard)
  • Connecting Cities With Minimum Cost (Medium)
  • #f03c15 Critical Connections in a Network (Hard)

Design

  • Copy List With Random Pointer (Medium)
  • Encode and Decode Tiny URL (Medium)
  • #f03c15 LFU Cache (Hard)
  • #f03c15 LRU Cache (Hard)
  • Insert Delete Get Random (Medium)
  • #f03c15 Serialize Deserialize Binary Tree (Hard)
  • Design Twitter (Medium)
  • Tic-Tac-Toe (Medium)
  • Implement Trie (Prefix Tree) (Medium)
  • Binary Search Tree Iterator (Medium)
  • #f03c15 Design Search Autocomplete System (Hard)
  • #f03c15 Design Excel Sum Formula (Hard)
  • Flatten Nested List Iterator (Medium)
  • Add and Search Word - Data structure design (Medium)
  • #f03c15 Prefix and Suffix Search (Hard)
  • #f03c15 Insert Delete GetRandom O(1) - Duplicates allowed (Hard)

Divide and Conquer

  • Kth Largest Element In a Array (Medium)
  • #f03c15 Reverse Pairs (Hard)
  • Search in a 2D Matrix (Medium)
  • #f03c15 24 Game (Hard)
  • #f03c15 Reverse Pairs II (Hard)
  • My Calendar II (Medium)

Dynamic Programming

  • Best Time To Buy and Sell Stocks (Easy)
  • #f03c15 Best Time to Buy and Sell Stock III (Hard)
  • Best Time to Buy and Sell Stock with Transaction Fee (Medium)
  • Climbing Stairs (Easy)
  • Coin Change (Medium)
  • Coin Change 2 (Medium)
  • Decode Ways (Medium)
  • House Robber (Easy)
  • House Robber II (Medium)
  • Longest Increasing Subsequence (Medium)
  • Longest Paliandromic Substring (Medium)
  • Longest Palindromic Subsequence (Medium)
  • Maximum Product Subarray (Medium)
  • Min Cost Climbing Stairs (Easy)
  • #f03c15 Palindrome Partitioning II (Hard)
  • UniqueBinary Search Trees (Medium)
  • Unique Binary Search Trees II (Medium)
  • WordBreak (Medium)
  • #f03c15 WordBreak II (Hard)
  • #f03c15 Concatenated Words (Hard)
  • Can I Win (Medium)
  • Maximum Subarray (Easy)
  • #f03c15 Dungeon Game (Hard)
  • 2 Keys Keyboard (Medium)
  • #f03c15 Maximum Sum of 3 Non-Overlapping Subarrays (Hard)
  • Maximal Square (Medium)
  • Continuous Subarray Sum (Medium)
  • #f03c15 Decode Ways II (Hard)
  • Palindromic Substrings (Medium)
  • Number of Longest Increasing Subsequence (Medium)
  • Combination Sum IV (Medium)
  • #f03c15 Paint House II (Hard)
  • #f03c15 Split Array Largest Sum (Hard)
  • Number Of Corner Rectangles (Medium)
  • #f03c15 Burst Balloons (Hard)
  • Largest Plus Sign (Medium)
  • #f03c15 Palindrome Pairs (Hard)
  • #f03c15 Cherry Pickup (Hard)
  • Knight Probability in Chessboard (Medium)
  • Largest Sum of Averages (Medium)
  • #f03c15 Minimum Number of Refueling Stops (Hard)
  • #f03c15 Cat and Mouse (Hard)
  • Stone Game (Medium)
  • #f03c15 Odd Even Jump (Hard)
  • #f03c15 Profitable Schemes (Hard)
  • #f03c15 Maximum Vacation Days (Hard)
  • #f03c15 Russian Doll Envelopes (Hard)
  • #f03c15 Student Attendance Record II (Hard)
  • Out of Boundary Paths (Medium)
  • #f03c15 Remove Boxes (Hard)
  • #f03c15 Stickers to Spell Word (Hard)
  • Ones and Zeroes (Medium)
  • #f03c15 Encode String with Shortest Length (Hard)
  • Length of Longest Fibonacci Subsequence (Medium)
  • #f03c15 Make Array Strictly Increasing (Hard)
  • #f03c15 Minimum Number of Taps to Open to Water a Garden (Hard)
  • #f03c15 Delete Columns to Make Sorted III (Hard)
  • #f03c15 Handshakes That Don't Cross (Hard)
  • #f03c15 Minimum Difficulty of a Job Schedule (Hard)
  • #f03c15 Jump Game V (Hard)
  • #f03c15 Freedom Trail (Hard)
  • #f03c15 Strange Printer (Hard)
  • #f03c15 Minimum Cost to Merge Stones (Hard)
  • #f03c15 Interleaving String (Hard)
  • #f03c15 Count Vowels Permutation (Hard)
  • #f03c15 Non-negative Integers without Consecutive Ones (Hard)
  • Bomb Enemy (Medium)
  • Number of Dice Rolls With Target Sum (Medium)
  • #f03c15 Distinct Subsequences (Hard)
  • #f03c15 Distinct Subsequences II (Hard)
  • #f03c15 Minimum Distance to Type a Word Using Two Fingers (Hard)
  • #f03c15 Valid Palindrome III (Hard)
  • #f03c15 Palindrome Partitioning III (Hard)
  • #f03c15 Tiling a Rectangle with the Fewest Squares (Hard)
  • #f03c15 Longest Chunked Palindrome Decomposition (Hard)
  • #f03c15 Stone Game III (Hard)
  • #f03c15 Number of Ways to Stay in the Same Place After Some Steps (Hard)
  • Toss Strange Coins (Medium)
  • Knight Dialer (Medium)
  • #f03c15 Palindrome Removal (Hard)
  • #f03c15 Restore The Array (Hard)
  • #f03c15 Cherry Pickup II (Hard)
  • #f03c15 Constrained Subsequence Sum (Hard)
  • #f03c15 Largest Multiple of Three (Hard)
  • #f03c15 Largest Multiple of Three (Hard)
  • #f03c15 Number of Music Playlists (Hard)
  • #f03c15 Paint House III (Hard)
  • #f03c15 Shortest Path Visiting All Nodes (Hard)
  • #f03c15 Smallest Sufficient Team (Hard)
  • #f03c15 Stone Game IV (Hard)
  • #f03c15 Tallest Billboard (Hard)
  • #f03c15 Count Different Palindromic Subsequences (Hard)
  • #f03c15 Number of Paths with Max Score (Hard)

Greedy

  • Jump Game (Medium)
  • #f03c15 Jump Game II (Hard)
  • Course Schedule III (Medium)
  • GasStation (Medium)
  • Non-Overlapping Intervals (Medium)
  • Minimum Number of Arrows to Burst Balloons (Medium)
  • Queue Reconstruction By Height (Medium)
  • Task Scheduler (Medium)
  • Maximum Length of Pair Chain (Medium)
  • Lemonade Change (Easy)
  • Score After Flipping Matrix (Medium)
  • #f03c15 IPO (Hard)
  • String Without AAA or BBB (Medium)
  • Boats to Save People (Medium)
  • Broken Calculator (Medium)
  • Two City Scheduling (Easy)
  • #f03c15 Minimum Time to Build Blocks (Hard)
  • #f03c15 Reducing Dishes (Hard)

Hashing

  • Anagrams (Medium)
  • Group Anagrams (Medium)
  • Kdiff Pairs In a Array (Easy)
  • Sort Character by Frequency (Medium)
  • Two Sum (Easy)
  • Valid Anagram (Easy)
  • Maximum Size Subarray Sum Equals k (Medium)
  • Contiguous Array (Medium)
  • Brick Wall (Medium)
  • Partition Labels (Medium)
  • Custom Sort String (Medium)
  • Short Encoding of Words (Medium)
  • #f03c15 Substring with Concatenation of All Words (Hard)
  • Distribute Candies (Easy)
  • Groups of Special-Equivalent Strings (Easy)
  • #f03c15 Number of Atoms (Hard)
  • Analyze User Website Visit Pattern (Medium)
  • #f03c15 String Transforms Into Another String (Hard)

Heap

  • #f03c15 Sliding Window Maximum (Hard)
  • #f03c15 The Skyline Problem (Hard)
  • Meeting Rooms II (Medium)
  • Top K Frequent Words (Medium)
  • #f03c15 Candy (Hard)
  • #f03c15 Smallest Rotation with Highest Score (Hard)
  • #f03c15 Maximum Frequency Stack (Hard)
  • #f03c15 Reachable Nodes In Subdivided Graph (Hard)
  • K Closest Points to Origin (Medium)
  • Distant Barcodes (Medium)

Linked List

  • Intersection of two Linked-Lists (Easy)
  • Linked List Cycle (Easy)
  • #f03c15 Merge K Sorted Lists (Hard)
  • Merge Two Sorted List (Easy)
  • Paliandrome List (Easy)
  • Reverse Linked List (Easy)
  • Delete Node in a Linked List (Easy)
  • #f03c15 Reverse Nodes in k-Group (Hard)
  • Swap Nodes in Pairs (Medium)
  • Middle of Linked List (Easy)
  • Split Linked List in Parts (Medium)
  • Next Greater Node In Linked List (Medium)

Math

  • Add Two Numbers (Medium)
  • Count Primes (Easy)
  • Rotate Function (Medium)
  • Water and Jug Problem (Medium)
  • Add Digits (Easy)
  • Excel Sheet Column Title (Easy)
  • Roman to Integer (Easy)
  • Bulb Switcher II (Medium)
  • Global and Local Inversions (Medium)
  • Solve the Equation (Medium)
  • #f03c15 Couples Holding Hands (Hard)
  • #f03c15 Reaching Points (Hard)
  • #f03c15 Nth Magical Number (Hard)
  • Squirrel Simulation (Medium)
  • Projection Area of 3D Shapes (Easy)
  • Decoded String at Index (Medium)
  • Base 7 (Easy)
  • Smallest Range I (Easy)
  • #f03c15 Largest Component Size by Common Factor (Hard)
  • #f03c15 Super Washing Machines (Hard)
  • Rectangle Overlap (Easy)
  • Nth Digit (Easy)

Reservoir Sampling

  • Random Pick Index (Medium)

Stack

  • Min Stack (Easy)
  • Valid Parentheses (Easy)
  • #f03c15 Largest Rectangle In Histogram (Hard)
  • Implement Queue using Stacks (Easy)
  • #f03c15 Maximal Rectangle (Hard)
  • Exclusive Time of Functions (Medium)
  • #f03c15 Basic Calculator (Hard)
  • Decode String (Medium)
  • #f03c15 Longest Valid Parentheses (Hard)

String

  • First Unique Character In a String (Easy)
  • Repeated Substring Pattern (Easy)
  • Reverse Words In a String (Medium)
  • ReverseWords II (Medium)
  • String to Integer (Medium)
  • #f03c15 Text Justification (Hard)
  • ZigZag Conversion (Medium)
  • Implement StrStr (Easy)
  • Excel Sheet Column Number (Easy)
  • Compare Version Numbers (Easy)
  • Valid Palindrome (Easy)
  • Simplify Path (Medium)
  • Permutation in String (Medium)
  • Add Binary (Easy)
  • Valid Palindrome II (Easy)
  • One Edit Distance (Medium)
  • Count and Say (Easy)
  • Multiply Strings (Medium)
  • Longest Word in Dictionary through Deleting (Medium)
  • Isomorphic Strings (Easy)
  • String Compression (Easy)
  • Longest Common Prefix (Easy)
  • #f03c15 Find the Closest Palindrome (Hard)
  • Monotone Increasing Digits (Medium)
  • #f03c15 Shortest Palindrome (Hard)
  • Valid Word Abbreviation (Easy)
  • Longest Palindrome (Easy)
  • Replace Words (Medium)
  • Rotate String (Easy)
  • Keyboard Row (Easy)
  • Student Attendance Record I (Easy)
  • Split Concatenated Strings (Medium)
  • Valid Word Square (Easy)
  • Reconstruct Original Digits from English (Medium)
  • Push Dominoes (Medium)
  • Validate IP Address (Medium)
  • Reverse String II (Easy)
  • Find Words That Can Be Formed by Characters (Easy)
  • Minimum Add to Make Parentheses Valid (Medium)

Tree

  • Binaray Tree Right Side View (Medium)
  • #f03c15 Binary Tree Maximum Path Sum (Hard)
  • Boundary of Binary Tree (Medium)
  • Convert sorted array to BST (Medium)
  • Lowest Common Ancestor of a Binary Tree (Medium)
  • Lowest Common Ancestor of a BST (Easy)
  • Most Frequent Subtree Sum (Medium)
  • Path Sum III (Easy)
  • Convert Postorder and Inorder traversal to Binary Tree (Medium)
  • Convert Preorder and Inorder traversal to Binary Tree (Medium)
  • Sorted Array to BST (Medium)
  • Valid Binary Search Tree (Medium)
  • Largest BST Subtree (Medium)
  • Closest Binary Search Tree Value (Easy)
  • Inorder Successor in BST (Medium)
  • Construct String From Binary Tree (Easy)
  • Flatten Binary Tree to Linked List (Medium)
  • Populating Next Right Pointers in Each Node (Medium)
  • Populating Next Right Pointers in Each Node II (Medium)
  • Subtree of Another Tree (Easy)
  • Binary Tree Zigzag Level Order Traversal (Medium)
  • Binary Tree Inorder Traversal (Medium)
  • Symmetric Tree (Easy)
  • Maximum Binary Tree (Medium)
  • Find Bottom Left Tree Value (Medium)
  • Diameter of Binary Tree (Easy)
  • Binary Tree Paths (Easy)
  • Sum of Left Leaves (Easy)
  • Two Sum IV - Input is a BST (Easy)
  • Average of Levels in Binary Tree (Easy)
  • Convert Binary Search Tree to Sorted Doubly Linked List (Easy)
  • Same Tree (Easy)
  • Binary Tree Longest Consecutive SequencefindMinDifference II (Medium)
  • Minimum Absolute Difference in BST (Medium)
  • Equal Tree Partition (Medium)
  • Split BST (Medium)
  • Closest Leaf in a Binary Tree (Medium)
  • Maximum Width of Binary Tree (Medium)
  • #f03c15 Recover Binary Search Tree (Hard)
  • #f03c15 Binary Tree Postorder Traversal (Hard)
  • #f03c15 Serialize and Deserialize N-ary Tree (Hard)
  • Convert BST to Greater Tree (Easy)
  • All Nodes Distance K in Binary Tree (Medium)
  • All Possible Full Binary Trees (Medium)
  • Flip Equivalent Binary Trees (Medium)
  • Construct Binary Tree from String (Medium)
  • Find Largest Value in Each Tree Row (Medium)
  • Find Bottom Left Tree Value (Medium)
  • Maximum Level Sum of a Binary Tree (Medium)
  • Leaf-Similar Trees (Easy)
  • Binary Tree Tilt (Easy)

Two Pointers

  • Four Sum (Medium)
  • Longest Substring Witout Repeats (Medium)
  • Three Sum (Medium)
  • #f03c15 Trapping Rain Water (Hard)
  • 3Sum Closest (Medium)
  • Move Zeroes (Easy)
  • Remove Duplicates (Easy)
  • Remove Duplicates II (Medium)
  • Minimum Size Subarray Sum (Medium)
  • #f03c15 Minimum Window Substring (Hard)
  • #f03c15 Smallest Range (Hard)
  • Subarray Product Less Than K (Medium)
  • Number of Matching Subsequences (Medium)
  • #f03c15 Subarrays with K Different Integers (Hard)
  • #f03c15 Last Substring in Lexicographical Order (Hard)