## DEV Community is a community of 674,199 amazing developers

We're a place where coders share, stay up-to-date and grow their careers.

# Leetcode Solutions Series' Articles

Back to seanpgallivan's Series
Solution: Next Permutation

## Solution: Next Permutation

Solution: Trim a Binary Search Tree

## Solution: Trim a Binary Search Tree

Leetcode Solutions Index

## Leetcode Solutions Index

Solution: Minimize Deviation in Array

## Solution: Minimize Deviation in Array

Solution: Vertical Order Traversal of a Binary Tree

## Solution: Vertical Order Traversal of a Binary Tree

Solution: Count Ways to Make Array With Product

## Solution: Count Ways to Make Array With Product

Solution: Smallest String With A Given Numeric Value

## Solution: Smallest String With A Given Numeric Value

Solution: Path With Minimum Effort

## Solution: Path With Minimum Effort

Solution: Concatenation of Consecutive Binary Numbers

## Solution: Concatenation of Consecutive Binary Numbers

Solution: Minimum Operations to Make a Subsequence

## Solution: Minimum Operations to Make a Subsequence

Solution: Longest Harmonious Subsequence

## Solution: Longest Harmonious Subsequence

Solution: Simplify Path

## Solution: Simplify Path

Solution: Building Boxes

## Solution: Building Boxes

Solution: Decode XORed Permutation

## Solution: Decode XORed Permutation

Solution: Binary Tree Right Side View

## Solution: Binary Tree Right Side View

Solution: Find Kth Largest XOR Coordinate Value

## Solution: Find Kth Largest XOR Coordinate Value

Solution: Change Minimum Characters to Satisfy One of Three Conditions

## Solution: Change Minimum Characters to Satisfy One of Three Conditions

Solution: Shortest Distance to a Character

## Solution: Shortest Distance to a Character

Solution: Peeking Iterator

## Solution: Peeking Iterator

Solution: Convert BST to Greater Tree

## Solution: Convert BST to Greater Tree

Solution: Copy List with Random Pointer

## Solution: Copy List with Random Pointer

Solution: Valid Anagram

## Solution: Valid Anagram

Solution: Number of Steps to Reduce a Number to Zero

## Solution: Number of Steps to Reduce a Number to Zero

Solution: Shortest Path in Binary Matrix

## Solution: Shortest Path in Binary Matrix

Solution: Is Graph Bipartite?

## Solution: Is Graph Bipartite?

Solution: Maximum Score From Removing Substrings (ver. 1)

## Solution: Maximum Score From Removing Substrings (ver. 1)

Solution: Maximum Score From Removing Substrings (ver. 2)

## Solution: Maximum Score From Removing Substrings (ver. 2)

Solution: Sort the Matrix Diagonally

## Solution: Sort the Matrix Diagonally

Solution: The K Weakest Rows in a Matrix (ver. 1)

## Solution: The K Weakest Rows in a Matrix (ver. 1)

Solution: The K Weakest Rows in a Matrix (ver. 2)

## Solution: The K Weakest Rows in a Matrix (ver. 2)

Solution: Letter Case Permutation

## Solution: Letter Case Permutation

Solution: Container With Most Water

## Solution: Container With Most Water

Solution: Arithmetic Slices

## Solution: Arithmetic Slices

Solution: Minimum Remove to Make Valid Parentheses

## Solution: Minimum Remove to Make Valid Parentheses

Solution: Roman to Integer

## Solution: Roman to Integer

Solution: Broken Calculator

## Solution: Broken Calculator

Solution: Find the Most Competitive Subsequence

## Solution: Find the Most Competitive Subsequence

Solution: Longest Word in Dictionary through Deleting

## Solution: Longest Word in Dictionary through Deleting

Solution: Search a 2D Matrix II

## Solution: Search a 2D Matrix II

Solution: Score of Parentheses

## Solution: Score of Parentheses

Solution: Shortest Unsorted Continuous Subarray

## Solution: Shortest Unsorted Continuous Subarray

Solution: Validate Stack Sequences

## Solution: Validate Stack Sequences

Solution: Divide Two Integers (ver. 1)

## Solution: Divide Two Integers (ver. 1)

Solution: Divide Two Integers (ver. 2)

## Solution: Divide Two Integers (ver. 2)

Solution: Maximum Frequency Stack

## Solution: Maximum Frequency Stack

Solution: Distribute Candies

## Solution: Distribute Candies

Solution: Set Mismatch (ver. 1)

## Solution: Set Mismatch (ver. 1)

