2024-04-01
58. Length of Last Word
Topic: String
Difficulty: Easy
Problem:
Given a string
A word is a maximal substring consisting of non-space characters only.
Example 1:
Example 2:
Example 3:
Constraints:
•
•
• There will be at least one word in
58. Length of Last Word
Topic: String
Difficulty: Easy
Problem:
Given a string
s consisting of words and spaces, return the length of the last word in the string.A word is a maximal substring consisting of non-space characters only.
Example 1:
Input: s = "Hello World"
Output: 5
Explanation: The last word is "World" with length 5.
Example 2:
Input: s = " fly me to the moon "
Output: 4
Explanation: The last word is "moon" with length 4.
Example 3:
Input: s = "luffy is still joyboy"
Output: 6
Explanation: The last word is "joyboy" with length 6.
Constraints:
•
1 <= s.length <= 10^4•
s consists of only English letters and spaces ' '.• There will be at least one word in
s.2024-04-02
205. Isomorphic Strings
Topic: Hash Table, String
Difficulty: Easy
Problem:
Given two strings
Two strings
All occurrences of a character must be replaced with another character while preserving the order of characters. No two characters may map to the same character, but a character may map to itself.
Example 1:
Example 2:
Example 3:
Constraints:
•
•
•
205. Isomorphic Strings
Topic: Hash Table, String
Difficulty: Easy
Problem:
Given two strings
s and t, determine if they are isomorphic.Two strings
s and t are isomorphic if the characters in s can be replaced to get t.All occurrences of a character must be replaced with another character while preserving the order of characters. No two characters may map to the same character, but a character may map to itself.
Example 1:
Input: s = "egg", t = "add"
Output: true
Example 2:
Input: s = "foo", t = "bar"
Output: false
Example 3:
Input: s = "paper", t = "title"
Output: true
Constraints:
•
1 <= s.length <= 5 * 10^4•
t.length == s.length•
s and t consist of any valid ascii character.2024-04-03
79. Word Search
Topic: Array, String, Backtracking, Matrix
Difficulty: Medium
Problem:
Given an
The word can 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.
Example 1:
Image: https://assets.leetcode.com/uploads/2020/11/04/word2.jpg
Example 2:
Image: https://assets.leetcode.com/uploads/2020/11/04/word-1.jpg
Example 3:
Image: https://assets.leetcode.com/uploads/2020/10/15/word3.jpg
Constraints:
•
•
•
•
•
Follow up: Could you use search pruning to make your solution faster with a larger
79. Word Search
Topic: Array, String, Backtracking, Matrix
Difficulty: Medium
Problem:
Given an
m x n grid of characters board and a string word, return true if word exists in the grid.The word can 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.
Example 1:
Image: https://assets.leetcode.com/uploads/2020/11/04/word2.jpg
Input: board = [["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], word = "ABCCED"
Output: true
Example 2:
Image: https://assets.leetcode.com/uploads/2020/11/04/word-1.jpg
Input: board = [["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], word = "SEE"
Output: true
Example 3:
Image: https://assets.leetcode.com/uploads/2020/10/15/word3.jpg
Input: board = [["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], word = "ABCB"
Output: false
Constraints:
•
m == board.length•
n = board[i].length•
1 <= m, n <= 6•
1 <= word.length <= 15•
board and word consists of only lowercase and uppercase English letters.Follow up: Could you use search pruning to make your solution faster with a larger
board?2024-04-04
1614. Maximum Nesting Depth of the Parentheses
Topic: String, Stack
Difficulty: Easy
Problem:
A string is a valid parentheses string (denoted VPS) if it meets one of the following:
• It is an empty string
• It can be written as
• It can be written as
We can similarly define the nesting depth
•
•
•
•
For example,
Given a VPS represented as string
Example 1:
Example 2:
Constraints:
•
•
• It is guaranteed that parentheses expression
1614. Maximum Nesting Depth of the Parentheses
Topic: String, Stack
Difficulty: Easy
Problem:
A string is a valid parentheses string (denoted VPS) if it meets one of the following:
• It is an empty string
"", or a single character not equal to "(" or ")",• It can be written as
AB (A concatenated with B), where A and B are VPS's, or• It can be written as
(A), where A is a VPS.We can similarly define the nesting depth
depth(S) of any VPS S as follows:•
depth("") = 0•
depth(C) = 0, where C is a string with a single character not equal to "(" or ")".•
depth(A + B) = max(depth(A), depth(B)), where A and B are VPS's.•
depth("(" + A + ")") = 1 + depth(A), where A is a VPS.For example,
"", "()()", and "()(()())" are VPS's (with nesting depths 0, 1, and 2), and ")(" and "(()" are not VPS's.Given a VPS represented as string
s, return the nesting depth of s.Example 1:
Input: s = "(1+(2*3)+((8)/4))+1"
Output: 3
Explanation: Digit 8 is inside of 3 nested parentheses in the string.
Example 2:
Input: s = "(1)+((2))+(((3)))"
Output: 3
Constraints:
•
1 <= s.length <= 100•
s consists of digits 0-9 and characters '+', '-', '*', '/', '(', and ')'.• It is guaranteed that parentheses expression
s is a VPS.2024-04-05
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.2024-04-06
1249. Minimum Remove to Make Valid Parentheses
Topic: String, Stack
Difficulty: Medium
Problem:
Given a string s of
Your task is to remove the minimum number of parentheses (
Formally, a parentheses string is valid if and only if:
• It is the empty string, contains only lowercase characters, or
• It can be written as
• It can be written as
Example 1:
Example 2:
Example 3:
Constraints:
•
•
1249. Minimum Remove to Make Valid Parentheses
Topic: String, Stack
Difficulty: Medium
Problem:
Given a string s of
'(' , ')' and lowercase English characters.Your task is to remove the minimum number of parentheses (
'(' or ')', in any positions ) so that the resulting parentheses string is valid and return any valid string.Formally, a parentheses string is valid if and only if:
• It is the empty string, contains only lowercase characters, or
• It can be written as
AB (A concatenated with B), where A and B are valid strings, or• It can be written as
(A), where A is a valid string.Example 1:
Input: s = "lee(t(c)o)de)"
Output: "lee(t(c)o)de"
Explanation: "lee(t(co)de)" , "lee(t(c)ode)" would also be accepted.
Example 2:
Input: s = "a)b(c)d"
Output: "ab(c)d"
Example 3:
Input: s = "))(("
Output: ""
Explanation: An empty string is also valid.
Constraints:
•
1 <= s.length <= 10^5•
s[i] is either'(' , ')', or lowercase English letter.2024-04-07
678. Valid Parenthesis String
Topic: String, Dynamic Programming, Stack, Greedy
Difficulty: Medium
Problem:
Given a string
The following rules define a valid string:
• Any left parenthesis
• Any right parenthesis
• Left parenthesis
•
Example 1:
Example 2:
Example 3:
Constraints:
•
•
678. Valid Parenthesis String
Topic: String, Dynamic Programming, Stack, Greedy
Difficulty: Medium
Problem:
Given a string
s containing only three types of characters: '(', ')' and '*', return true if s is valid.The following rules define a valid string:
• Any left parenthesis
'(' must have a corresponding right parenthesis ')'.• Any right parenthesis
')' must have a corresponding left parenthesis '('.• Left parenthesis
'(' must go before the corresponding right parenthesis ')'.•
'*' could be treated as a single right parenthesis ')' or a single left parenthesis '(' or an empty string "".Example 1:
Input: s = "()"
Output: true
Example 2:
Input: s = "(*)"
Output: true
Example 3:
Input: s = "(*))"
Output: true
Constraints:
•
1 <= s.length <= 100•
s[i] is '(', ')' or '*'.2024-04-08
1700. Number of Students Unable to Eat Lunch
Topic: Array, Stack, Queue, Simulation
Difficulty: Easy
Problem:
The school cafeteria offers circular and square sandwiches at lunch break, referred to by numbers
The number of sandwiches in the cafeteria is equal to the number of students. The sandwiches are placed in a stack. At each step:
• If the student at the front of the queue prefers the sandwich on the top of the stack, they will take it and leave the queue.
• Otherwise, they will leave it and go to the queue's end.
This continues until none of the queue students want to take the top sandwich and are thus unable to eat.
You are given two integer arrays
Example 1:
Example 2:
Constraints:
•
•
•
•
1700. Number of Students Unable to Eat Lunch
Topic: Array, Stack, Queue, Simulation
Difficulty: Easy
Problem:
The school cafeteria offers circular and square sandwiches at lunch break, referred to by numbers
0 and 1 respectively. All students stand in a queue. Each student either prefers square or circular sandwiches.The number of sandwiches in the cafeteria is equal to the number of students. The sandwiches are placed in a stack. At each step:
• If the student at the front of the queue prefers the sandwich on the top of the stack, they will take it and leave the queue.
• Otherwise, they will leave it and go to the queue's end.
This continues until none of the queue students want to take the top sandwich and are thus unable to eat.
You are given two integer arrays
students and sandwiches where sandwiches[i] is the type of the i^th sandwich in the stack (i = 0 is the top of the stack) and students[j] is the preference of the j^th student in the initial queue (j = 0 is the front of the queue). Return the number of students that are unable to eat.Example 1:
Input: students = [1,1,0,0], sandwiches = [0,1,0,1]
Output: 0
Explanation:
- Front student leaves the top sandwich and returns to the end of the line making students = [1,0,0,1].
- Front student leaves the top sandwich and returns to the end of the line making students = [0,0,1,1].
- Front student takes the top sandwich and leaves the line making students = [0,1,1] and sandwiches = [1,0,1].
- Front student leaves the top sandwich and returns to the end of the line making students = [1,1,0].
- Front student takes the top sandwich and leaves the line making students = [1,0] and sandwiches = [0,1].
- Front student leaves the top sandwich and returns to the end of the line making students = [0,1].
- Front student takes the top sandwich and leaves the line making students = [1] and sandwiches = [1].
- Front student takes the top sandwich and leaves the line making students = [] and sandwiches = [].
Hence all students are able to eat.
Example 2:
Input: students = [1,1,1,0,0,1], sandwiches = [1,0,0,0,1,1]
Output: 3
Constraints:
•
1 <= students.length, sandwiches.length <= 100•
students.length == sandwiches.length•
sandwiches[i] is 0 or 1.•
students[i] is 0 or 1.2024-04-09
2073. Time Needed to Buy Tickets
Topic: Array, Queue, Simulation
Difficulty: Easy
Problem:
There are
You are given a 0-indexed integer array
Each person takes exactly 1 second to buy a ticket. A person can only buy 1 ticket at a time and has to go back to the end of the line (which happens instantaneously) in order to buy more tickets. If a person does not have any tickets left to buy, the person will leave the line.
Return the time taken for the person at position
Example 1:
Example 2:
Constraints:
•
•
•
•
2073. Time Needed to Buy Tickets
Topic: Array, Queue, Simulation
Difficulty: Easy
Problem:
There are
n people in a line queuing to buy tickets, where the 0^th person is at the front of the line and the (n - 1)^th person is at the back of the line.You are given a 0-indexed integer array
tickets of length n where the number of tickets that the i^th person would like to buy is tickets[i].Each person takes exactly 1 second to buy a ticket. A person can only buy 1 ticket at a time and has to go back to the end of the line (which happens instantaneously) in order to buy more tickets. If a person does not have any tickets left to buy, the person will leave the line.
Return the time taken for the person at position
k (0-indexed) to finish buying tickets.Example 1:
Input: tickets = [2,3,2], k = 2
Output: 6
Explanation:
- In the first pass, everyone in the line buys a ticket and the line becomes [1, 2, 1].
- In the second pass, everyone in the line buys a ticket and the line becomes [0, 1, 0].
The person at position 2 has successfully bought 2 tickets and it took 3 + 3 = 6 seconds.
Example 2:
Input: tickets = [5,1,1,1], k = 0
Output: 8
Explanation:
- In the first pass, everyone in the line buys a ticket and the line becomes [4, 0, 0, 0].
- In the next 4 passes, only the person in position 0 is buying tickets.
The person at position 0 has successfully bought 5 tickets and it took 4 + 1 + 1 + 1 + 1 = 8 seconds.
Constraints:
•
n == tickets.length•
1 <= n <= 100•
1 <= tickets[i] <= 100•
0 <= k < n2024-04-10
950. Reveal Cards In Increasing Order
Topic: Array, Queue, Sorting, Simulation
Difficulty: Medium
Problem:
You are given an integer array
You can order the deck in any order you want. Initially, all the cards start face down (unrevealed) in one deck.
You will do the following steps repeatedly until all cards are revealed:
1. Take the top card of the deck, reveal it, and take it out of the deck.
2. If there are still cards in the deck then put the next top card of the deck at the bottom of the deck.
3. If there are still unrevealed cards, go back to step 1. Otherwise, stop.
Return an ordering of the deck that would reveal the cards in increasing order.
Note that the first entry in the answer is considered to be the top of the deck.
Example 1:
Example 2:
Constraints:
•
•
• All the values of
950. Reveal Cards In Increasing Order
Topic: Array, Queue, Sorting, Simulation
Difficulty: Medium
Problem:
You are given an integer array
deck. There is a deck of cards where every card has a unique integer. The integer on the i^th card is deck[i].You can order the deck in any order you want. Initially, all the cards start face down (unrevealed) in one deck.
You will do the following steps repeatedly until all cards are revealed:
1. Take the top card of the deck, reveal it, and take it out of the deck.
2. If there are still cards in the deck then put the next top card of the deck at the bottom of the deck.
3. If there are still unrevealed cards, go back to step 1. Otherwise, stop.
Return an ordering of the deck that would reveal the cards in increasing order.
Note that the first entry in the answer is considered to be the top of the deck.
Example 1:
Input: deck = [17,13,11,2,3,5,7]
Output: [2,13,3,11,5,17,7]
Explanation:
We get the deck in the order [17,13,11,2,3,5,7] (this order does not matter), and reorder it.
After reordering, the deck starts as [2,13,3,11,5,17,7], where 2 is the top of the deck.
We reveal 2, and move 13 to the bottom. The deck is now [3,11,5,17,7,13].
We reveal 3, and move 11 to the bottom. The deck is now [5,17,7,13,11].
We reveal 5, and move 17 to the bottom. The deck is now [7,13,11,17].
We reveal 7, and move 13 to the bottom. The deck is now [11,17,13].
We reveal 11, and move 17 to the bottom. The deck is now [13,17].
We reveal 13, and move 17 to the bottom. The deck is now [17].
We reveal 17.
Since all the cards revealed are in increasing order, the answer is correct.
Example 2:
Input: deck = [1,1000]
Output: [1,1000]
Constraints:
•
1 <= deck.length <= 1000•
1 <= deck[i] <= 10^6• All the values of
deck are unique.2024-04-11
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.2024-04-12
42. Trapping Rain Water
Topic: Array, Two Pointers, Dynamic Programming, Stack, Monotonic Stack
Difficulty: Hard
Problem:
Given
Example 1:
Image: https://assets.leetcode.com/uploads/2018/10/22/rainwatertrap.png
Example 2:
Constraints:
•
•
•
42. Trapping Rain Water
Topic: Array, Two Pointers, Dynamic Programming, Stack, Monotonic Stack
Difficulty: Hard
Problem:
Given
n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it can trap after raining.Example 1:
Image: https://assets.leetcode.com/uploads/2018/10/22/rainwatertrap.png
Input: height = [0,1,0,2,1,0,1,3,2,1,2,1]
Output: 6
Explanation: The above elevation map (black section) is represented by array [0,1,0,2,1,0,1,3,2,1,2,1]. In this case, 6 units of rain water (blue section) are being trapped.
Example 2:
Input: height = [4,2,0,3,2,5]
Output: 9
Constraints:
•
n == height.length•
1 <= n <= 2 * 10^4•
0 <= height[i] <= 10^52024-04-13
85. Maximal Rectangle
Topic: Array, Dynamic Programming, Stack, Matrix, Monotonic Stack
Difficulty: Hard
Problem:
Given a
Example 1:
Image: https://assets.leetcode.com/uploads/2020/09/14/maximal.jpg
Example 2:
Example 3:
Constraints:
•
•
•
•
85. Maximal Rectangle
Topic: Array, Dynamic Programming, Stack, Matrix, Monotonic Stack
Difficulty: Hard
Problem:
Given a
rows x cols binary matrix filled with 0's and 1's, find the largest rectangle containing only 1's and return its area.Example 1:
Image: https://assets.leetcode.com/uploads/2020/09/14/maximal.jpg
Input: matrix = [["1","0","1","0","0"],["1","0","1","1","1"],["1","1","1","1","1"],["1","0","0","1","0"]]
Output: 6
Explanation: The maximal rectangle is shown in the above picture.
Example 2:
Input: matrix = [["0"]]
Output: 0
Example 3:
Input: matrix = [["1"]]
Output: 1
Constraints:
•
rows == matrix.length•
cols == matrix[i].length•
1 <= row, cols <= 200•
matrix[i][j] is '0' or '1'.2024-04-14
404. Sum of Left Leaves
Topic: Tree, Depth-First Search, Breadth-First Search, Binary Tree
Difficulty: Easy
Problem:
Given the
A leaf is a node with no children. A left leaf is a leaf that is the left child of another node.
Example 1:
Image: https://assets.leetcode.com/uploads/2021/04/08/leftsum-tree.jpg
Example 2:
Constraints:
• The number of nodes in the tree is in the range
•
404. Sum of Left Leaves
Topic: Tree, Depth-First Search, Breadth-First Search, Binary Tree
Difficulty: Easy
Problem:
Given the
root of a binary tree, return the sum of all left leaves.A leaf is a node with no children. A left leaf is a leaf that is the left child of another node.
Example 1:
Image: https://assets.leetcode.com/uploads/2021/04/08/leftsum-tree.jpg
Input: root = [3,9,20,null,null,15,7]
Output: 24
Explanation: There are two left leaves in the binary tree, with values 9 and 15 respectively.
Example 2:
Input: root = [1]
Output: 0
Constraints:
• The number of nodes in the tree is in the range
[1, 1000].•
-1000 <= Node.val <= 10002024-04-15
129. Sum Root to Leaf Numbers
Topic: Tree, Depth-First Search, Binary Tree
Difficulty: Medium
Problem:
You are given the
Each root-to-leaf path in the tree represents a number.
• For example, the root-to-leaf path
Return the total sum of all root-to-leaf numbers. Test cases are generated so that the answer will fit in a 32-bit integer.
A leaf node is a node with no children.
Example 1:
Image: https://assets.leetcode.com/uploads/2021/02/19/num1tree.jpg
Example 2:
Image: https://assets.leetcode.com/uploads/2021/02/19/num2tree.jpg
Constraints:
• The number of nodes in the tree is in the range
•
• The depth of the tree will not exceed
129. Sum Root to Leaf Numbers
Topic: Tree, Depth-First Search, Binary Tree
Difficulty: Medium
Problem:
You are given the
root of a binary tree containing digits from 0 to 9 only.Each root-to-leaf path in the tree represents a number.
• For example, the root-to-leaf path
1 -> 2 -> 3 represents the number 123.Return the total sum of all root-to-leaf numbers. Test cases are generated so that the answer will fit in a 32-bit integer.
A leaf node is a node with no children.
Example 1:
Image: https://assets.leetcode.com/uploads/2021/02/19/num1tree.jpg
Input: root = [1,2,3]
Output: 25
Explanation:
The root-to-leaf path 1->2 represents the number 12.
The root-to-leaf path 1->3 represents the number 13.
Therefore, sum = 12 + 13 = 25.
Example 2:
Image: https://assets.leetcode.com/uploads/2021/02/19/num2tree.jpg
Input: root = [4,9,0,5,1]
Output: 1026
Explanation:
The root-to-leaf path 4->9->5 represents the number 495.
The root-to-leaf path 4->9->1 represents the number 491.
The root-to-leaf path 4->0 represents the number 40.
Therefore, sum = 495 + 491 + 40 = 1026.
Constraints:
• The number of nodes in the tree is in the range
[1, 1000].•
0 <= Node.val <= 9• The depth of the tree will not exceed
10.2024-04-16
623. Add One Row to Tree
Topic: Tree, Depth-First Search, Breadth-First Search, Binary Tree
Difficulty: Medium
Problem:
Given the
Note that the
The adding rule is:
• Given the integer
•
•
• If
Example 1:
Image: https://assets.leetcode.com/uploads/2021/03/15/addrow-tree.jpg
Example 2:
Image: https://assets.leetcode.com/uploads/2021/03/11/add2-tree.jpg
Constraints:
• The number of nodes in the tree is in the range
• The depth of the tree is in the range
•
•
•
623. Add One Row to Tree
Topic: Tree, Depth-First Search, Breadth-First Search, Binary Tree
Difficulty: Medium
Problem:
Given the
root of a binary tree and two integers val and depth, add a row of nodes with value val at the given depth depth.Note that the
root node is at depth 1.The adding rule is:
• Given the integer
depth, for each not null tree node cur at the depth depth - 1, create two tree nodes with value val as cur's left subtree root and right subtree root.•
cur's original left subtree should be the left subtree of the new left subtree root.•
cur's original right subtree should be the right subtree of the new right subtree root.• If
depth == 1 that means there is no depth depth - 1 at all, then create a tree node with value val as the new root of the whole original tree, and the original tree is the new root's left subtree.Example 1:
Image: https://assets.leetcode.com/uploads/2021/03/15/addrow-tree.jpg
Input: root = [4,2,6,3,1,5], val = 1, depth = 2
Output: [4,1,1,2,null,null,6,3,1,5]
Example 2:
Image: https://assets.leetcode.com/uploads/2021/03/11/add2-tree.jpg
Input: root = [4,2,null,3,1], val = 1, depth = 3
Output: [4,2,null,1,1,3,null,null,1]
Constraints:
• The number of nodes in the tree is in the range
[1, 10^4].• The depth of the tree is in the range
[1, 10^4].•
-100 <= Node.val <= 100•
-10^5 <= val <= 10^5•
1 <= depth <= the depth of tree + 12024-04-17
988. Smallest String Starting From Leaf
Topic: String, Tree, Depth-First Search, Binary Tree
Difficulty: Medium
Problem:
You are given the
Return the lexicographically smallest string that starts at a leaf of this tree and ends at the root.
As a reminder, any shorter prefix of a string is lexicographically smaller.
• For example,
A leaf of a node is a node that has no children.
Example 1:
Image: https://assets.leetcode.com/uploads/2019/01/30/tree1.png
Example 2:
Image: https://assets.leetcode.com/uploads/2019/01/30/tree2.png
Example 3:
Image: https://assets.leetcode.com/uploads/2019/02/01/tree3.png
Constraints:
• The number of nodes in the tree is in the range
•
988. Smallest String Starting From Leaf
Topic: String, Tree, Depth-First Search, Binary Tree
Difficulty: Medium
Problem:
You are given the
root of a binary tree where each node has a value in the range [0, 25] representing the letters 'a' to 'z'.Return the lexicographically smallest string that starts at a leaf of this tree and ends at the root.
As a reminder, any shorter prefix of a string is lexicographically smaller.
• For example,
"ab" is lexicographically smaller than "aba".A leaf of a node is a node that has no children.
Example 1:
Image: https://assets.leetcode.com/uploads/2019/01/30/tree1.png
Input: root = [0,1,2,3,4,3,4]
Output: "dba"
Example 2:
Image: https://assets.leetcode.com/uploads/2019/01/30/tree2.png
Input: root = [25,1,3,1,3,0,2]
Output: "adz"
Example 3:
Image: https://assets.leetcode.com/uploads/2019/02/01/tree3.png
Input: root = [2,2,1,null,1,0,null,0]
Output: "abc"
Constraints:
• The number of nodes in the tree is in the range
[1, 8500].•
0 <= Node.val <= 252024-04-18
463. Island Perimeter
Topic: Array, Depth-First Search, Breadth-First Search, Matrix
Difficulty: Easy
Problem:
You are given
Grid cells are connected horizontally/vertically (not diagonally). The
The island doesn't have "lakes", meaning the water inside isn't connected to the water around the island. One cell is a square with side length 1. The grid is rectangular, width and height don't exceed 100. Determine the perimeter of the island.
Example 1:
Image: https://assets.leetcode.com/uploads/2018/10/12/island.png
Example 2:
Example 3:
Constraints:
•
•
•
•
• There is exactly one island in
463. Island Perimeter
Topic: Array, Depth-First Search, Breadth-First Search, Matrix
Difficulty: Easy
Problem:
You are given
row x col grid representing a map where grid[i][j] = 1 represents land and grid[i][j] = 0 represents water.Grid cells are connected horizontally/vertically (not diagonally). The
grid is completely surrounded by water, and there is exactly one island (i.e., one or more connected land cells).The island doesn't have "lakes", meaning the water inside isn't connected to the water around the island. One cell is a square with side length 1. The grid is rectangular, width and height don't exceed 100. Determine the perimeter of the island.
Example 1:
Image: https://assets.leetcode.com/uploads/2018/10/12/island.png
Input: grid = [[0,1,0,0],[1,1,1,0],[0,1,0,0],[1,1,0,0]]
Output: 16
Explanation: The perimeter is the 16 yellow stripes in the image above.
Example 2:
Input: grid = [[1]]
Output: 4
Example 3:
Input: grid = [[1,0]]
Output: 4
Constraints:
•
row == grid.length•
col == grid[i].length•
1 <= row, col <= 100•
grid[i][j] is 0 or 1.• There is exactly one island in
grid.2024-04-19
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'.2024-04-20
1992. Find All Groups of Farmland
Topic: Array, Depth-First Search, Breadth-First Search, Matrix
Difficulty: Medium
Problem:
You are given a 0-indexed
To keep the land organized, there are designated rectangular areas of hectares that consist entirely of farmland. These rectangular areas are called groups. No two groups are adjacent, meaning farmland in one group is not four-directionally adjacent to another farmland in a different group.
Return a 2D array containing the 4-length arrays described above for each group of farmland in
Example 1:
Image: https://assets.leetcode.com/uploads/2021/07/27/screenshot-2021-07-27-at-12-23-15-copy-of-diagram-drawio-diagrams-net.png
Example 2:
Image: https://assets.leetcode.com/uploads/2021/07/27/screenshot-2021-07-27-at-12-30-26-copy-of-diagram-drawio-diagrams-net.png
Example 3:
Image: https://assets.leetcode.com/uploads/2021/07/27/screenshot-2021-07-27-at-12-32-24-copy-of-diagram-drawio-diagrams-net.png
Constraints:
•
•
•
•
• Groups of farmland are rectangular in shape.
1992. Find All Groups of Farmland
Topic: Array, Depth-First Search, Breadth-First Search, Matrix
Difficulty: Medium
Problem:
You are given a 0-indexed
m x n binary matrix land where a 0 represents a hectare of forested land and a 1 represents a hectare of farmland.To keep the land organized, there are designated rectangular areas of hectares that consist entirely of farmland. These rectangular areas are called groups. No two groups are adjacent, meaning farmland in one group is not four-directionally adjacent to another farmland in a different group.
land can be represented by a coordinate system where the top left corner of land is (0, 0) and the bottom right corner of land is (m-1, n-1). Find the coordinates of the top left and bottom right corner of each group of farmland. A group of farmland with a top left corner at (r_1, c_1) and a bottom right corner at (r_2, c_2) is represented by the 4-length array [r_1, c_1, r_2, c_2].Return a 2D array containing the 4-length arrays described above for each group of farmland in
land. If there are no groups of farmland, return an empty array. You may return the answer in any order.Example 1:
Image: https://assets.leetcode.com/uploads/2021/07/27/screenshot-2021-07-27-at-12-23-15-copy-of-diagram-drawio-diagrams-net.png
Input: land = [[1,0,0],[0,1,1],[0,1,1]]
Output: [[0,0,0,0],[1,1,2,2]]
Explanation:
The first group has a top left corner at land[0][0] and a bottom right corner at land[0][0].
The second group has a top left corner at land[1][1] and a bottom right corner at land[2][2].
Example 2:
Image: https://assets.leetcode.com/uploads/2021/07/27/screenshot-2021-07-27-at-12-30-26-copy-of-diagram-drawio-diagrams-net.png
Input: land = [[1,1],[1,1]]
Output: [[0,0,1,1]]
Explanation:
The first group has a top left corner at land[0][0] and a bottom right corner at land[1][1].
Example 3:
Image: https://assets.leetcode.com/uploads/2021/07/27/screenshot-2021-07-27-at-12-32-24-copy-of-diagram-drawio-diagrams-net.png
Input: land = [[0]]
Output: []
Explanation:
There are no groups of farmland.
Constraints:
•
m == land.length•
n == land[i].length•
1 <= m, n <= 300•
land consists of only 0's and 1's.• Groups of farmland are rectangular in shape.
2024-04-21
1971. Find if Path Exists in Graph
Topic: Depth-First Search, Breadth-First Search, Union Find, Graph
Difficulty: Easy
Problem:
There is a bi-directional graph with
You want to determine if there is a valid path that exists from vertex
Given
Example 1:
Image: https://assets.leetcode.com/uploads/2021/08/14/validpath-ex1.png
Example 2:
Image: https://assets.leetcode.com/uploads/2021/08/14/validpath-ex2.png
Constraints:
•
•
•
•
•
•
• There are no duplicate edges.
• There are no self edges.
1971. Find if Path Exists in Graph
Topic: Depth-First Search, Breadth-First Search, Union Find, Graph
Difficulty: Easy
Problem:
There is a bi-directional graph with
n vertices, where each vertex is labeled from 0 to n - 1 (inclusive). The edges in the graph are represented as a 2D integer array edges, where each edges[i] = [u_i, v_i] denotes a bi-directional edge between vertex u_i and vertex v_i. Every vertex pair is connected by at most one edge, and no vertex has an edge to itself.You want to determine if there is a valid path that exists from vertex
source to vertex destination.Given
edges and the integers n, source, and destination, return true if there is a valid path from source to destination, or false otherwise.Example 1:
Image: https://assets.leetcode.com/uploads/2021/08/14/validpath-ex1.png
Input: n = 3, edges = [[0,1],[1,2],[2,0]], source = 0, destination = 2
Output: true
Explanation: There are two paths from vertex 0 to vertex 2:
- 0 → 1 → 2
- 0 → 2
Example 2:
Image: https://assets.leetcode.com/uploads/2021/08/14/validpath-ex2.png
Input: n = 6, edges = [[0,1],[0,2],[3,5],[5,4],[4,3]], source = 0, destination = 5
Output: false
Explanation: There is no path from vertex 0 to vertex 5.
Constraints:
•
1 <= n <= 2 * 10^5•
0 <= edges.length <= 2 * 10^5•
edges[i].length == 2•
0 <= u_i, v_i <= n - 1•
u_i != v_i•
0 <= source, destination <= n - 1• There are no duplicate edges.
• There are no self edges.