/Leetcode-Problems

Collection of LeetCode questions to ace the coding interview!

Primary LanguageJava

Problems

Collection of Standard Problems and my solutions

Arrays and Strings

Sort an array
Rotate array
Removing minimum and maximum from array
Sort colors
Pancake sorting
Minimum steps to make product equal to one
Shortest distance to a character
Push dominoes
First missing positive
Maximum subarray
Maximum sum circular subarray
Smallest range II
Global and local inversions
Reverse pairs
Squares of a sorted array
Rearrange an array with O(1) space
Rearrange array elements by sign
Count and say
Add strings
Multiply strings
Equal sum arrays with minimum number of operations

Two pointer approach / Sliding window

Two sum
3 sum
3 sum closest
4 sum
Container with most water
Max consecutive ones III
Valid triangle number
Number of subsequences that satisfy the given sum condition
Longest continuous increasing subsequence
First negative integer in every window of size k
Minimum size subarray sum
Find all anagrams in a string
Sliding window maximum
Longest continuous subarray with absolute difference less than or equal to limit
Minimum window substring

Linked list

Reverse linked list
Delete node in a linked list
Linked list cycle
Linked list cycle II
Remove Nth node from end of list
Palindrome linked list
Add 1 to a number represented as linked list
Add two numbers
Partition list
Intersection of two linked lists
Sort Linked list (Merge sort and Quick Sort)
Swap nodes in pairs
Reverse node in k group
Merge k sorted lists
Copy list with random pointer

Stack and Queues

Valid parentheses
Implement queue using stack
Implement stack using queue
Min stack
Sort a stack
LRU Cache
Basic calculator
Next greater element II
Online stock span
Maximal rectangle
Smallest subsequence of distinct characters
Steps to make array non decreasing
Longest valid parentheses

Heap

Kth largest element in an array
Sort a K nearly sorted array
Find k closest elements
Top K frequent elements
Frequency sort
K closest points to origin
Merge k sorted lists
Reorganize string
Find k pairs with smallest sums

Tree

Binary tree inorder traversal
Binary tree preorder traversal
Binary tree postorder traversal
Binary tree level order traversal
Binary tree zingzag level order traversal
Convert Sorted Array to Binary Search Tree
Convert Sorted List to Binary Search Tree
Diameter of binary tree
Invert binary tree
Delete node in a BST
Symmetric tree
Validate binary search tree
Right view of a binary tree
Boundary traversal of a binary tree
Vertical order traversal of a binary tree
Top view of a Binary tree
Bottom view of a Binary tree
Diagonal traversal of Binary tree
Find mode in Binary Search Tree
Lowest common ancestor of a binary search tree
Lowest common ancestor of a binary tree
Path sum
Path sum II
Path sum III
Maximum width of Binary tree
Count complete tree nodes
All nodes distance K in a binary tree
Construct binary tree from preorder and inorder traversal
Construct binary tree from inorder and postorder traversal
Construct binary search tree from preorder traversal
Flatten binary tree to linked list
Binary search tree iterator
Two Sum IV - Input is a BST
Longest zigzag path in a binary tree
Recover binary search tree
Find duplicate subtrees
Maximum sum BST in binary tree
Linked list in Binary Tree
Binary tree maximum path sum
House robber III
Binary tree cameras
Min distance between two given nodes of a binary tree
Kth ancestor of a tree node

Backtracking

Rat in a maze
Letter combinations of a phone number
Lexicographical numbers
Generate parentheses
Permutations
Unique paths III
Combination sum
Palindrome partitioning
Word Break II
N queens
Sudoko solver
Increasing subsequences
Unique Binary search trees II
Restore IP addresses

Dynamic programming

Climbing stairs
Min cost climbing stairs
House Robber
Delete and earn
Unique paths
Unique paths II
Decode ways
Decode ways II
Binary tree with factors
Unique binary search trees
Word Break
Minimum path sum
Maximal square
Count square submatrices with all ones
0-1 Knapsack
Subset sum
Partition equal subset sum
Minimum sum partition
Target sum
Knapsack with duplicate items
Rod cutting
Coin change 2: Max number of ways
Coin change: Min number of coins
Perfect squares
Integer break
Combination sum IV
Minimum cost for tickets
Ones and zeroes
Longest common subsequence and Print LCS
Longest common substring
Maximum length of repeated subarray
Length of longest subsequence of a string which is substring of another string
Number of times a string occurs as subsequence in another string
Shortest common supersequence
Print shortest common supersequence
Minimum number of deletions and insertions
Delete operations for two strings
Longest palindromic subsequence
Longest repeating subsequence
Minimum number of deletions to make a string palindrome
Minimum number of insertions to make a string palindrome
Count common subsequence in two strings (Need not necessarily be distinct)
Longest palindromic substring
Count palindromic substrings
Count palindromic subsequences
Edit distance
Distinct subsequences
Distinct subsequences II
Count different palindromic subsequences
Matrix chain multiplication
Palindrome partitioning II
Palindrome partitioning IV
Palindrome partitioning III
Partition array for maximum sum
Boolean Parenthesization
Scramble string
Super egg drop
Burst balloons
Longest increasing subsequence
Number of longest increasing subsequence
Longest ideal subsequence
Student attendance record II
Word wrap
Domino and tromino tiling
Maximum earnings from taxi