Solution: Set Mismatch (ver. 2)

## Solution: Set Mismatch (ver. 2)

Solution: Missing Number

## Solution: Missing Number

Solution: Intersection of Two Linked Lists

## Solution: Intersection of Two Linked Lists

Solution: Average of Levels in Binary Tree

## Solution: Average of Levels in Binary Tree

Solution: Short Encoding of Words (ver. 1)

## Solution: Short Encoding of Words (ver. 1)

Solution: Design HashMap (ver. 1)

## Solution: Design HashMap (ver. 1)

Solution: Short Encoding of Words (ver. 2)

## Solution: Short Encoding of Words (ver. 2)

Solution: Design HashMap (ver. 2)

## Solution: Design HashMap (ver. 2)

Solution: Remove Palindromic Subsequences

## Solution: Remove Palindromic Subsequences

Solution: Add One Row to Tree

## Solution: Add One Row to Tree

Solution: Integer to Roman

## Solution: Integer to Roman

Solution: Coin Change

## Solution: Coin Change

Solution: Check If a String Contains All Binary Codes of Size K

## Solution: Check If a String Contains All Binary Codes of Size K

Solution: Binary Trees With Factors

## Solution: Binary Trees With Factors

Solution: Swapping Nodes in a Linked List

## Solution: Swapping Nodes in a Linked List

Solution: Encode and Decode TinyURL

## Solution: Encode and Decode TinyURL

Solution: Best Time to Buy and Sell Stock with Transaction Fee

## Solution: Best Time to Buy and Sell Stock with Transaction Fee

Solution: Generate Random Point in a Circle

## Solution: Generate Random Point in a Circle

Solution: Wiggle Subsequence

## Solution: Wiggle Subsequence

Solution: Keys and Rooms

## Solution: Keys and Rooms

Solution: Design Underground System

## Solution: Design Underground System

Solution: Reordered Power of 2

## Solution: Reordered Power of 2

Solution: Vowel Spellchecker

## Solution: Vowel Spellchecker

Solution: 3Sum With Multiplicity

## Solution: 3Sum With Multiplicity

Solution: Pacific Atlantic Water Flow

## Solution: Pacific Atlantic Water Flow

Solution: Word Subsets

## Solution: Word Subsets

Solution: Palindromic Substrings

## Solution: Palindromic Substrings

Solution: Reconstruct Original Digits from English

## Solution: Reconstruct Original Digits from English

Solution: Flip Binary Tree To Match Preorder Traversal

## Solution: Flip Binary Tree To Match Preorder Traversal

Solution: Russian Doll Envelopes

## Solution: Russian Doll Envelopes

Solution: Stamping The Sequence

## Solution: Stamping The Sequence

Solution: Ones and Zeroes

## Solution: Ones and Zeroes

Solution: Longest Valid Parentheses

## Solution: Longest Valid Parentheses

Solution: Design Circular Queue

## Solution: Design Circular Queue

Solution: Global and Local Inversions

## Solution: Global and Local Inversions

Solution: Minimum Operations to Make Array Equal

## Solution: Minimum Operations to Make Array Equal

Solution: Determine if String Halves Are Alike

## Solution: Determine if String Halves Are Alike

Solution: Letter Combinations of a Phone Number

## Solution: Letter Combinations of a Phone Number

Solution: Verifying an Alien Dictionary

## Solution: Verifying an Alien Dictionary

Solution: Longest Increasing Path in a Matrix

## Solution: Longest Increasing Path in a Matrix

Solution: Deepest Leaves Sum

## Solution: Deepest Leaves Sum

Solution: Beautiful Arrangement II

## Solution: Beautiful Arrangement II

Solution: Flatten Nested List Iterator

## Solution: Flatten Nested List Iterator

Solution: Partition List

## Solution: Partition List

Solution: Fibonacci Number

## Solution: Fibonacci Number

Solution: Remove All Adjacent Duplicates in String II

## Solution: Remove All Adjacent Duplicates in String II

Solution: Number of Submatrices That Sum to Target

## Solution: Number of Submatrices That Sum to Target

Solution: Remove Nth Node From End of List

## Solution: Remove Nth Node From End of List

Solution: Combination Sum IV

## Solution: Combination Sum IV

Solution: N-ary Tree Preorder Traversal

## Solution: N-ary Tree Preorder Traversal

Solution: Triangle

## Solution: Triangle

Solution: Brick Wall

## Solution: Brick Wall

Solution: Count Binary Substrings

## Solution: Count Binary Substrings

Solution: Critical Connections in a Network

