2022-10-30
1293. Shortest Path in a Grid with Obstacles Elimination
Topic: Array, Breadth-First Search, Matrix
Difficulty: Hard
Problem:
You are given an
Return the minimum number of steps to walk from the upper left corner
Example 1:
Image: https://assets.leetcode.com/uploads/2021/09/30/short1-grid.jpg
Example 2:
Image: https://assets.leetcode.com/uploads/2021/09/30/short2-grid.jpg
Constraints:
•
•
•
•
•
•
1293. Shortest Path in a Grid with Obstacles Elimination
Topic: Array, Breadth-First Search, Matrix
Difficulty: Hard
Problem:
You are given an
m x n integer matrix grid where each cell is either 0 (empty) or 1 (obstacle). You can move up, down, left, or right from and to an empty cell in one step.Return the minimum number of steps to walk from the upper left corner
(0, 0) to the lower right corner (m - 1, n - 1) given that you can eliminate at most k obstacles. If it is not possible to find such walk return -1.Example 1:
Image: https://assets.leetcode.com/uploads/2021/09/30/short1-grid.jpg
Input: grid = [[0,0,0],[1,1,0],[0,0,0],[0,1,1],[0,0,0]], k = 1
Output: 6
Explanation:
The shortest path without eliminating any obstacle is 10.
The shortest path with one obstacle elimination at position (3,2) is 6. Such path is (0,0) -> (0,1) -> (0,2) -> (1,2) -> (2,2) -> (3,2) -> (4,2).
Example 2:
Image: https://assets.leetcode.com/uploads/2021/09/30/short2-grid.jpg
Input: grid = [[0,1,1],[1,1,1],[1,0,0]], k = 1
Output: -1
Explanation: We need to eliminate at least two obstacles to find such a walk.
Constraints:
•
m == grid.length•
n == grid[i].length•
1 <= m, n <= 40•
1 <= k <= m * n•
grid[i][j] is either 0 or 1.•
grid[0][0] == grid[m - 1][n - 1] == 02022-10-31
766. Toeplitz Matrix
Topic: Array, Matrix
Difficulty: Easy
Problem:
Given an
A matrix is Toeplitz if every diagonal from top-left to bottom-right has the same elements.
Example 1:
Image: https://assets.leetcode.com/uploads/2020/11/04/ex1.jpg
Example 2:
Image: https://assets.leetcode.com/uploads/2020/11/04/ex2.jpg
Constraints:
•
•
•
•
Follow up:
• What if the
• What if the
766. Toeplitz Matrix
Topic: Array, Matrix
Difficulty: Easy
Problem:
Given an
m x n matrix, return true if the matrix is Toeplitz. Otherwise, return false.A matrix is Toeplitz if every diagonal from top-left to bottom-right has the same elements.
Example 1:
Image: https://assets.leetcode.com/uploads/2020/11/04/ex1.jpg
Input: matrix = [[1,2,3,4],[5,1,2,3],[9,5,1,2]]
Output: true
Explanation:
In the above grid, the diagonals are:
"[9]", "[5, 5]", "[1, 1, 1]", "[2, 2, 2]", "[3, 3]", "[4]".
In each diagonal all elements are the same, so the answer is True.
Example 2:
Image: https://assets.leetcode.com/uploads/2020/11/04/ex2.jpg
Input: matrix = [[1,2],[2,2]]
Output: false
Explanation:
The diagonal "[1, 2]" has different elements.
Constraints:
•
m == matrix.length•
n == matrix[i].length•
1 <= m, n <= 20•
0 <= matrix[i][j] <= 99Follow up:
• What if the
matrix is stored on disk, and the memory is limited such that you can only load at most one row of the matrix into the memory at once?• What if the
matrix is so large that you can only load up a partial row into the memory at once?2022-11-01
1706. Where Will the Ball Fall
Topic: Array, Dynamic Programming, Depth-First Search, Matrix, Simulation
Difficulty: Medium
Problem:
You have a 2-D
Each cell in the box has a diagonal board spanning two corners of the cell that can redirect a ball to the right or to the left.
• A board that redirects the ball to the right spans the top-left corner to the bottom-right corner and is represented in the grid as
• A board that redirects the ball to the left spans the top-right corner to the bottom-left corner and is represented in the grid as
We drop one ball at the top of each column of the box. Each ball can get stuck in the box or fall out of the bottom. A ball gets stuck if it hits a "V" shaped pattern between two boards or if a board redirects the ball into either wall of the box.
Return an array
Example 1:
Image: https://assets.leetcode.com/uploads/2019/09/26/ball.jpg
Example 2:
Example 3:
Constraints:
•
•
•
•
1706. Where Will the Ball Fall
Topic: Array, Dynamic Programming, Depth-First Search, Matrix, Simulation
Difficulty: Medium
Problem:
You have a 2-D
grid of size m x n representing a box, and you have n balls. The box is open on the top and bottom sides.Each cell in the box has a diagonal board spanning two corners of the cell that can redirect a ball to the right or to the left.
• A board that redirects the ball to the right spans the top-left corner to the bottom-right corner and is represented in the grid as
1.• A board that redirects the ball to the left spans the top-right corner to the bottom-left corner and is represented in the grid as
-1.We drop one ball at the top of each column of the box. Each ball can get stuck in the box or fall out of the bottom. A ball gets stuck if it hits a "V" shaped pattern between two boards or if a board redirects the ball into either wall of the box.
Return an array
answer of size n where answer[i] is the column that the ball falls out of at the bottom after dropping the ball from the i^th column at the top, or -1 if the ball gets stuck in the box.Example 1:
Image: https://assets.leetcode.com/uploads/2019/09/26/ball.jpg
Input: grid = [[1,1,1,-1,-1],[1,1,1,-1,-1],[-1,-1,-1,1,1],[1,1,1,1,-1],[-1,-1,-1,-1,-1]]
Output: [1,-1,-1,-1,-1]
Explanation: This example is shown in the photo.
Ball b0 is dropped at column 0 and falls out of the box at column 1.
Ball b1 is dropped at column 1 and will get stuck in the box between column 2 and 3 and row 1.
Ball b2 is dropped at column 2 and will get stuck on the box between column 2 and 3 and row 0.
Ball b3 is dropped at column 3 and will get stuck on the box between column 2 and 3 and row 0.
Ball b4 is dropped at column 4 and will get stuck on the box between column 2 and 3 and row 1.
Example 2:
Input: grid = [[-1]]
Output: [-1]
Explanation: The ball gets stuck against the left wall.
Example 3:
Input: grid = [[1,1,1,1,1,1],[-1,-1,-1,-1,-1,-1],[1,1,1,1,1,1],[-1,-1,-1,-1,-1,-1]]
Output: [0,1,2,3,4,-1]
Constraints:
•
m == grid.length•
n == grid[i].length•
1 <= m, n <= 100•
grid[i][j] is 1 or -1.2022-11-02
433. Minimum Genetic Mutation
Topic: Hash Table, String, Breadth-First Search
Difficulty: Medium
Problem:
A gene string can be represented by an 8-character long string, with choices from
Suppose we need to investigate a mutation from a gene string
• For example,
There is also a gene bank
Given the two gene strings
Note that the starting point is assumed to be valid, so it might not be included in the bank.
Example 1:
Example 2:
Example 3:
Constraints:
•
•
•
•
•
433. Minimum Genetic Mutation
Topic: Hash Table, String, Breadth-First Search
Difficulty: Medium
Problem:
A gene string can be represented by an 8-character long string, with choices from
'A', 'C', 'G', and 'T'.Suppose we need to investigate a mutation from a gene string
start to a gene string end where one mutation is defined as one single character changed in the gene string.• For example,
"AACCGGTT" --> "AACCGGTA" is one mutation.There is also a gene bank
bank that records all the valid gene mutations. A gene must be in bank to make it a valid gene string.Given the two gene strings
start and end and the gene bank bank, return the minimum number of mutations needed to mutate from start to end. If there is no such a mutation, return -1.Note that the starting point is assumed to be valid, so it might not be included in the bank.
Example 1:
Input: start = "AACCGGTT", end = "AACCGGTA", bank = ["AACCGGTA"]
Output: 1
Example 2:
Input: start = "AACCGGTT", end = "AAACGGTA", bank = ["AACCGGTA","AACCGCTA","AAACGGTA"]
Output: 2
Example 3:
Input: start = "AAAAACCC", end = "AACCCCCC", bank = ["AAAACCCC","AAACCCCC","AACCCCCC"]
Output: 3
Constraints:
•
start.length == 8•
end.length == 8•
0 <= bank.length <= 10•
bank[i].length == 8•
start, end, and bank[i] consist of only the characters ['A', 'C', 'G', 'T'].2022-11-03
2131. Longest Palindrome by Concatenating Two Letter Words
Topic: Array, Hash Table, String, Greedy, Counting
Difficulty: Medium
Problem:
You are given an array of strings
Create the longest possible palindrome by selecting some elements from
Return the length of the longest palindrome that you can create. If it is impossible to create any palindrome, return
A palindrome is a string that reads the same forward and backward.
Example 1:
Example 2:
Example 3:
Constraints:
•
•
•
2131. Longest Palindrome by Concatenating Two Letter Words
Topic: Array, Hash Table, String, Greedy, Counting
Difficulty: Medium
Problem:
You are given an array of strings
words. Each element of words consists of two lowercase English letters.Create the longest possible palindrome by selecting some elements from
words and concatenating them in any order. Each element can be selected at most once.Return the length of the longest palindrome that you can create. If it is impossible to create any palindrome, return
0.A palindrome is a string that reads the same forward and backward.
Example 1:
Input: words = ["lc","cl","gg"]
Output: 6
Explanation: One longest palindrome is "lc" + "gg" + "cl" = "lcggcl", of length 6.
Note that "clgglc" is another longest palindrome that can be created.
Example 2:
Input: words = ["ab","ty","yt","lc","cl","ab"]
Output: 8
Explanation: One longest palindrome is "ty" + "lc" + "cl" + "yt" = "tylcclyt", of length 8.
Note that "lcyttycl" is another longest palindrome that can be created.
Example 3:
Input: words = ["cc","ll","xx"]
Output: 2
Explanation: One longest palindrome is "cc", of length 2.
Note that "ll" is another longest palindrome that can be created, and so is "xx".
Constraints:
•
1 <= words.length <= 10^5•
words[i].length == 2•
words[i] consists of lowercase English letters.2022-11-04
345. Reverse Vowels of a String
Topic: Two Pointers, String
Difficulty: Easy
Problem:
Given a string
The vowels are
Example 1:
Example 2:
Constraints:
•
•
345. Reverse Vowels of a String
Topic: Two Pointers, String
Difficulty: Easy
Problem:
Given a string
s, reverse only all the vowels in the string and return it.The vowels are
'a', 'e', 'i', 'o', and 'u', and they can appear in both lower and upper cases, more than once.Example 1:
Input: s = "hello"
Output: "holle"
Example 2:
Input: s = "leetcode"
Output: "leotcede"
Constraints:
•
1 <= s.length <= 3 * 10^5•
s consist of printable ASCII characters.2022-11-05
212. Word Search II
Topic: Array, String, Backtracking, Trie, Matrix
Difficulty: Hard
Problem:
Given an
Each word must be constructed from letters of sequentially adjacent cells, where adjacent cells are horizontally or vertically neighboring. The same letter cell may not be used more than once in a word.
Example 1:
Image: https://assets.leetcode.com/uploads/2020/11/07/search1.jpg
Example 2:
Image: https://assets.leetcode.com/uploads/2020/11/07/search2.jpg
Constraints:
•
•
•
•
•
•
•
• All the strings of
212. Word Search II
Topic: Array, String, Backtracking, Trie, Matrix
Difficulty: Hard
Problem:
Given an
m x n board of characters and a list of strings words, return all words on the board.Each word must be constructed from letters of sequentially adjacent cells, where adjacent cells are horizontally or vertically neighboring. The same letter cell may not be used more than once in a word.
Example 1:
Image: https://assets.leetcode.com/uploads/2020/11/07/search1.jpg
Input: board = [["o","a","a","n"],["e","t","a","e"],["i","h","k","r"],["i","f","l","v"]], words = ["oath","pea","eat","rain"]
Output: ["eat","oath"]
Example 2:
Image: https://assets.leetcode.com/uploads/2020/11/07/search2.jpg
Input: board = [["a","b"],["c","d"]], words = ["abcb"]
Output: []
Constraints:
•
m == board.length•
n == board[i].length•
1 <= m, n <= 12•
board[i][j] is a lowercase English letter.•
1 <= words.length <= 3 * 10^4•
1 <= words[i].length <= 10•
words[i] consists of lowercase English letters.• All the strings of
words are unique.2022-11-06
899. Orderly Queue
Topic: Math, String, Sorting
Difficulty: Hard
Problem:
You are given a string
Return the lexicographically smallest string you could have after applying the mentioned step any number of moves.
Example 1:
Example 2:
Constraints:
•
•
899. Orderly Queue
Topic: Math, String, Sorting
Difficulty: Hard
Problem:
You are given a string
s and an integer k. You can choose one of the first k letters of s and append it at the end of the string..Return the lexicographically smallest string you could have after applying the mentioned step any number of moves.
Example 1:
Input: s = "cba", k = 1
Output: "acb"
Explanation:
In the first move, we move the 1^st character 'c' to the end, obtaining the string "bac".
In the second move, we move the 1^st character 'b' to the end, obtaining the final result "acb".
Example 2:
Input: s = "baaca", k = 3
Output: "aaabc"
Explanation:
In the first move, we move the 1^st character 'b' to the end, obtaining the string "aacab".
In the second move, we move the 3^rd character 'c' to the end, obtaining the final result "aaabc".
Constraints:
•
1 <= k <= s.length <= 1000•
s consist of lowercase English letters.2022-11-07
1323. Maximum 69 Number
Topic: Math, Greedy
Difficulty: Easy
Problem:
You are given a positive integer
Return the maximum number you can get by changing at most one digit (
Example 1:
Example 2:
Example 3:
Constraints:
•
•
1323. Maximum 69 Number
Topic: Math, Greedy
Difficulty: Easy
Problem:
You are given a positive integer
num consisting only of digits 6 and 9.Return the maximum number you can get by changing at most one digit (
6 becomes 9, and 9 becomes 6).Example 1:
Input: num = 9669
Output: 9969
Explanation:
Changing the first digit results in 6669.
Changing the second digit results in 9969.
Changing the third digit results in 9699.
Changing the fourth digit results in 9666.
The maximum number is 9969.
Example 2:
Input: num = 9996
Output: 9999
Explanation: Changing the last digit 6 to 9 results in the maximum number.
Example 3:
Input: num = 9999
Output: 9999
Explanation: It is better not to apply any change.
Constraints:
•
1 <= num <= 10^4•
num consists of only 6 and 9 digits.2022-11-08
1544. Make The String Great
Topic: String, Stack
Difficulty: Easy
Problem:
Given a string
A good string is a string which doesn't have two adjacent characters
•
•
To make the string good, you can choose two adjacent characters that make the string bad and remove them. You can keep doing this until the string becomes good.
Return the string after making it good. The answer is guaranteed to be unique under the given constraints.
Notice that an empty string is also good.
Example 1:
Example 2:
Example 3:
Constraints:
•
•
1544. Make The String Great
Topic: String, Stack
Difficulty: Easy
Problem:
Given a string
s of lower and upper case English letters.A good string is a string which doesn't have two adjacent characters
s[i] and s[i + 1] where:•
0 <= i <= s.length - 2•
s[i] is a lower-case letter and s[i + 1] is the same letter but in upper-case or vice-versa.To make the string good, you can choose two adjacent characters that make the string bad and remove them. You can keep doing this until the string becomes good.
Return the string after making it good. The answer is guaranteed to be unique under the given constraints.
Notice that an empty string is also good.
Example 1:
Input: s = "leEeetcode"
Output: "leetcode"
Explanation: In the first step, either you choose i = 1 or i = 2, both will result "leEeetcode" to be reduced to "leetcode".
Example 2:
Input: s = "abBAcC"
Output: ""
Explanation: We have many possible scenarios, and all lead to the same answer. For example:
"abBAcC" --> "aAcC" --> "cC" --> ""
"abBAcC" --> "abBA" --> "aA" --> ""
Example 3:
Input: s = "s"
Output: "s"
Constraints:
•
1 <= s.length <= 100•
s contains only lower and upper case English letters.2022-11-09
901. Online Stock Span
Topic: Stack, Design, Monotonic Stack, Data Stream
Difficulty: Medium
Problem:
Design an algorithm that collects daily price quotes for some stock and returns the span of that stock's price for the current day.
The span of the stock's price today is defined as the maximum number of consecutive days (starting from today and going backward) for which the stock price was less than or equal to today's price.
• For example, if the price of a stock over the next
Implement the
•
•
Example 1:
Constraints:
•
• At most
901. Online Stock Span
Topic: Stack, Design, Monotonic Stack, Data Stream
Difficulty: Medium
Problem:
Design an algorithm that collects daily price quotes for some stock and returns the span of that stock's price for the current day.
The span of the stock's price today is defined as the maximum number of consecutive days (starting from today and going backward) for which the stock price was less than or equal to today's price.
• For example, if the price of a stock over the next
7 days were [100,80,60,70,60,75,85], then the stock spans would be [1,1,1,2,1,4,6].Implement the
StockSpanner class:•
StockSpanner() Initializes the object of the class.•
int next(int price) Returns the span of the stock's price given that today's price is price.Example 1:
Input
["StockSpanner", "next", "next", "next", "next", "next", "next", "next"]
[[], [100], [80], [60], [70], [60], [75], [85]]
Output
[null, 1, 1, 1, 2, 1, 4, 6]
Explanation
StockSpanner stockSpanner = new StockSpanner();
stockSpanner.next(100); // return 1
stockSpanner.next(80); // return 1
stockSpanner.next(60); // return 1
stockSpanner.next(70); // return 2
stockSpanner.next(60); // return 1
stockSpanner.next(75); // return 4, because the last 4 prices (including today's price of 75) were less than or equal to today's price.
stockSpanner.next(85); // return 6
Constraints:
•
1 <= price <= 10^5• At most
10^4 calls will be made to next.2022-11-10
1047. Remove All Adjacent Duplicates In String
Topic: String, Stack
Difficulty: Easy
Problem:
You are given a string
We repeatedly make duplicate removals on
Return the final string after all such duplicate removals have been made. It can be proven that the answer is unique.
Example 1:
Example 2:
Constraints:
•
•
1047. Remove All Adjacent Duplicates In String
Topic: String, Stack
Difficulty: Easy
Problem:
You are given a string
s consisting of lowercase English letters. A duplicate removal consists of choosing two adjacent and equal letters and removing them.We repeatedly make duplicate removals on
s until we no longer can.Return the final string after all such duplicate removals have been made. It can be proven that the answer is unique.
Example 1:
Input: s = "abbaca"
Output: "ca"
Explanation:
For example, in "abbaca" we could remove "bb" since the letters are adjacent and equal, and this is the only possible move. The result of this move is that the string is "aaca", of which only "aa" is possible, so the final string is "ca".
Example 2:
Input: s = "azxxzy"
Output: "ay"
Constraints:
•
1 <= s.length <= 10^5•
s consists of lowercase English letters.2022-11-11
26. Remove Duplicates from Sorted Array
Topic: Array, Two Pointers
Difficulty: Easy
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:
•
•
•
26. Remove Duplicates from Sorted Array
Topic: Array, Two Pointers
Difficulty: Easy
Problem:
Given an integer array
nums sorted in non-decreasing order, remove the duplicates in-place such that each unique element appears only once. 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,2]
Output: 2, nums = [1,2,_]
Explanation: Your function should return k = 2, with the first two elements of nums being 1 and 2 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,2,2,3,3,4]
Output: 5, nums = [0,1,2,3,4,_,_,_,_,_]
Explanation: Your function should return k = 5, with the first five elements of nums being 0, 1, 2, 3, and 4 respectively.
It does not matter what you leave beyond the returned k (hence they are underscores).
Constraints:
•
1 <= nums.length <= 3 * 10^4•
-100 <= nums[i] <= 100•
nums is sorted in non-decreasing order.2022-11-12
295. Find Median from Data Stream
Topic: Two Pointers, Design, Sorting, Heap (Priority Queue), Data Stream
Difficulty: Hard
Problem:
The median is the middle value in an ordered integer list. If the size of the list is even, there is no middle value, and the median is the mean of the two middle values.
• For example, for
• For example, for
Implement the MedianFinder class:
•
•
•
Example 1:
Constraints:
•
• There will be at least one element in the data structure before calling
• At most
Follow up:
• If all integer numbers from the stream are in the range
• If
295. Find Median from Data Stream
Topic: Two Pointers, Design, Sorting, Heap (Priority Queue), Data Stream
Difficulty: Hard
Problem:
The median is the middle value in an ordered integer list. If the size of the list is even, there is no middle value, and the median is the mean of the two middle values.
• For example, for
arr = [2,3,4], the median is 3.• For example, for
arr = [2,3], the median is (2 + 3) / 2 = 2.5.Implement the MedianFinder class:
•
MedianFinder() initializes the MedianFinder object.•
void addNum(int num) adds the integer num from the data stream to the data structure.•
double findMedian() returns the median of all elements so far. Answers within 10^-5 of the actual answer will be accepted.Example 1:
Input
["MedianFinder", "addNum", "addNum", "findMedian", "addNum", "findMedian"]
[[], [1], [2], [], [3], []]
Output
[null, null, null, 1.5, null, 2.0]
Explanation
MedianFinder medianFinder = new MedianFinder();
medianFinder.addNum(1); // arr = [1]
medianFinder.addNum(2); // arr = [1, 2]
medianFinder.findMedian(); // return 1.5 (i.e., (1 + 2) / 2)
medianFinder.addNum(3); // arr[1, 2, 3]
medianFinder.findMedian(); // return 2.0
Constraints:
•
-10^5 <= num <= 10^5• There will be at least one element in the data structure before calling
findMedian.• At most
5 * 10^4 calls will be made to addNum and findMedian.Follow up:
• If all integer numbers from the stream are in the range
[0, 100], how would you optimize your solution?• If
99% of all integer numbers from the stream are in the range [0, 100], how would you optimize your solution?2022-11-13
151. Reverse Words in a String
Topic: Two Pointers, String
Difficulty: Medium
Problem:
Given an input string
A word is defined as a sequence of non-space characters. The words in
Return a string of the words in reverse order concatenated by a single space.
Note that
Example 1:
Example 2:
Example 3:
Constraints:
•
•
• There is at least one word in
Follow-up: If the string data type is mutable in your language, can you solve it in-place with
151. Reverse Words in a String
Topic: Two Pointers, String
Difficulty: Medium
Problem:
Given an input string
s, reverse the order of the words.A word is defined as a sequence of non-space characters. The words in
s will be separated by at least one space.Return a string of the words in reverse order concatenated by a single space.
Note that
s may contain leading or trailing spaces or multiple spaces between two words. The returned string should only have a single space separating the words. Do not include any extra spaces.Example 1:
Input: s = "the sky is blue"
Output: "blue is sky the"
Example 2:
Input: s = " hello world "
Output: "world hello"
Explanation: Your reversed string should not contain leading or trailing spaces.
Example 3:
Input: s = "a good example"
Output: "example good a"
Explanation: You need to reduce multiple spaces between two words to a single space in the reversed string.
Constraints:
•
1 <= s.length <= 10^4•
s contains English letters (upper-case and lower-case), digits, and spaces ' '.• There is at least one word in
s.Follow-up: If the string data type is mutable in your language, can you solve it in-place with
O(1) extra space?2022-11-14
947. Most Stones Removed with Same Row or Column
Topic: Depth-First Search, Union Find, Graph
Difficulty: Medium
Problem:
On a 2D plane, we place
A stone can be removed if it shares either the same row or the same column as another stone that has not been removed.
Given an array
Example 1:
Example 2:
Example 3:
Constraints:
•
•
• No two stones are at the same coordinate point.
947. Most Stones Removed with Same Row or Column
Topic: Depth-First Search, Union Find, Graph
Difficulty: Medium
Problem:
On a 2D plane, we place
n stones at some integer coordinate points. Each coordinate point may have at most one stone.A stone can be removed if it shares either the same row or the same column as another stone that has not been removed.
Given an array
stones of length n where stones[i] = [x_i, y_i] represents the location of the i^th stone, return the largest possible number of stones that can be removed.Example 1:
Input: stones = [[0,0],[0,1],[1,0],[1,2],[2,1],[2,2]]
Output: 5
Explanation: One way to remove 5 stones is as follows:
1. Remove stone [2,2] because it shares the same row as [2,1].
2. Remove stone [2,1] because it shares the same column as [0,1].
3. Remove stone [1,2] because it shares the same row as [1,0].
4. Remove stone [1,0] because it shares the same column as [0,0].
5. Remove stone [0,1] because it shares the same row as [0,0].
Stone [0,0] cannot be removed since it does not share a row/column with another stone still on the plane.
Example 2:
Input: stones = [[0,0],[0,2],[1,1],[2,0],[2,2]]
Output: 3
Explanation: One way to make 3 moves is as follows:
1. Remove stone [2,2] because it shares the same row as [2,0].
2. Remove stone [2,0] because it shares the same column as [0,0].
3. Remove stone [0,2] because it shares the same row as [0,0].
Stones [0,0] and [1,1] cannot be removed since they do not share a row/column with another stone still on the plane.
Example 3:
Input: stones = [[0,0]]
Output: 0
Explanation: [0,0] is the only stone on the plane, so you cannot remove it.
Constraints:
•
1 <= stones.length <= 1000•
0 <= x_i, y_i <= 10^4• No two stones are at the same coordinate point.
2022-11-15
222. Count Complete Tree Nodes
Topic: Binary Search, Tree, Depth-First Search, Binary Tree
Difficulty: Medium
Problem:
Given the
According to Wikipedia, every level, except possibly the last, is completely filled in a complete binary tree, and all nodes in the last level are as far left as possible. It can have between
Design an algorithm that runs in less than
Example 1:
Image: https://assets.leetcode.com/uploads/2021/01/14/complete.jpg
Example 2:
Example 3:
Constraints:
• The number of nodes in the tree is in the range
•
• The tree is guaranteed to be complete.
222. Count Complete Tree Nodes
Topic: Binary Search, Tree, Depth-First Search, Binary Tree
Difficulty: Medium
Problem:
Given the
root of a complete binary tree, return the number of the nodes in the tree.According to Wikipedia, every level, except possibly the last, is completely filled in a complete binary tree, and all nodes in the last level are as far left as possible. It can have between
1 and 2^h nodes inclusive at the last level h.Design an algorithm that runs in less than
O(n) time complexity.Example 1:
Image: https://assets.leetcode.com/uploads/2021/01/14/complete.jpg
Input: root = [1,2,3,4,5,6]
Output: 6
Example 2:
Input: root = []
Output: 0
Example 3:
Input: root = [1]
Output: 1
Constraints:
• The number of nodes in the tree is in the range
[0, 5 * 10^4].•
0 <= Node.val <= 5 * 10^4• The tree is guaranteed to be complete.
2022-11-16
374. Guess Number Higher or Lower
Topic: Binary Search, Interactive
Difficulty: Easy
Problem:
We are playing the Guess Game. The game is as follows:
I pick a number from
Every time you guess wrong, I will tell you whether the number I picked is higher or lower than your guess.
You call a pre-defined API
•
•
•
Return the number that I picked.
Example 1:
Example 2:
Example 3:
Constraints:
•
•
374. Guess Number Higher or Lower
Topic: Binary Search, Interactive
Difficulty: Easy
Problem:
We are playing the Guess Game. The game is as follows:
I pick a number from
1 to n. You have to guess which number I picked.Every time you guess wrong, I will tell you whether the number I picked is higher or lower than your guess.
You call a pre-defined API
int guess(int num), which returns three possible results:•
-1: Your guess is higher than the number I picked (i.e. num > pick).•
1: Your guess is lower than the number I picked (i.e. num < pick).•
0: your guess is equal to the number I picked (i.e. num == pick).Return the number that I picked.
Example 1:
Input: n = 10, pick = 6
Output: 6
Example 2:
Input: n = 1, pick = 1
Output: 1
Example 3:
Input: n = 2, pick = 1
Output: 1
Constraints:
•
1 <= n <= 2^31 - 1•
1 <= pick <= n2022-11-17
223. Rectangle Area
Topic: Math, Geometry
Difficulty: Medium
Problem:
Given the coordinates of two rectilinear rectangles in a 2D plane, return the total area covered by the two rectangles.
The first rectangle is defined by its bottom-left corner
The second rectangle is defined by its bottom-left corner
Example 1:
Image: https://assets.leetcode.com/uploads/2021/05/08/rectangle-plane.png
Example 2:
Constraints:
•
•
•
•
223. Rectangle Area
Topic: Math, Geometry
Difficulty: Medium
Problem:
Given the coordinates of two rectilinear rectangles in a 2D plane, return the total area covered by the two rectangles.
The first rectangle is defined by its bottom-left corner
(ax1, ay1) and its top-right corner (ax2, ay2).The second rectangle is defined by its bottom-left corner
(bx1, by1) and its top-right corner (bx2, by2).Example 1:
Image: https://assets.leetcode.com/uploads/2021/05/08/rectangle-plane.png
Input: ax1 = -3, ay1 = 0, ax2 = 3, ay2 = 4, bx1 = 0, by1 = -1, bx2 = 9, by2 = 2
Output: 45
Example 2:
Input: ax1 = -2, ay1 = -2, ax2 = 2, ay2 = 2, bx1 = -2, by1 = -2, bx2 = 2, by2 = 2
Output: 16
Constraints:
•
-10^4 <= ax1 <= ax2 <= 10^4•
-10^4 <= ay1 <= ay2 <= 10^4•
-10^4 <= bx1 <= bx2 <= 10^4•
-10^4 <= by1 <= by2 <= 10^42022-11-18
263. Ugly Number
Topic: Math
Difficulty: Easy
Problem:
An ugly number is a positive integer whose prime factors are limited to
Given an integer
Example 1:
Example 2:
Example 3:
Constraints:
•
263. Ugly Number
Topic: Math
Difficulty: Easy
Problem:
An ugly number is a positive integer whose prime factors are limited to
2, 3, and 5.Given an integer
n, return true if n is an ugly number.Example 1:
Input: n = 6
Output: true
Explanation: 6 = 2 × 3
Example 2:
Input: n = 1
Output: true
Explanation: 1 has no prime factors, therefore all of its prime factors are limited to 2, 3, and 5.
Example 3:
Input: n = 14
Output: false
Explanation: 14 is not ugly since it includes the prime factor 7.
Constraints:
•
-2^31 <= n <= 2^31 - 1