Binary search

Guess number higher or lower
Search insert position
Sqrt(x)
Peak index in a mountain array
Find in mountain array
Find first position and last position of element in sorted array
Count the number of occurences of an element in a sorted array
Find minimum in rotated sorted array / No of times a sorted array is rotated
Find minimum in rotated sorted array II
Search in rotated sorted array
Kth missing positive number
Find peak element
Median of two sorted arrays
Longest increasing subsequence
Successful pairs of spells and potions
Maximum profit in job scheduling

BFS and DFS

Flood fill
Keys and rooms
Path with maximum gold
Open the lock
Steps by knight
Jump Game IV
Jump Game V
Nearest exit from entrance in maze
Shortest path in a grid with obstacles elimination
Rotting oranges
Sequential digits
Surrounded regions
Word ladder
Longest increasing path in a matrix
Out of boundary paths
01 Matrix
Shortest bridge
Minesweeper
Race car
Minimum jumps to reach home

Graphs

Find the town judge
Number of islands
Time needed to inform all employees
Number of provinces
Number of operations to make network connected
Detect cycle in an undirected graph
Detect cycles in 2d grid
Detect cycle in a directed graph
Course schedule
Clone graph
Is graph bipartite
Topological sort
Minimum height trees
Detonate the maximum bombs
Find eventual safe states
Sum of distances in tree
Redundant connection
Redundant connection II
Network delay time
Path with maximum probability
Cheapest flight within k stops
Evaluate division
Path with minimum effort
Flower planting with no adjacent
Find the City With the Smallest Number of Neighbors at a Threshold Distance

Greedy

Fractional knapsack
Jump Game
Jump Game II
Gas station
Minimum number of taps to open to water a garden
Minimum number of fibonacci numbers whose sum is k
Candy
Integer replacement
Shortest unsorted continuous subarray
Maximum number of events that can be attended

Bit manipulation

Single number
Single number II
Single number III
Counting bits
Subsets
Divide two integers
Reverse bits
Bitwise AND of Numbers Range
Maximum XOR of two numbers in an array
Super Pow

Matrix

Rotate image
Spiral matrix
Spiral matrix II
Diagonal traverse
Search a 2d matrix
Search in a 2d matrix II
Kth smallest element in a sorted matrix
Maximum sum rectangle in a 2d matrix
Largest rectangular submatrix with sum 0
Image overlap

Buy and sell stock Problems

Buy and sell stock with one transaction allowed
Buy and sell stock with infinite transactions allowed
Buy and sell stock with infinite transactions and transaction fee
Buy and sell stock with infinite transactions and cooldown
Buy and sell stock with two transactions allowed
Buy and sell stock with k transactions allowed

Interval questions

Merge intervals
Insert interval
Count integers in intervals
Maximum length of pair chain
Non overlapping intervals
Minimum number of arrows to burst balloons
Interval list intersections
My Calendar I

Standard problems

Container with most water
Car pooling
Number of minimum picks to get 'k' pairs of socks from a drawer
Next permutation
Majority element
Majority element II
Subarray sum equals k
Median of two sorted arrays
Largest number
Trapping rainwater
Largest Rectangle in Histogram
Koko eating bananas
Find all duplicates in an array
The celebrity problem
Triangle
Josephus problem
Push dominoes
Flatten binary tree to linked list
Longest increasing subsequence
Longest consecutive subsequence
Water and jug problem
Maximum product subarray
Minimum number of removals to make mountain array

Extras

Integer to roman
Roman to integer
Possible groups
Count numbers with unique digits
Word subsets
Remove palindromic subsequences
Longest substring without repeating characters
Longest subarray with sum K
Minimum operations to reduce X to zero
Capacity to ship packages within d days
Find minimum time to finish all jobs
Minimum moves to equal array elements
Subarray product less than K
Subarray sum divisible by K
Minimum number of flips to make binary string alternating
Minimum number of swaps to make the string balanced
Count array pairs divisible by k
Mirror reflection

Group of Questions

Subarray sum

Subarray sum equals K
Maximum sum subarray of size K
Longest subarray with sum K with array containing only positive numbers
Longest subarray with sum K with array containing both positive and negative numbers
Maximum sum subarray in an array
Maximum sum circular subarray
Subarray product less than K
Subarray sum divisible by K
Count array pairs whose sum is divisible by K
Count array pairs whose product is divisible by K
Maximum product subarray

Traversals and view of Binary Tree

Inorder traversal
Preorder traversal
Postorder traversal
Level order traversal
Spiral traversal
Left view of Binary tree
Right view of Binary tree
Top view of Binary tree
Bottom view of Binary tree
Boundary traversal of Binary tree
Vertical order traversal of a Binary tree
Diagonal traversal of a Binary tree