2022-08-12
235. Lowest Common Ancestor of a Binary Search Tree
Topic: Tree, Depth-First Search, Binary Search Tree, Binary Tree
Difficulty: Easy
Problem:
Given a binary search tree (BST), find the lowest common ancestor (LCA) node of two given nodes in the BST.
According to the definition of LCA on Wikipedia: “The lowest common ancestor is defined between two nodes
Example 1:
Image: https://assets.leetcode.com/uploads/2018/12/14/binarysearchtree_improved.png
Example 2:
Image: https://assets.leetcode.com/uploads/2018/12/14/binarysearchtree_improved.png
Example 3:
Constraints:
• The number of nodes in the tree is in the range
•
• All
•
•
235. Lowest Common Ancestor of a Binary Search Tree
Topic: Tree, Depth-First Search, Binary Search Tree, Binary Tree
Difficulty: Easy
Problem:
Given a binary search tree (BST), find the lowest common ancestor (LCA) node of two given nodes in the BST.
According to the definition of LCA on Wikipedia: “The lowest common ancestor is defined between two nodes
p and q as the lowest node in T that has both p and q as descendants (where we allow a node to be a descendant of itself).”Example 1:
Image: https://assets.leetcode.com/uploads/2018/12/14/binarysearchtree_improved.png
Input: root = [6,2,8,0,4,7,9,null,null,3,5], p = 2, q = 8
Output: 6
Explanation: The LCA of nodes 2 and 8 is 6.
Example 2:
Image: https://assets.leetcode.com/uploads/2018/12/14/binarysearchtree_improved.png
Input: root = [6,2,8,0,4,7,9,null,null,3,5], p = 2, q = 4
Output: 2
Explanation: The LCA of nodes 2 and 4 is 2, since a node can be a descendant of itself according to the LCA definition.
Example 3:
Input: root = [2,1], p = 2, q = 1
Output: 2
Constraints:
• The number of nodes in the tree is in the range
[2, 10^5].•
-10^9 <= Node.val <= 10^9• All
Node.val are unique.•
p != q•
p and q will exist in the BST.2022-08-13
30. Substring with Concatenation of All Words
Topic: Hash Table, String, Sliding Window
Difficulty: Hard
Problem:
You are given a string
You can return the answer in any order.
Example 1:
Example 2:
Example 3:
Constraints:
•
•
•
•
30. Substring with Concatenation of All Words
Topic: Hash Table, String, Sliding Window
Difficulty: Hard
Problem:
You are given a string
s and an array of strings words of the same length. Return all starting indices of substring(s) in s that is a concatenation of each word in words exactly once, in any order, and without any intervening characters.You can return the answer in any order.
Example 1:
Input: s = "barfoothefoobarman", words = ["foo","bar"]
Output: [0,9]
Explanation: Substrings starting at index 0 and 9 are "barfoo" and "foobar" respectively.
The output order does not matter, returning [9,0] is fine too.
Example 2:
Input: s = "wordgoodgoodgoodbestword", words = ["word","good","best","word"]
Output: []
Example 3:
Input: s = "barfoofoobarthefoobarman", words = ["bar","foo","the"]
Output: [6,9,12]
Constraints:
•
1 <= s.length <= 10^4•
1 <= words.length <= 5000•
1 <= words[i].length <= 30•
s and words[i] consist of lowercase English letters.2022-08-14
126. Word Ladder II
Topic: Hash Table, String, Backtracking, 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
126. Word Ladder II
Topic: Hash Table, String, Backtracking, 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 all the shortest transformation sequences from beginWord to endWord, or an empty list if no such sequence exists. Each sequence should be returned as a list of the words [beginWord, s_1, s_2, ..., s_k].Example 1:
Input: beginWord = "hit", endWord = "cog", wordList = ["hot","dot","dog","lot","log","cog"]
Output: [["hit","hot","dot","dog","cog"],["hit","hot","lot","log","cog"]]
Explanation: There are 2 shortest transformation sequences:
"hit" -> "hot" -> "dot" -> "dog" -> "cog"
"hit" -> "hot" -> "lot" -> "log" -> "cog"
Example 2:
Input: beginWord = "hit", endWord = "cog", wordList = ["hot","dot","dog","lot","log"]
Output: []
Explanation: The endWord "cog" is not in wordList, therefore there is no valid transformation sequence.
Constraints:
•
1 <= beginWord.length <= 5•
endWord.length == beginWord.length•
1 <= wordList.length <= 500•
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-08-15
13. Roman to Integer
Topic: Hash Table, Math, String
Difficulty: Easy
Problem:
Roman numerals are represented by seven different symbols:
For example,
Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not
•
•
•
Given a roman numeral, convert it to an integer.
Example 1:
Example 2:
Example 3:
Constraints:
•
•
• It is guaranteed that
13. Roman to Integer
Topic: Hash Table, Math, String
Difficulty: Easy
Problem:
Roman numerals are represented by seven different symbols:
I, V, X, L, C, D and M.Symbol Value
I 1
V 5
X 10
L 50
C 100
D 500
M 1000
For example,
2 is written as II in Roman numeral, just two ones added together. 12 is written as XII, which is simply X + II. The number 27 is written as XXVII, which is XX + V + II.Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not
IIII. Instead, the number four is written as IV. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as IX. There are six instances where subtraction is used:•
I can be placed before V (5) and X (10) to make 4 and 9.•
X can be placed before L (50) and C (100) to make 40 and 90.•
C can be placed before D (500) and M (1000) to make 400 and 900.Given a roman numeral, convert it to an integer.
Example 1:
Input: s = "III"
Output: 3
Explanation: III = 3.
Example 2:
Input: s = "LVIII"
Output: 58
Explanation: L = 50, V= 5, III = 3.
Example 3:
Input: s = "MCMXCIV"
Output: 1994
Explanation: M = 1000, CM = 900, XC = 90 and IV = 4.
Constraints:
•
1 <= s.length <= 15•
s contains only the characters ('I', 'V', 'X', 'L', 'C', 'D', 'M').• It is guaranteed that
s is a valid roman numeral in the range [1, 3999].2022-08-16
387. First Unique Character in a String
Topic: Hash Table, String, Queue, Counting
Difficulty: Easy
Problem:
Given a string
Example 1:
Example 2:
Example 3:
Constraints:
•
•
387. First Unique Character in a String
Topic: Hash Table, String, Queue, Counting
Difficulty: Easy
Problem:
Given a string
s, find the first non-repeating character in it and return its index. If it does not exist, return -1.Example 1:
Input: s = "leetcode"
Output: 0
Example 2:
Input: s = "loveleetcode"
Output: 2
Example 3:
Input: s = "aabb"
Output: -1
Constraints:
•
1 <= s.length <= 10^5•
s consists of only lowercase English letters.2022-08-17
804. Unique Morse Code Words
Topic: Array, Hash Table, String
Difficulty: Easy
Problem:
International Morse Code defines a standard encoding where each letter is mapped to a series of dots and dashes, as follows:
•
•
•
For convenience, the full table for the
Given an array of strings
• For example,
Return the number of different transformations among all words we have.
Example 1:
Example 2:
Constraints:
•
•
•
804. Unique Morse Code Words
Topic: Array, Hash Table, String
Difficulty: Easy
Problem:
International Morse Code defines a standard encoding where each letter is mapped to a series of dots and dashes, as follows:
•
'a' maps to ".-",•
'b' maps to "-...",•
'c' maps to "-.-.", and so on.For convenience, the full table for the
26 letters of the English alphabet is given below:[".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."]
Given an array of strings
words where each word can be written as a concatenation of the Morse code of each letter.• For example,
"cab" can be written as "-.-..--...", which is the concatenation of "-.-.", ".-", and "-...". We will call such a concatenation the transformation of a word.Return the number of different transformations among all words we have.
Example 1:
Input: words = ["gin","zen","gig","msg"]
Output: 2
Explanation: The transformation of each word is:
"gin" -> "--...-."
"zen" -> "--...-."
"gig" -> "--...--."
"msg" -> "--...--."
There are 2 different transformations: "--...-." and "--...--.".
Example 2:
Input: words = ["a"]
Output: 1
Constraints:
•
1 <= words.length <= 100•
1 <= words[i].length <= 12•
words[i] consists of lowercase English letters.2022-08-18
1338. Reduce Array Size to The Half
Topic: Array, Hash Table, Greedy, Sorting, Heap (Priority Queue)
Difficulty: Medium
Problem:
You are given an integer array
Return the minimum size of the set so that at least half of the integers of the array are removed.
Example 1:
Example 2:
Constraints:
•
•
•
1338. Reduce Array Size to The Half
Topic: Array, Hash Table, Greedy, Sorting, Heap (Priority Queue)
Difficulty: Medium
Problem:
You are given an integer array
arr. You can choose a set of integers and remove all the occurrences of these integers in the array.Return the minimum size of the set so that at least half of the integers of the array are removed.
Example 1:
Input: arr = [3,3,3,3,5,5,5,2,2,7]
Output: 2
Explanation: Choosing {3,7} will make the new array [5,5,5,2,2] which has size 5 (i.e equal to half of the size of the old array).
Possible sets of size 2 are {3,5},{3,2},{5,2}.
Choosing set {2,7} is not possible as it will make the new array [3,3,3,3,5,5,5] which has a size greater than half of the size of the old array.
Example 2:
Input: arr = [7,7,7,7,7,7]
Output: 1
Explanation: The only possible set you can choose is {7}. This will make the new array empty.
Constraints:
•
2 <= arr.length <= 10^5•
arr.length is even.•
1 <= arr[i] <= 10^52022-08-19
659. Split Array into Consecutive Subsequences
Topic: Array, Hash Table, Greedy, Heap (Priority Queue)
Difficulty: Medium
Problem:
You are given an integer array
Determine if it is possible to split
• Each subsequence is a consecutive increasing sequence (i.e. each integer is exactly one more than the previous integer).
• All subsequences have a length of
Return
A subsequence of an array is a new array that is formed from the original array by deleting some (can be none) of the elements without disturbing the relative positions of the remaining elements. (i.e.,
Example 1:
Example 2:
Example 3:
Constraints:
•
•
•
659. Split Array into Consecutive Subsequences
Topic: Array, Hash Table, Greedy, Heap (Priority Queue)
Difficulty: Medium
Problem:
You are given an integer array
nums that is sorted in non-decreasing order.Determine if it is possible to split
nums into one or more subsequences such that both of the following conditions are true:• Each subsequence is a consecutive increasing sequence (i.e. each integer is exactly one more than the previous integer).
• All subsequences have a length of
3 or more.Return
true if you can split nums according to the above conditions, or false otherwise.A subsequence of an array is a new array that is formed from the original array by deleting some (can be none) of the elements without disturbing the relative positions of the remaining elements. (i.e.,
[1,3,5] is a subsequence of [1,2,3,4,5] while [1,3,2] is not).Example 1:
Input: nums = [1,2,3,3,4,5]
Output: true
Explanation: nums can be split into the following subsequences:
[1,2,3,3,4,5] --> 1, 2, 3
[1,2,3,3,4,5] --> 3, 4, 5
Example 2:
Input: nums = [1,2,3,3,4,4,5,5]
Output: true
Explanation: nums can be split into the following subsequences:
[1,2,3,3,4,4,5,5] --> 1, 2, 3, 4, 5
[1,2,3,3,4,4,5,5] --> 3, 4, 5
Example 3:
Input: nums = [1,2,3,4,4,5]
Output: false
Explanation: It is impossible to split nums into consecutive increasing subsequences of length 3 or more.
Constraints:
•
1 <= nums.length <= 10^4•
-1000 <= nums[i] <= 1000•
nums is sorted in non-decreasing order.2022-08-20
871. Minimum Number of Refueling Stops
Topic: Array, Dynamic Programming, Greedy, Heap (Priority Queue)
Difficulty: Hard
Problem:
A car travels from a starting position to a destination which is
There are gas stations along the way. The gas stations are represented as an array
The car starts with an infinite tank of gas, which initially has
Return the minimum number of refueling stops the car must make in order to reach its destination. If it cannot reach the destination, return
Note that if the car reaches a gas station with
Example 1:
Example 2:
Example 3:
Constraints:
•
•
•
•
871. Minimum Number of Refueling Stops
Topic: Array, Dynamic Programming, Greedy, Heap (Priority Queue)
Difficulty: Hard
Problem:
A car travels from a starting position to a destination which is
target miles east of the starting position.There are gas stations along the way. The gas stations are represented as an array
stations where stations[i] = [position_i, fuel_i] indicates that the i^th gas station is position_i miles east of the starting position and has fuel_i liters of gas.The car starts with an infinite tank of gas, which initially has
startFuel liters of fuel in it. It uses one liter of gas per one mile that it drives. When the car reaches a gas station, it may stop and refuel, transferring all the gas from the station into the car.Return the minimum number of refueling stops the car must make in order to reach its destination. If it cannot reach the destination, return
-1.Note that if the car reaches a gas station with
0 fuel left, the car can still refuel there. If the car reaches the destination with 0 fuel left, it is still considered to have arrived.Example 1:
Input: target = 1, startFuel = 1, stations = []
Output: 0
Explanation: We can reach the target without refueling.
Example 2:
Input: target = 100, startFuel = 1, stations = [[10,100]]
Output: -1
Explanation: We can not reach the target (or even the first gas station).
Example 3:
Input: target = 100, startFuel = 10, stations = [[10,60],[20,30],[30,30],[60,40]]
Output: 2
Explanation: We start with 10 liters of fuel.
We drive to position 10, expending 10 liters of fuel. We refuel from 0 liters to 60 liters of gas.
Then, we drive from position 10 to position 60 (expending 50 liters of fuel),
and refuel from 10 liters to 50 liters of gas. We then drive to and reach the target.
We made 2 refueling stops along the way, so we return 2.
Constraints:
•
1 <= target, startFuel <= 10^9•
0 <= stations.length <= 500•
0 <= position_i <= position_i+1 < target•
1 <= fuel_i < 10^92022-08-21
936. Stamping The Sequence
Topic: String, Stack, Greedy, Queue
Difficulty: Hard
Problem:
You are given two strings
In one turn, you can place
• For example, if
• place
• place
• place
Note that
We want to convert
Return an array of the index of the left-most letter being stamped at each turn. If we cannot obtain
Example 1:
Example 2:
Constraints:
•
•
936. Stamping The Sequence
Topic: String, Stack, Greedy, Queue
Difficulty: Hard
Problem:
You are given two strings
stamp and target. Initially, there is a string s of length target.length with all s[i] == '?'.In one turn, you can place
stamp over s and replace every letter in the s with the corresponding letter from stamp.• For example, if
stamp = "abc" and target = "abcba", then s is "?????" initially. In one turn you can:• place
stamp at index 0 of s to obtain "abc??",• place
stamp at index 1 of s to obtain "?abc?", or• place
stamp at index 2 of s to obtain "??abc".Note that
stamp must be fully contained in the boundaries of s in order to stamp (i.e., you cannot place stamp at index 3 of s).We want to convert
s to target using at most 10 * target.length turns.Return an array of the index of the left-most letter being stamped at each turn. If we cannot obtain
target from s within 10 * target.length turns, return an empty array.Example 1:
Input: stamp = "abc", target = "ababc"
Output: [0,2]
Explanation: Initially s = "?????".
- Place stamp at index 0 to get "abc??".
- Place stamp at index 2 to get "ababc".
[1,0,2] would also be accepted as an answer, as well as some other answers.
Example 2:
Input: stamp = "abca", target = "aabcaca"
Output: [3,0,1]
Explanation: Initially s = "???????".
- Place stamp at index 3 to get "???abca".
- Place stamp at index 0 to get "abcabca".
- Place stamp at index 1 to get "aabcaca".
Constraints:
•
1 <= stamp.length <= target.length <= 1000•
stamp and target consist of lowercase English letters.2022-08-22
342. Power of Four
Topic: Math, Bit Manipulation, Recursion
Difficulty: Easy
Problem:
Given an integer
An integer
Example 1:
Example 2:
Example 3:
Constraints:
•
Follow up: Could you solve it without loops/recursion?
342. Power of Four
Topic: Math, Bit Manipulation, Recursion
Difficulty: Easy
Problem:
Given an integer
n, return true if it is a power of four. Otherwise, return false.An integer
n is a power of four, if there exists an integer x such that n == 4^x.Example 1:
Input: n = 16
Output: true
Example 2:
Input: n = 5
Output: false
Example 3:
Input: n = 1
Output: true
Constraints:
•
-2^31 <= n <= 2^31 - 1Follow up: Could you solve it without loops/recursion?
2022-08-23
234. Palindrome Linked List
Topic: Linked List, Two Pointers, Stack, Recursion
Difficulty: Easy
Problem:
Given the
Example 1:
Image: https://assets.leetcode.com/uploads/2021/03/03/pal1linked-list.jpg
Example 2:
Image: https://assets.leetcode.com/uploads/2021/03/03/pal2linked-list.jpg
Constraints:
• The number of nodes in the list is in the range
•
Follow up: Could you do it in
234. Palindrome Linked List
Topic: Linked List, Two Pointers, Stack, Recursion
Difficulty: Easy
Problem:
Given the
head of a singly linked list, return true if it is a palindrome.Example 1:
Image: https://assets.leetcode.com/uploads/2021/03/03/pal1linked-list.jpg
Input: head = [1,2,2,1]
Output: true
Example 2:
Image: https://assets.leetcode.com/uploads/2021/03/03/pal2linked-list.jpg
Input: head = [1,2]
Output: false
Constraints:
• The number of nodes in the list is in the range
[1, 10^5].•
0 <= Node.val <= 9Follow up: Could you do it in
O(n) time and O(1) space?2022-08-24
326. Power of Three
Topic: Math, Recursion
Difficulty: Easy
Problem:
Given an integer
An integer
Example 1:
Example 2:
Example 3:
Constraints:
•
Follow up: Could you solve it without loops/recursion?
326. Power of Three
Topic: Math, Recursion
Difficulty: Easy
Problem:
Given an integer
n, return true if it is a power of three. Otherwise, return false.An integer
n is a power of three, if there exists an integer x such that n == 3^x.Example 1:
Input: n = 27
Output: true
Example 2:
Input: n = 0
Output: false
Example 3:
Input: n = 9
Output: true
Constraints:
•
-2^31 <= n <= 2^31 - 1Follow up: Could you solve it without loops/recursion?
2022-08-25
383. Ransom Note
Topic: Hash Table, String, Counting
Difficulty: Easy
Problem:
Given two strings
Each letter in
Example 1:
Example 2:
Example 3:
Constraints:
•
•
383. Ransom Note
Topic: Hash Table, String, Counting
Difficulty: Easy
Problem:
Given two strings
ransomNote and magazine, return true if ransomNote can be constructed by using the letters from magazine and false otherwise.Each letter in
magazine can only be used once in ransomNote.Example 1:
Input: ransomNote = "a", magazine = "b"
Output: false
Example 2:
Input: ransomNote = "aa", magazine = "ab"
Output: false
Example 3:
Input: ransomNote = "aa", magazine = "aab"
Output: true
Constraints:
•
1 <= ransomNote.length, magazine.length <= 10^5•
ransomNote and magazine consist of lowercase English letters.2022-08-26
869. Reordered Power of 2
Topic: Math, Sorting, Counting, Enumeration
Difficulty: Medium
Problem:
You are given an integer
Return
Example 1:
Example 2:
Constraints:
•
869. Reordered Power of 2
Topic: Math, Sorting, Counting, Enumeration
Difficulty: Medium
Problem:
You are given an integer
n. We reorder the digits in any order (including the original order) such that the leading digit is not zero.Return
true if and only if we can do this so that the resulting number is a power of two.Example 1:
Input: n = 1
Output: true
Example 2:
Input: n = 10
Output: false
Constraints:
•
1 <= n <= 10^92022-08-27
363. Max Sum of Rectangle No Larger Than K
Topic: Array, Binary Search, Matrix, Prefix Sum, Ordered Set
Difficulty: Hard
Problem:
Given an
It is guaranteed that there will be a rectangle with a sum no larger than
Example 1:
Image: https://assets.leetcode.com/uploads/2021/03/18/sum-grid.jpg
Example 2:
Constraints:
•
•
•
•
•
Follow up: What if the number of rows is much larger than the number of columns?
363. Max Sum of Rectangle No Larger Than K
Topic: Array, Binary Search, Matrix, Prefix Sum, Ordered Set
Difficulty: Hard
Problem:
Given an
m x n matrix matrix and an integer k, return the max sum of a rectangle in the matrix such that its sum is no larger than k.It is guaranteed that there will be a rectangle with a sum no larger than
k.Example 1:
Image: https://assets.leetcode.com/uploads/2021/03/18/sum-grid.jpg
Input: matrix = [[1,0,1],[0,-2,3]], k = 2
Output: 2
Explanation: Because the sum of the blue rectangle [[0, 1], [-2, 3]] is 2, and 2 is the max number no larger than k (k = 2).
Example 2:
Input: matrix = [[2,2,-1]], k = 3
Output: 3
Constraints:
•
m == matrix.length•
n == matrix[i].length•
1 <= m, n <= 100•
-100 <= matrix[i][j] <= 100•
-10^5 <= k <= 10^5Follow up: What if the number of rows is much larger than the number of columns?
2022-08-28
1329. Sort the Matrix Diagonally
Topic: Array, Sorting, Matrix
Difficulty: Medium
Problem:
A matrix diagonal is a diagonal line of cells starting from some cell in either the topmost row or leftmost column and going in the bottom-right direction until reaching the matrix's end. For example, the matrix diagonal starting from
Given an
Example 1:
Image: https://assets.leetcode.com/uploads/2020/01/21/1482_example_1_2.png
Example 2:
Constraints:
•
•
•
•
1329. Sort the Matrix Diagonally
Topic: Array, Sorting, Matrix
Difficulty: Medium
Problem:
A matrix diagonal is a diagonal line of cells starting from some cell in either the topmost row or leftmost column and going in the bottom-right direction until reaching the matrix's end. For example, the matrix diagonal starting from
mat[2][0], where mat is a 6 x 3 matrix, includes cells mat[2][0], mat[3][1], and mat[4][2].Given an
m x n matrix mat of integers, sort each matrix diagonal in ascending order and return the resulting matrix.Example 1:
Image: https://assets.leetcode.com/uploads/2020/01/21/1482_example_1_2.png
Input: mat = [[3,3,1,1],[2,2,1,2],[1,1,1,2]]
Output: [[1,1,1,1],[1,2,2,2],[1,2,3,3]]
Example 2:
Input: mat = [[11,25,66,1,69,7],[23,55,17,45,15,52],[75,31,36,44,58,8],[22,27,33,25,68,4],[84,28,14,11,5,50]]
Output: [[5,17,4,1,52,7],[11,11,25,45,8,69],[14,23,25,44,58,15],[22,27,31,36,50,66],[84,28,75,33,55,68]]
Constraints:
•
m == mat.length•
n == mat[i].length•
1 <= m, n <= 100•
1 <= mat[i][j] <= 1002022-08-29
200. Number of Islands
Topic: Array, Depth-First Search, Breadth-First Search, Union Find, Matrix
Difficulty: Medium
Problem:
Given an
An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water.
Example 1:
Example 2:
Constraints:
•
•
•
•
200. Number of Islands
Topic: Array, Depth-First Search, Breadth-First Search, Union Find, Matrix
Difficulty: Medium
Problem:
Given an
m x n 2D binary grid grid which represents a map of '1's (land) and '0's (water), return the number of islands.An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water.
Example 1:
Input: grid = [
["1","1","1","1","0"],
["1","1","0","1","0"],
["1","1","0","0","0"],
["0","0","0","0","0"]
]
Output: 1
Example 2:
Input: grid = [
["1","1","0","0","0"],
["1","1","0","0","0"],
["0","0","1","0","0"],
["0","0","0","1","1"]
]
Output: 3
Constraints:
•
m == grid.length•
n == grid[i].length•
1 <= m, n <= 300•
grid[i][j] is '0' or '1'.2022-08-30
48. Rotate Image
Topic: Array, Math, Matrix
Difficulty: Medium
Problem:
You are given an
You have to rotate the image in-place, which means you have to modify the input 2D matrix directly. DO NOT allocate another 2D matrix and do the rotation.
Example 1:
Image: https://assets.leetcode.com/uploads/2020/08/28/mat1.jpg
Example 2:
Image: https://assets.leetcode.com/uploads/2020/08/28/mat2.jpg
Constraints:
•
•
•
48. Rotate Image
Topic: Array, Math, Matrix
Difficulty: Medium
Problem:
You are given an
n x n 2D matrix representing an image, rotate the image by 90 degrees (clockwise).You have to rotate the image in-place, which means you have to modify the input 2D matrix directly. DO NOT allocate another 2D matrix and do the rotation.
Example 1:
Image: https://assets.leetcode.com/uploads/2020/08/28/mat1.jpg
Input: matrix = [[1,2,3],[4,5,6],[7,8,9]]
Output: [[7,4,1],[8,5,2],[9,6,3]]
Example 2:
Image: https://assets.leetcode.com/uploads/2020/08/28/mat2.jpg
Input: matrix = [[5,1,9,11],[2,4,8,10],[13,3,6,7],[15,14,12,16]]
Output: [[15,13,2,5],[14,3,4,1],[12,6,8,9],[16,7,10,11]]
Constraints:
•
n == matrix.length == matrix[i].length•
1 <= n <= 20•
-1000 <= matrix[i][j] <= 10002022-08-31
417. Pacific Atlantic Water Flow
Topic: Array, Depth-First Search, Breadth-First Search, Matrix
Difficulty: Medium
Problem:
There is an
The island is partitioned into a grid of square cells. You are given an
The island receives a lot of rain, and the rain water can flow to neighboring cells directly north, south, east, and west if the neighboring cell's height is less than or equal to the current cell's height. Water can flow from any cell adjacent to an ocean into the ocean.
Return a 2D list of grid coordinates
Example 1:
Image: https://assets.leetcode.com/uploads/2021/06/08/waterflow-grid.jpg
Example 2:
Constraints:
•
•
•
•
417. Pacific Atlantic Water Flow
Topic: Array, Depth-First Search, Breadth-First Search, Matrix
Difficulty: Medium
Problem:
There is an
m x n rectangular island that borders both the Pacific Ocean and Atlantic Ocean. The Pacific Ocean touches the island's left and top edges, and the Atlantic Ocean touches the island's right and bottom edges.The island is partitioned into a grid of square cells. You are given an
m x n integer matrix heights where heights[r][c] represents the height above sea level of the cell at coordinate (r, c).The island receives a lot of rain, and the rain water can flow to neighboring cells directly north, south, east, and west if the neighboring cell's height is less than or equal to the current cell's height. Water can flow from any cell adjacent to an ocean into the ocean.
Return a 2D list of grid coordinates
result where result[i] = [r_i, c_i] denotes that rain water can flow from cell (r_i, c_i) to both the Pacific and Atlantic oceans.Example 1:
Image: https://assets.leetcode.com/uploads/2021/06/08/waterflow-grid.jpg
Input: heights = [[1,2,2,3,5],[3,2,3,4,4],[2,4,5,3,1],[6,7,1,4,5],[5,1,1,2,4]]
Output: [[0,4],[1,3],[1,4],[2,2],[3,0],[3,1],[4,0]]
Explanation: The following cells can flow to the Pacific and Atlantic oceans, as shown below:
[0,4]: [0,4] -> Pacific Ocean
[0,4] -> Atlantic Ocean
[1,3]: [1,3] -> [0,3] -> Pacific Ocean
[1,3] -> [1,4] -> Atlantic Ocean
[1,4]: [1,4] -> [1,3] -> [0,3] -> Pacific Ocean
[1,4] -> Atlantic Ocean
[2,2]: [2,2] -> [1,2] -> [0,2] -> Pacific Ocean
[2,2] -> [2,3] -> [2,4] -> Atlantic Ocean
[3,0]: [3,0] -> Pacific Ocean
[3,0] -> [4,0] -> Atlantic Ocean
[3,1]: [3,1] -> [3,0] -> Pacific Ocean
[3,1] -> [4,1] -> Atlantic Ocean
[4,0]: [4,0] -> Pacific Ocean
[4,0] -> Atlantic Ocean
Note that there are other possible paths for these cells to flow to the Pacific and Atlantic oceans.
Example 2:
Input: heights = [[1]]
Output: [[0,0]]
Explanation: The water can flow from the only cell to the Pacific and Atlantic oceans.
Constraints:
•
m == heights.length•
n == heights[r].length•
1 <= m, n <= 200•
0 <= heights[r][c] <= 10^52022-09-01
1448. Count Good Nodes in Binary Tree
Topic: Tree, Depth-First Search, Breadth-First Search, Binary Tree
Difficulty: Medium
Problem:
Given a binary tree
Return the number of good nodes in the binary tree.
Example 1:
Image: https://assets.leetcode.com/uploads/2020/04/02/test_sample_1.png
Example 2:
Image: https://assets.leetcode.com/uploads/2020/04/02/test_sample_2.png
Example 3:
Constraints:
• The number of nodes in the binary tree is in the range
• Each node's value is between
1448. Count Good Nodes in Binary Tree
Topic: Tree, Depth-First Search, Breadth-First Search, Binary Tree
Difficulty: Medium
Problem:
Given a binary tree
root, a node X in the tree is named good if in the path from root to X there are no nodes with a value greater than X.Return the number of good nodes in the binary tree.
Example 1:
Image: https://assets.leetcode.com/uploads/2020/04/02/test_sample_1.png
Input: root = [3,1,4,3,null,1,5]
Output: 4
Explanation: Nodes in blue are good.
Root Node (3) is always a good node.
Node 4 -> (3,4) is the maximum value in the path starting from the root.
Node 5 -> (3,4,5) is the maximum value in the path
Node 3 -> (3,1,3) is the maximum value in the path.
Example 2:
Image: https://assets.leetcode.com/uploads/2020/04/02/test_sample_2.png
Input: root = [3,3,null,4,2]
Output: 3
Explanation: Node 2 -> (3, 3, 2) is not good, because "3" is higher than it.
Example 3:
Input: root = [1]
Output: 1
Explanation: Root is considered as good.
Constraints:
• The number of nodes in the binary tree is in the range
[1, 10^5].• Each node's value is between
[-10^4, 10^4].