## Solution: Critical Connections in a Network

Solution: Rotate Image

## Solution: Rotate Image

Solution: Furthest Building You Can Reach

## Solution: Furthest Building You Can Reach

Solution: Power of Three

## Solution: Power of Three

Solution: Unique Paths II

## Solution: Unique Paths II

Solution: Find First and Last Position of Element in Sorted Array

## Solution: Find First and Last Position of Element in Sorted Array

Solution: Powerful Integers

## Solution: Powerful Integers

Solution: Prefix and Suffix Search

## Solution: Prefix and Suffix Search

Solution: Course Schedule III

## Solution: Course Schedule III

Solution: Running Sum of 1d Array

## Solution: Running Sum of 1d Array

Solution: Non-decreasing Array

## Solution: Non-decreasing Array

Solution: Jump Game II

## Solution: Jump Game II

Solution: Convert Sorted List to Binary Search Tree

## Solution: Convert Sorted List to Binary Search Tree

Solution: Delete Operation for Two Strings

## Solution: Delete Operation for Two Strings

Solution: Super Palindromes

## Solution: Super Palindromes

Solution: Construct Target Array With Multiple Sums

## Solution: Construct Target Array With Multiple Sums

Solution: Count Primes

## Solution: Count Primes

Solution: Maximum Points You Can Obtain from Cards

## Solution: Maximum Points You Can Obtain from Cards

Solution: Range Sum Query 2D - Immutable

## Solution: Range Sum Query 2D - Immutable

Solution: Ambiguous Coordinates

## Solution: Ambiguous Coordinates

Solution: Flatten Binary Tree to Linked List

## Solution: Flatten Binary Tree to Linked List

Solution: Valid Number

## Solution: Valid Number

Solution: Binary Tree Cameras

## Solution: Binary Tree Cameras

Solution: Longest String Chain

## Solution: Longest String Chain

Solution: Find Duplicate File in System

## Solution: Find Duplicate File in System

Solution: Minimum Moves to Equal Array Elements II

## Solution: Minimum Moves to Equal Array Elements II

Solution: Binary Tree Level Order Traversal

## Solution: Binary Tree Level Order Traversal

Solution: Find and Replace Pattern

## Solution: Find and Replace Pattern

Solution: N-Queens

## Solution: N-Queens

Solution: To Lower Case

## Solution: To Lower Case

Solution: Evaluate Reverse Polish Notation

## Solution: Evaluate Reverse Polish Notation

Solution: Partitioning Into Minimum Number Of Deci-Binary Numbers

## Solution: Partitioning Into Minimum Number Of Deci-Binary Numbers

Solution: Maximum Product of Word Lengths

## Solution: Maximum Product of Word Lengths

Solution: Maximum Erasure Value

## Solution: Maximum Erasure Value

Solution: N-Queens II

## Solution: N-Queens II

Solution: Maximum Gap

## Solution: Maximum Gap

Solution: Search Suggestions System

## Solution: Search Suggestions System

Solution: Max Area of Island

## Solution: Max Area of Island

Solution: Interleaving String

## Solution: Interleaving String

Solution: Maximum Area of a Piece of Cake After Horizontal and Vertical Cuts

## Solution: Maximum Area of a Piece of Cake After Horizontal and Vertical Cuts

Solution: Open the Lock

## Solution: Open the Lock

Solution: Maximum Performance of a Team

## Solution: Maximum Performance of a Team

Solution: Longest Consecutive Sequence

## Solution: Longest Consecutive Sequence

Solution: Min Cost Climbing Stairs

## Solution: Min Cost Climbing Stairs

Solution: Construct Binary Tree from Preorder and Inorder Traversal

## Solution: Construct Binary Tree from Preorder and Inorder Traversal

Solution: Jump Game VI

## Solution: Jump Game VI

Solution: My Calendar I

## Solution: My Calendar I

Solution: Stone Game VII

## Solution: Stone Game VII

Solution: Minimum Number of Refueling Stops

## Solution: Minimum Number of Refueling Stops

Solution: Palindrome Pairs

## Solution: Palindrome Pairs

Solution: Maximum Units on a Truck

## Solution: Maximum Units on a Truck

Solution: Matchsticks to Square

## Solution: Matchsticks to Square

Solution: Generate Parentheses

## Solution: Generate Parentheses

Solution: Number of Subarrays with Bounded Maximum

## Solution: Number of Subarrays with Bounded Maximum

Solution: Swim in Rising Water

## Solution: Swim in Rising Water

Solution: Pascal's Triangle