-
Connected Components in an Undirected Graph [Solution]
-
Connected Components in an Undirected Graph
Given an undirected graph represented as an adjacency list, implement a function to find the number of connected components in the graph.
-
Count Bits [Solution]
-
Count Bits
Given a non-negative integer
num
, create an arrayresult
of lengthnum + 1
where each elementresult[i]
represents the number of 1’s in the binary representation ofi
. -
Missing Positive [Solution]
-
Missing Positive
Given an unsorted integer array, find the smallest missing positive integer.
-
Binary Tree Path Sum [Solution]
-
Binary Tree Path Sum
Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum.
-
Bitonic Array Maximum [Solution]
-
Bitonic Array Maximum
A bitonic array is an array that starts strictly increasing and then strictly decreasing. Given a bitonic array
arr
of distinct integers, find the maximum element in the array. You may assume that the array always contains at least one element. -
Inorder Successor in Binary Search Tree [Solution]
-
Inorder Successor in Binary Search Tree
Given a binary search tree (BST) and a node
p
in it, find the in-order successor of that node in the BST. -
Maximum Subarray Sum [Solution]
-
Maximum Subarray Sum
Given an array of integers, find the contiguous subarray (containing at least one number) that has the largest sum and return its sum.
-
Course Schedule [Solution]
-
Course Schedule
There are a total of
numCourses
courses you have to take, labeled from0
tonumCourses - 1
. You are given an arrayprerequisites
whereprerequisites[i] = [y, x]
indicates that you must take coursex
first if you want to take coursey
. -
Longest Increasing Subsequence [Solution]
-
Longest Increasing Subsequence
Given an unsorted array of integers, find the length of the longest increasing subsequence (LIS).
-
Search in Rotated Sorted Array [Solution]
-
Search in Rotated Sorted Array
Suppose an array of length
n
sorted in ascending order is rotated between1
andn
times. Given a target valuetarget
, write a function to search for the target in the rotated sorted array. -
Lowest Common Ancestor in Binary Tree [Solution]
-
Lowest Common Ancestor in Binary Tree
Given a binary tree, find the lowest common ancestor (LCA) of two given nodes
p
andq
in the tree. -
Symmetric Binary Tree [Solution]
-
Symmetric Binary Tree
Given a binary tree, check whether it is a symmetric tree. A symmetric tree is a mirror image of itself with respect to its center.
-
Longest Palindromic Substring [Solution]
-
Longest Palindromic Substring
Given a string, find the longest palindromic substring in it. A palindrome is a word, phrase, or sequence that reads the same backward as forward. A substring is a contiguous sequence of characters within the string.
-
Longest Common Prefix [Solution]
-
Longest Common Prefix
Write a function to find the longest common prefix string amongst an array of strings.
-
Rotate Image [Solution]
-
Rotate Image
You are given an
n x n
2D matrix representing an image, rotate the image by 90 degrees (clockwise). -
Median of Two Sorted Arrays [Solution]
-
Median of Two Sorted Arrays
You are given two sorted arrays,
nums1
andnums2
, wherenums1
has sizem
andnums2
has sizen
. Your task is to find the median of the two sorted arrays. If possible, the overall run time complexity should be O(log(min(m, n))
). -
Word Search 2 [Solution]
-
Word Search 2
Given a 2D board of characters and a list of words, find all words in the board.
-
Minimize Meeting Rooms [Solution]
-
Minimize Meeting Rooms
Given a list of meetings with start and end times, determine the minimum number of meeting rooms required to accommodate all the meetings. The start and end times of the meetings are represented as tuples
(start_time, end_time)
. -
Minimum Cost to Climb Stairs [Solution]
-
Minimum Cost to Climb Stairs
You are given an array
cost
wherecost[i]
is the cost ofi
-th step on a staircase. You can start from either the0
-th step or the1
-st step. Each step can be climbed by paying the cost specified, and you can either climb one step or two steps at a time. -
Longest Substring with K Distinct Characters [Solution]
-
Longest Substring with K Distinct Characters
Given a string
s
and an integerk
, find the length of the longest substring with at mostk
distinct characters. -
Reverse a Linked List [Solution]
-
Reverse a Linked List
You are given a singly linked list. Write a function to reverse the linked list.
-
Product of Array Except Self [Solution]
-
Product of Array Except Self
Given an array
nums
ofn
integers wheren > 1
, return an arrayoutput
such thatoutput[i]
is equal to the product of all the elements ofnums
exceptnums[i]
. -
Coin Change [Solution]
-
Coin Change
Given a set of coin denominations and a target amount, determine the minimum number of coins needed to make up that amount.
-
Kth Largest Element in an Array [Solution]
-
Kth Largest Element in an Array
Given an integer array
nums
and an integerk
, return thek
th largest element in the array. Note that it is thek
th largest element in the sorted order, not thek
th distinct element. -
Word Search [Solution]
-
Word Search
Given a 2D board of letters and a word, determine if the word exists in the grid.
-
Valid Parentheses [Solution]
-
Valid Parentheses
Given a string containing just the characters
(
,)
,{
,}
,[
and]
, determine if the input string is valid. -
Merge Overlapping Intervals [Solution]
-
Merge Overlapping Intervals
Given a list of intervals, where each interval is represented as a pair of integers
[start, end]
, write a function to merge overlapping intervals. -
Minimum Window Substring [Solution]
-
Minimum Window Substring
Given a string
s
and a stringt
, find the minimum window ins
that contains all the characters oft
in any order. If there is no such window, return an empty string “”. -
Two Sum [Solution]
-
Two Sum
Given an array of integers
nums
and an integertarget
, return the indices of the two numbers such that they add up to the target. -
Subarray Product Less Than K [Solution]
-
Subarray Product Less Than K
Given an array of positive integers and an integer
k
, find the number of contiguous subarrays where the product of all the elements is less thank
. -
Design a Doubly Linked List [Solution]
-
Design a Doubly Linked List
Design a doubly linked list (DLL) with the following operations:
insert_at_head(val)
: Insert a node with the given value at the beginning of the doubly linked list.insert_at_tail(val)
: Insert a node with the given value at the end of the doubly linked list.insert_after_node(node, val)
: Insert a node with the given value after the specified node in the doubly linked list.delete_at_head()
: Delete the node at the beginning of the doubly linked list.delete_at_tail()
: Delete the node at the end of the doubly linked list.delete_node(node)
: Delete the specified node from the doubly linked list.display()
: Display the elements of the doubly linked list.
-
Word Break [Solution]
-
Word Break
Given a non-empty string and a dictionary containing a list of non-empty words, determine if s can be segmented into a space-separated sequence of one or more dictionary words.
-
Kth Smallest Element in a Sorted Matrix [Solution]
-
Kth Smallest Element in a Sorted Matrix
Given an
n x n
matrix where each of the rows and columns are sorted in ascending order, find thek
th smallest element in the matrix. -
Maximum Depth of Binary Tree [Solution]
-
Maximum Depth of Binary Tree
Given the root of a binary tree, find its maximum depth.
-
Two Sum Less Than K [Solution]
-
Two Sum Less Than K
Given an array
nums
of integers and an integerk
, find two distinct indicesi
andj
in the array such thatnums[i] + nums[j]
is the maximum possible value less thank
. If no such indices exist, return-1
. -
Container With Most Water [Solution]
-
Container With Most Water
Given
n
non-negative integersa1, a2, ..., an
, where each represents a point at coordinate(i, ai)
,n
vertical lines are drawn such that the two endpoints of the linei
are at(i, ai)
and(i, 0)
. Find two lines, which, together with the x-axis, forms a container that would hold the greatest amount of water. Return the maximum area of the water it can contain. -
Perfect Squares Sum [Solution]
-
Perfect Squares Sum
Given a positive integer
n
, find the least number of perfect square numbers (for example,1
,4
,9
,16
, …) that sum up ton
. -
Anagram Groups [Solution]
-
Anagram Groups
Given a list of strings, write a function to group the strings into sets of anagrams.
-
Sum of Left Leaves in Binary Tree [Solution]
-
Sum of Left Leaves in Binary Tree
Given the root of a binary tree, return the sum of all left leaves.
-
Binary Tree Maximum Path Sum [Solution]
-
Binary Tree Maximum Path Sum
Given a binary tree, find the maximum path sum. The path may start and end at any node in the tree.
-
Valid Sudoku [Solution]
-
Valid Sudoku
Determine whether a 9 x 9 Sudoku board is valid.
-
Longest Consecutive Sequence in an Array [Solution]
-
Longest Consecutive Sequence in an Array
Given an unsorted array of integers, find the length of the longest consecutive elements sequence.
-
Kth Smallest Element in a Binary Search Tree [Solution]
-
Kth Smallest Element in a Binary Search Tree
Given a binary search tree (BST), find the kth smallest element in it.
-
Invert Binary Tree [Solution]
-
Invert Binary Tree
Given the root of a binary tree, invert the tree by swapping the left and right children of each node.
-
Find the Missing Number [Solution]
-
Find the Missing Number
You are given an array of length
n
containingn
distinct numbers taken from the range0
ton
. Since the array hasn
distinct numbers and the numbers are in the range0
ton
, there is exactly one number missing. -
Sum of Digits Until a Single Digit [Solution]
-
Sum of Digits Until a Single Digit
Given a non-negative integer, repeatedly add all its digits until the result has only one digit.
-
Unique Paths [Solution]
-
Unique Paths
A robot is located at the top-left corner of a
m x n
grid. The robot can only move either down or right at any point in time. The robot is trying to reach the bottom-right corner of the grid. How many possible unique paths are there? -
Minimum Cost to Reach Destination in a Weighted Graph [Solution]
-
Minimum Cost to Reach Destination in a Weighted Graph
You are given a weighted graph represented by an adjacency matrix graph where
graph[i][j]
represents the cost to move from nodei
to nodej
. Each node is labeled from0
ton-1
. You are also given two nodessource
anddestination
. -
Word Ladder Transformation [Solution]
-
Word Ladder Transformation
Given two words,
beginWord
andendWord
, and a dictionary of wordswordList
, find the length of the shortest transformation sequence frombeginWord
toendWord
such that: -
Binary Tree Level Order Traversal [Solution]
-
Binary Tree Level Order Traversal
Given a binary tree, return the level order traversal of its nodes’ values. Level order traversal means traversing the tree level by level, from left to right.
-
Merge Sorted Arrays [Solution]
-
Merge Sorted Arrays
You are given an array of
k
sorted integer arrays, where each array is sorted in ascending order. Merge these arrays into a single sorted array. -
Merge K Sorted Linked Lists [Solution]
-
Merge K Sorted Linked Lists
Merge
k
sorted linked lists and return it as one sorted list. -
Shortest Path in Binary Matrix [Solution]
-
Shortest Path in Binary Matrix
Given a binary matrix representing an obstacle grid where
0
represents an empty cell and1
represents an obstacle, find the length of the shortest path from the top-left corner(0, 0)
to the bottom-right corner(m-1, n-1)
. If there is no path, return-1
. -
Rotate Array to the Right [Solution]
-
Rotate Array to the Right
Given an array, rotate the array to the right by
k
steps, wherek
is a non-negative integer. The function should modify the input array in-place. -
Find All Anagrams in a String [Solution]
-
Find All Anagrams in a String
Given a string
s
and a non-empty stringp
, find all the start indices ofp
’s anagrams ins
. -
Reverse Linked List [Solution]
-
Reverse Linked List
Given the head of a singly linked list, reverse the list without modifing the values of the nodes (modify only their pointers).
-
Longest Substring Without Repeating Characters [Solution]
-
Longest Substring Without Repeating Characters
Given a string, find the length of the longest substring without repeating characters.
-
Rearrange Array Elements [Solution]
-
Rearrange Array Elements
Given an array of positive integers, rearrange the elements such that the resulting number formed by concatenating the elements is the largest possible number.
-
Palindromic Substrings [Solution]
-
Palindromic Substrings
Given a string
s
, return the number of palindromic substrings in it. -
String Power [Solution]
-
String Power
Given a string
s
, the power of the string is the maximum length of a non-empty substring that contains only one unique character. -
Spiral Matrix [Solution]
-
Spiral Matrix
Given a matrix of
m x n
elements (m
rows,n
columns), return all elements of the matrix in spiral order. -
Integer Hamming Distance [Solution]
-
Integer Hamming Distance
The Hamming distance between two integers is the number of positions at which the corresponding bits are different. Given two integers
x
andy
, calculate the Hamming distance between them. -
Reverse Bits [Solution]
-
Reverse Bits
Given an unsigned integer, reverse its binary representation (i.e., reading it from right to left).
-
Maximum Product Subarray [Solution]
-
Maximum Product Subarray
Given an integer array, find the contiguous subarray within the array (containing at least one number) that has the largest product. Return the maximum product.
-
Maximum XOR Pair [Solution]
-
Maximum XOR Pair
Given an array of integers, find the maximum XOR value between any two elements in the array.
-
Randomized Shuffle [Solution]
-
Randomized Shuffle
Given an array of integers, implement a function to shuffle the array randomly. Each permutation of the array should be equally likely.
-
Linked List Cycle [Solution]
-
Linked List Cycle
Given a linked list, determine if it has a cycle in it. A cycle is defined as having at least one node in the list whose next is the node itself.
-
Subarray Sum Equals K [Solution]
-
Subarray Sum Equals K
Given an array of integers
nums
and an integerk
, return the total number of continuous subarrays whose sum equals tok
. -
Letter Combinations of a Phone Number [Solution]
-
Letter Combinations of a Phone Number
Given a string containing digits from
2
-9
inclusive, return all possible letter combinations that the number could represent. A mapping of digit to letters (just like on the telephone buttons) is given below. Note that1
does not map to any letters. -
Three Sum [Solution]
-
Three Sum
Given an array nums of
n
integers, find all unique triplets in the array which gives the sum of zero. -
Reverse Words [Solution]
-
Reverse Words
Given a string containing words separated by spaces, reverse the order of words in the string. For example, given the input string
"Hello World"
, the output should be"World Hello"
. -
Buy and Sell Stock [Solution]
-
Buy and Sell Stock
You are given an array
prices
whereprices[i]
is the price of a given stock on thei
th day. -
Find Peak Element [Solution]
-
Find Peak Element
A peak element in an array is an element that is greater than or equal to its neighbors. Given an input array
nums
wherenums[i] != nums[i+1]
, find a peak element and return its index. -
Anagram Pairs [Solution]
-
Anagram Pairs
Given an array of strings, find the number of pairs of strings that are anagrams of each other. Two strings are considered anagrams if they have the same characters, but in a different order.
-
Valid Palindrome [Solution]
-
Valid Palindrome
Given a string, determine if it is a palindrome. A palindrome is a word, phrase, number, or other sequence of characters that reads the same forward and backward, ignoring spaces, punctuation, and capitalization.