2022-01-30
189. Rotate Array
Topic: Array, Math, Two Pointers
Difficulty: Medium
Problem:
Given an array, rotate the array to the right by
Example 1:
Example 2:
Constraints:
•
•
•
Follow up:
• Try to come up with as many solutions as you can. There are at least three different ways to solve this problem.
• Could you do it in-place with
189. Rotate Array
Topic: Array, Math, Two Pointers
Difficulty: Medium
Problem:
Given an array, rotate the array to the right by
k steps, where k is non-negative.Example 1:
Input: nums = [1,2,3,4,5,6,7], k = 3
Output: [5,6,7,1,2,3,4]
Explanation:
rotate 1 steps to the right: [7,1,2,3,4,5,6]
rotate 2 steps to the right: [6,7,1,2,3,4,5]
rotate 3 steps to the right: [5,6,7,1,2,3,4]
Example 2:
Input: nums = [-1,-100,3,99], k = 2
Output: [3,99,-1,-100]
Explanation:
rotate 1 steps to the right: [99,-1,-100,3]
rotate 2 steps to the right: [3,99,-1,-100]
Constraints:
•
1 <= nums.length <= 10^5•
-2^31 <= nums[i] <= 2^31 - 1•
0 <= k <= 10^5Follow up:
• Try to come up with as many solutions as you can. There are at least three different ways to solve this problem.
• Could you do it in-place with
O(1) extra space?2022-01-31
1672. Richest Customer Wealth
Topic: Array, Matrix
Difficulty: Easy
Problem:
You are given an
A customer's wealth is the amount of money they have in all their bank accounts. The richest customer is the customer that has the maximum wealth.
Example 1:
Example 2:
Example 3:
Constraints:
•
•
•
•
1672. Richest Customer Wealth
Topic: Array, Matrix
Difficulty: Easy
Problem:
You are given an
m x n integer grid accounts where accounts[i][j] is the amount of money the i^th customer has in the j^th bank. Return the wealth that the richest customer has.A customer's wealth is the amount of money they have in all their bank accounts. The richest customer is the customer that has the maximum wealth.
Example 1:
Input: accounts = [[1,2,3],[3,2,1]]
Output: 6
Explanation:
1st customer has wealth = 1 + 2 + 3 = 6
2nd customer has wealth = 3 + 2 + 1 = 6
Both customers are considered the richest with a wealth of 6 each, so return 6.
Example 2:
Input: accounts = [[1,5],[7,3],[3,5]]
Output: 10
Explanation:
1st customer has wealth = 6
2nd customer has wealth = 10
3rd customer has wealth = 8
The 2nd customer is the richest with a wealth of 10.
Example 3:
Input: accounts = [[2,8,7],[7,1,3],[1,9,5]]
Output: 17
Constraints:
•
m == accounts.length•
n == accounts[i].length•
1 <= m, n <= 50•
1 <= accounts[i][j] <= 1002022-02-01
121. Best Time to Buy and Sell Stock
Topic: Array, Dynamic Programming
Difficulty: Easy
Problem:
You are given an array
You want to maximize your profit by choosing a single day to buy one stock and choosing a different day in the future to sell that stock.
Return the maximum profit you can achieve from this transaction. If you cannot achieve any profit, return
Example 1:
Example 2:
Constraints:
•
•
121. Best Time to Buy and Sell Stock
Topic: Array, Dynamic Programming
Difficulty: Easy
Problem:
You are given an array
prices where prices[i] is the price of a given stock on the i^th day.You want to maximize your profit by choosing a single day to buy one stock and choosing a different day in the future to sell that stock.
Return the maximum profit you can achieve from this transaction. If you cannot achieve any profit, return
0.Example 1:
Input: prices = [7,1,5,3,6,4]
Output: 5
Explanation: Buy on day 2 (price = 1) and sell on day 5 (price = 6), profit = 6-1 = 5.
Note that buying on day 2 and selling on day 1 is not allowed because you must buy before you sell.
Example 2:
Input: prices = [7,6,4,3,1]
Output: 0
Explanation: In this case, no transactions are done and the max profit = 0.
Constraints:
•
1 <= prices.length <= 10^5•
0 <= prices[i] <= 10^42022-02-02
438. Find All Anagrams in a String
Topic: Hash Table, String, Sliding Window
Difficulty: Medium
Problem:
Given two strings
An Anagram is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once.
Example 1:
Example 2:
Constraints:
•
•
438. Find All Anagrams in a String
Topic: Hash Table, String, Sliding Window
Difficulty: Medium
Problem:
Given two strings
s and p, return an array of all the start indices of p's anagrams in s. You may return the answer in any order.An Anagram is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once.
Example 1:
Input: s = "cbaebabacd", p = "abc"
Output: [0,6]
Explanation:
The substring with start index = 0 is "cba", which is an anagram of "abc".
The substring with start index = 6 is "bac", which is an anagram of "abc".
Example 2:
Input: s = "abab", p = "ab"
Output: [0,1,2]
Explanation:
The substring with start index = 0 is "ab", which is an anagram of "ab".
The substring with start index = 1 is "ba", which is an anagram of "ab".
The substring with start index = 2 is "ab", which is an anagram of "ab".
Constraints:
•
1 <= s.length, p.length <= 3 * 10^4•
s and p consist of lowercase English letters.2022-02-03
454. 4Sum II
Topic: Array, Hash Table
Difficulty: Medium
Problem:
Given four integer arrays
•
•
Example 1:
Example 2:
Constraints:
•
•
•
•
•
•
454. 4Sum II
Topic: Array, Hash Table
Difficulty: Medium
Problem:
Given four integer arrays
nums1, nums2, nums3, and nums4 all of length n, return the number of tuples (i, j, k, l) such that:•
0 <= i, j, k, l < n•
nums1[i] + nums2[j] + nums3[k] + nums4[l] == 0Example 1:
Input: nums1 = [1,2], nums2 = [-2,-1], nums3 = [-1,2], nums4 = [0,2]
Output: 2
Explanation:
The two tuples are:
1. (0, 0, 0, 1) -> nums1[0] + nums2[0] + nums3[0] + nums4[1] = 1 + (-2) + (-1) + 2 = 0
2. (1, 1, 0, 0) -> nums1[1] + nums2[1] + nums3[0] + nums4[0] = 2 + (-1) + (-1) + 0 = 0
Example 2:
Input: nums1 = [0], nums2 = [0], nums3 = [0], nums4 = [0]
Output: 1
Constraints:
•
n == nums1.length•
n == nums2.length•
n == nums3.length•
n == nums4.length•
1 <= n <= 200•
-2^28 <= nums1[i], nums2[i], nums3[i], nums4[i] <= 2^282022-02-04
525. Contiguous Array
Topic: Array, Hash Table, Prefix Sum
Difficulty: Medium
Problem:
Given a binary array
Example 1:
Example 2:
Constraints:
•
•
525. Contiguous Array
Topic: Array, Hash Table, Prefix Sum
Difficulty: Medium
Problem:
Given a binary array
nums, return the maximum length of a contiguous subarray with an equal number of 0 and 1.Example 1:
Input: nums = [0,1]
Output: 2
Explanation: [0, 1] is the longest contiguous subarray with an equal number of 0 and 1.
Example 2:
Input: nums = [0,1,0]
Output: 2
Explanation: [0, 1] (or [1, 0]) is a longest contiguous subarray with equal number of 0 and 1.
Constraints:
•
1 <= nums.length <= 10^5•
nums[i] is either 0 or 1.2022-02-05
23. Merge k Sorted Lists
Topic: Linked List, Divide and Conquer, Heap (Priority Queue), Merge Sort
Difficulty: Hard
Problem:
You are given an array of
Merge all the linked-lists into one sorted linked-list and return it.
Example 1:
Example 2:
Example 3:
Constraints:
•
•
•
•
•
• The sum of
23. Merge k Sorted Lists
Topic: Linked List, Divide and Conquer, Heap (Priority Queue), Merge Sort
Difficulty: Hard
Problem:
You are given an array of
k linked-lists lists, each linked-list is sorted in ascending order.Merge all the linked-lists into one sorted linked-list and return it.
Example 1:
Input: lists = [[1,4,5],[1,3,4],[2,6]]
Output: [1,1,2,3,4,4,5,6]
Explanation: The linked-lists are:
[
1->4->5,
1->3->4,
2->6
]
merging them into one sorted list:
1->1->2->3->4->4->5->6
Example 2:
Input: lists = []
Output: []
Example 3:
Input: lists = [[]]
Output: []
Constraints:
•
k == lists.length•
0 <= k <= 10^4•
0 <= lists[i].length <= 500•
-10^4 <= lists[i][j] <= 10^4•
lists[i] is sorted in ascending order.• The sum of
lists[i].length won't exceed 10^4.2022-02-06
80. Remove Duplicates from Sorted Array II
Topic: Array, Two Pointers
Difficulty: Medium
Problem:
Given an integer array
Since it is impossible to change the length of the array in some languages, you must instead have the result be placed in the first part of the array
Return
Do not allocate extra space for another array. You must do this by modifying the input array in-place with O(1) extra memory.
Custom Judge:
The judge will test your solution with the following code:
If all assertions pass, then your solution will be accepted.
Example 1:
Example 2:
Constraints:
•
•
•
80. Remove Duplicates from Sorted Array II
Topic: Array, Two Pointers
Difficulty: Medium
Problem:
Given an integer array
nums sorted in non-decreasing order, remove some duplicates in-place such that each unique element appears at most twice. The relative order of the elements should be kept the same.Since it is impossible to change the length of the array in some languages, you must instead have the result be placed in the first part of the array
nums. More formally, if there are k elements after removing the duplicates, then the first k elements of nums should hold the final result. It does not matter what you leave beyond the first k elements.Return
k after placing the final result in the first k slots of nums.Do not allocate extra space for another array. You must do this by modifying the input array in-place with O(1) extra memory.
Custom Judge:
The judge will test your solution with the following code:
int[] nums = [...]; // Input array
int[] expectedNums = [...]; // The expected answer with correct length
int k = removeDuplicates(nums); // Calls your implementation
assert k == expectedNums.length;
for (int i = 0; i < k; i++) {
assert nums[i] == expectedNums[i];
}
If all assertions pass, then your solution will be accepted.
Example 1:
Input: nums = [1,1,1,2,2,3]
Output: 5, nums = [1,1,2,2,3,\_]
Explanation: Your function should return k = 5, with the first five elements of nums being 1, 1, 2, 2 and 3 respectively.
It does not matter what you leave beyond the returned k (hence they are underscores).
Example 2:
Input: nums = [0,0,1,1,1,1,2,3,3]
Output: 7, nums = [0,0,1,1,2,3,3,\_,\_]
Explanation: Your function should return k = 7, with the first seven elements of nums being 0, 0, 1, 1, 2, 3 and 3 respectively.
It does not matter what you leave beyond the returned k (hence they are underscores).
Constraints:
•
1 <= nums.length <= 3 * 10^4•
-10^4 <= nums[i] <= 10^4•
nums is sorted in non-decreasing order.2022-02-07
389. Find the Difference
Topic: Hash Table, String, Bit Manipulation, Sorting
Difficulty: Easy
Problem:
You are given two strings
String
Return the letter that was added to
Example 1:
Example 2:
Constraints:
•
•
•
389. Find the Difference
Topic: Hash Table, String, Bit Manipulation, Sorting
Difficulty: Easy
Problem:
You are given two strings
s and t.String
t is generated by random shuffling string s and then add one more letter at a random position.Return the letter that was added to
t.Example 1:
Input: s = "abcd", t = "abcde"
Output: "e"
Explanation: 'e' is the letter that was added.
Example 2:
Input: s = "", t = "y"
Output: "y"
Constraints:
•
0 <= s.length <= 1000•
t.length == s.length + 1•
s and t consist of lowercase English letters.2022-02-08
258. Add Digits
Topic: Math, Simulation, Number Theory
Difficulty: Easy
Problem:
Given an integer
Example 1:
Example 2:
Constraints:
•
Follow up: Could you do it without any loop/recursion in
258. Add Digits
Topic: Math, Simulation, Number Theory
Difficulty: Easy
Problem:
Given an integer
num, repeatedly add all its digits until the result has only one digit, and return it.Example 1:
Input: num = 38
Output: 2
Explanation: The process is
38 --> 3 + 8 --> 11
11 --> 1 + 1 --> 2
Since 2 has only one digit, return it.
Example 2:
Input: num = 0
Output: 0
Constraints:
•
0 <= num <= 2^31 - 1Follow up: Could you do it without any loop/recursion in
O(1) runtime?2022-02-09
532. K-diff Pairs in an Array
Topic: Array, Hash Table, Two Pointers, Binary Search, Sorting
Difficulty: Medium
Problem:
Given an array of integers
A k-diff pair is an integer pair
•
•
Notice that
Example 1:
Example 2:
Example 3:
Constraints:
•
•
•
532. K-diff Pairs in an Array
Topic: Array, Hash Table, Two Pointers, Binary Search, Sorting
Difficulty: Medium
Problem:
Given an array of integers
nums and an integer k, return the number of unique k-diff pairs in the array.A k-diff pair is an integer pair
(nums[i], nums[j]), where the following are true:•
0 <= i < j < nums.length•
|nums[i] - nums[j]| == kNotice that
|val| denotes the absolute value of val.Example 1:
Input: nums = [3,1,4,1,5], k = 2
Output: 2
Explanation: There are two 2-diff pairs in the array, (1, 3) and (3, 5).
Although we have two 1s in the input, we should only return the number of unique pairs.
Example 2:
Input: nums = [1,2,3,4,5], k = 1
Output: 4
Explanation: There are four 1-diff pairs in the array, (1, 2), (2, 3), (3, 4) and (4, 5).
Example 3:
Input: nums = [1,3,1,5,4], k = 0
Output: 1
Explanation: There is one 0-diff pair in the array, (1, 1).
Constraints:
•
1 <= nums.length <= 10^4•
-10^7 <= nums[i] <= 10^7•
0 <= k <= 10^72022-02-10
560. Subarray Sum Equals K
Topic: Array, Hash Table, Prefix Sum
Difficulty: Medium
Problem:
Given an array of integers
Example 1:
Example 2:
Constraints:
•
•
•
560. Subarray Sum Equals K
Topic: Array, Hash Table, Prefix Sum
Difficulty: Medium
Problem:
Given an array of integers
nums and an integer k, return the total number of continuous subarrays whose sum equals to k.Example 1:
Input: nums = [1,1,1], k = 2
Output: 2
Example 2:
Input: nums = [1,2,3], k = 3
Output: 2
Constraints:
•
1 <= nums.length <= 2 * 10^4•
-1000 <= nums[i] <= 1000•
-10^7 <= k <= 10^72022-02-11
567. Permutation in String
Topic: Hash Table, Two Pointers, String, Sliding Window
Difficulty: Medium
Problem:
Given two strings
In other words, return
Example 1:
Example 2:
Constraints:
•
•
567. Permutation in String
Topic: Hash Table, Two Pointers, String, Sliding Window
Difficulty: Medium
Problem:
Given two strings
s1 and s2, return true if s2 contains a permutation of s1, or false otherwise.In other words, return
true if one of s1's permutations is the substring of s2.Example 1:
Input: s1 = "ab", s2 = "eidbaooo"
Output: true
Explanation: s2 contains one permutation of s1 ("ba").
Example 2:
Input: s1 = "ab", s2 = "eidboaoo"
Output: false
Constraints:
•
1 <= s1.length, s2.length <= 10^4•
s1 and s2 consist of lowercase English letters.2022-02-12
127. Word Ladder
Topic: Hash Table, String, Breadth-First Search
Difficulty: Hard
Problem:
A transformation sequence from word
• Every adjacent pair of words differs by a single letter.
• Every
•
Given two words,
Example 1:
Example 2:
Constraints:
•
•
•
•
•
•
• All the words in
127. Word Ladder
Topic: Hash Table, String, Breadth-First Search
Difficulty: Hard
Problem:
A transformation sequence from word
beginWord to word endWord using a dictionary wordList is a sequence of words beginWord -> s_1 -> s_2 -> ... -> s_k such that:• Every adjacent pair of words differs by a single letter.
• Every
s_i for 1 <= i <= k is in wordList. Note that beginWord does not need to be in wordList.•
s_k == endWordGiven two words,
beginWord and endWord, and a dictionary wordList, return the number of words in the shortest transformation sequence from beginWord to endWord, or 0 if no such sequence exists.Example 1:
Input: beginWord = "hit", endWord = "cog", wordList = ["hot","dot","dog","lot","log","cog"]
Output: 5
Explanation: One shortest transformation sequence is "hit" -> "hot" -> "dot" -> "dog" -> cog", which is 5 words long.
Example 2:
Input: beginWord = "hit", endWord = "cog", wordList = ["hot","dot","dog","lot","log"]
Output: 0
Explanation: The endWord "cog" is not in wordList, therefore there is no valid transformation sequence.
Constraints:
•
1 <= beginWord.length <= 10•
endWord.length == beginWord.length•
1 <= wordList.length <= 5000•
wordList[i].length == beginWord.length•
beginWord, endWord, and wordList[i] consist of lowercase English letters.•
beginWord != endWord• All the words in
wordList are unique.2022-02-13
78. Subsets
Topic: Array, Backtracking, Bit Manipulation
Difficulty: Medium
Problem:
Given an integer array
The solution set must not contain duplicate subsets. Return the solution in any order.
Example 1:
Example 2:
Constraints:
•
•
• All the numbers of
78. Subsets
Topic: Array, Backtracking, Bit Manipulation
Difficulty: Medium
Problem:
Given an integer array
nums of unique elements, return all possible subsets (the power set).The solution set must not contain duplicate subsets. Return the solution in any order.
Example 1:
Input: nums = [1,2,3]
Output: [[],[1],[2],[1,2],[3],[1,3],[2,3],[1,2,3]]
Example 2:
Input: nums = [0]
Output: [[],[0]]
Constraints:
•
1 <= nums.length <= 10•
-10 <= nums[i] <= 10• All the numbers of
nums are unique.2022-02-14
104. Maximum Depth of Binary Tree
Topic: Tree, Depth-First Search, Breadth-First Search, Binary Tree
Difficulty: Easy
Problem:
Given the
A binary tree's maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.
Example 1:
Image: https://assets.leetcode.com/uploads/2020/11/26/tmp-tree.jpg
Example 2:
Constraints:
• The number of nodes in the tree is in the range
•
104. Maximum Depth of Binary Tree
Topic: Tree, Depth-First Search, Breadth-First Search, Binary Tree
Difficulty: Easy
Problem:
Given the
root of a binary tree, return its maximum depth.A binary tree's maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.
Example 1:
Image: https://assets.leetcode.com/uploads/2020/11/26/tmp-tree.jpg
Input: root = [3,9,20,null,null,15,7]
Output: 3
Example 2:
Input: root = [1,null,2]
Output: 2
Constraints:
• The number of nodes in the tree is in the range
[0, 10^4].•
-100 <= Node.val <= 1002022-02-15
136. Single Number
Topic: Array, Bit Manipulation
Difficulty: Easy
Problem:
Given a non-empty array of integers
You must implement a solution with a linear runtime complexity and use only constant extra space.
Example 1:
Example 2:
Example 3:
Constraints:
•
•
• Each element in the array appears twice except for one element which appears only once.
136. Single Number
Topic: Array, Bit Manipulation
Difficulty: Easy
Problem:
Given a non-empty array of integers
nums, every element appears twice except for one. Find that single one.You must implement a solution with a linear runtime complexity and use only constant extra space.
Example 1:
Input: nums = [2,2,1]
Output: 1
Example 2:
Input: nums = [4,1,2,1,2]
Output: 4
Example 3:
Input: nums = [1]
Output: 1
Constraints:
•
1 <= nums.length <= 3 * 10^4•
-3 * 10^4 <= nums[i] <= 3 * 10^4• Each element in the array appears twice except for one element which appears only once.
2022-02-16
24. Swap Nodes in Pairs
Topic: Linked List, Recursion
Difficulty: Medium
Problem:
Given a linked list, swap every two adjacent nodes and return its head. You must solve the problem without modifying the values in the list's nodes (i.e., only nodes themselves may be changed.)
Example 1:
Image: https://assets.leetcode.com/uploads/2020/10/03/swap_ex1.jpg
Example 2:
Example 3:
Constraints:
• The number of nodes in the list is in the range
•
24. Swap Nodes in Pairs
Topic: Linked List, Recursion
Difficulty: Medium
Problem:
Given a linked list, swap every two adjacent nodes and return its head. You must solve the problem without modifying the values in the list's nodes (i.e., only nodes themselves may be changed.)
Example 1:
Image: https://assets.leetcode.com/uploads/2020/10/03/swap_ex1.jpg
Input: head = [1,2,3,4]
Output: [2,1,4,3]
Example 2:
Input: head = []
Output: []
Example 3:
Input: head = [1]
Output: [1]
Constraints:
• The number of nodes in the list is in the range
[0, 100].•
0 <= Node.val <= 1002022-02-17
39. Combination Sum
Topic: Array, Backtracking
Difficulty: Medium
Problem:
Given an array of distinct integers
The same number may be chosen from
It is guaranteed that the number of unique combinations that sum up to
Example 1:
Example 2:
Example 3:
Constraints:
•
•
• All elements of
•
39. Combination Sum
Topic: Array, Backtracking
Difficulty: Medium
Problem:
Given an array of distinct integers
candidates and a target integer target, return a list of all unique combinations of candidates where the chosen numbers sum to target. You may return the combinations in any order.The same number may be chosen from
candidates an unlimited number of times. Two combinations are unique if the frequency of at least one of the chosen numbers is different.It is guaranteed that the number of unique combinations that sum up to
target is less than 150 combinations for the given input.Example 1:
Input: candidates = [2,3,6,7], target = 7
Output: [[2,2,3],[7]]
Explanation:
2 and 3 are candidates, and 2 + 2 + 3 = 7. Note that 2 can be used multiple times.
7 is a candidate, and 7 = 7.
These are the only two combinations.
Example 2:
Input: candidates = [2,3,5], target = 8
Output: [[2,2,2,2],[2,3,3],[3,5]]
Example 3:
Input: candidates = [2], target = 1
Output: []
Constraints:
•
1 <= candidates.length <= 30•
1 <= candidates[i] <= 200• All elements of
candidates are distinct.•
1 <= target <= 5002022-02-18
402. Remove K Digits
Topic: String, Stack, Greedy, Monotonic Stack
Difficulty: Medium
Problem:
Given string num representing a non-negative integer
Example 1:
Example 2:
Example 3:
Constraints:
•
•
•
402. Remove K Digits
Topic: String, Stack, Greedy, Monotonic Stack
Difficulty: Medium
Problem:
Given string num representing a non-negative integer
num, and an integer k, return the smallest possible integer after removing k digits from num.Example 1:
Input: num = "1432219", k = 3
Output: "1219"
Explanation: Remove the three digits 4, 3, and 2 to form the new number 1219 which is the smallest.
Example 2:
Input: num = "10200", k = 1
Output: "200"
Explanation: Remove the leading 1 and the number is 200. Note that the output must not contain leading zeroes.
Example 3:
Input: num = "10", k = 2
Output: "0"
Explanation: Remove all the digits from the number and it is left with nothing which is 0.
Constraints:
•
1 <= k <= num.length <= 10^5•
num consists of only digits.•
num does not have any leading zeros except for the zero itself.