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.
2024-04-22
752. Open the Lock
Topic: Array, Hash Table, String, Breadth-First Search
Difficulty: Medium
Problem:
You have a lock in front of you with 4 circular wheels. Each wheel has 10 slots:
The lock initially starts at
You are given a list of
Given a
Example 1:
Example 2:
Example 3:
Constraints:
•
•
•
• target will not be in the list
•
752. Open the Lock
Topic: Array, Hash Table, String, Breadth-First Search
Difficulty: Medium
Problem:
You have a lock in front of you with 4 circular wheels. Each wheel has 10 slots:
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'. The wheels can rotate freely and wrap around: for example we can turn '9' to be '0', or '0' to be '9'. Each move consists of turning one wheel one slot.The lock initially starts at
'0000', a string representing the state of the 4 wheels.You are given a list of
deadends dead ends, meaning if the lock displays any of these codes, the wheels of the lock will stop turning and you will be unable to open it.Given a
target representing the value of the wheels that will unlock the lock, return the minimum total number of turns required to open the lock, or -1 if it is impossible.Example 1:
Input: deadends = ["0201","0101","0102","1212","2002"], target = "0202"
Output: 6
Explanation:
A sequence of valid moves would be "0000" -> "1000" -> "1100" -> "1200" -> "1201" -> "1202" -> "0202".
Note that a sequence like "0000" -> "0001" -> "0002" -> "0102" -> "0202" would be invalid,
because the wheels of the lock become stuck after the display becomes the dead end "0102".
Example 2:
Input: deadends = ["8888"], target = "0009"
Output: 1
Explanation: We can turn the last wheel in reverse to move from "0000" -> "0009".
Example 3:
Input: deadends = ["8887","8889","8878","8898","8788","8988","7888","9888"], target = "8888"
Output: -1
Explanation: We cannot reach the target without getting stuck.
Constraints:
•
1 <= deadends.length <= 500•
deadends[i].length == 4•
target.length == 4• target will not be in the list
deadends.•
target and deadends[i] consist of digits only.2024-04-23
310. Minimum Height Trees
Topic: Depth-First Search, Breadth-First Search, Graph, Topological Sort
Difficulty: Medium
Problem:
A tree is an undirected graph in which any two vertices are connected by exactly one path. In other words, any connected graph without simple cycles is a tree.
Given a tree of
Return a list of all MHTs' root labels. You can return the answer in any order.
The height of a rooted tree is the number of edges on the longest downward path between the root and a leaf.
Example 1:
Image: https://assets.leetcode.com/uploads/2020/09/01/e1.jpg
Example 2:
Image: https://assets.leetcode.com/uploads/2020/09/01/e2.jpg
Constraints:
•
•
•
•
• All the pairs
• The given input is guaranteed to be a tree and there will be no repeated edges.
310. Minimum Height Trees
Topic: Depth-First Search, Breadth-First Search, Graph, Topological Sort
Difficulty: Medium
Problem:
A tree is an undirected graph in which any two vertices are connected by exactly one path. In other words, any connected graph without simple cycles is a tree.
Given a tree of
n nodes labelled from 0 to n - 1, and an array of n - 1 edges where edges[i] = [a_i, b_i] indicates that there is an undirected edge between the two nodes a_i and b_i in the tree, you can choose any node of the tree as the root. When you select a node x as the root, the result tree has height h. Among all possible rooted trees, those with minimum height (i.e. min(h)) are called minimum height trees (MHTs).Return a list of all MHTs' root labels. You can return the answer in any order.
The height of a rooted tree is the number of edges on the longest downward path between the root and a leaf.
Example 1:
Image: https://assets.leetcode.com/uploads/2020/09/01/e1.jpg
Input: n = 4, edges = [[1,0],[1,2],[1,3]]
Output: [1]
Explanation: As shown, the height of the tree is 1 when the root is the node with label 1 which is the only MHT.
Example 2:
Image: https://assets.leetcode.com/uploads/2020/09/01/e2.jpg
Input: n = 6, edges = [[3,0],[3,1],[3,2],[3,4],[5,4]]
Output: [3,4]
Constraints:
•
1 <= n <= 2 * 10^4•
edges.length == n - 1•
0 <= a_i, b_i < n•
a_i != b_i• All the pairs
(a_i, b_i) are distinct.• The given input is guaranteed to be a tree and there will be no repeated edges.
2024-04-25
2370. Longest Ideal Subsequence
Topic: Hash Table, String, Dynamic Programming
Difficulty: Medium
Problem:
You are given a string
•
• The absolute difference in the alphabet order of every two adjacent letters in
Return the length of the longest ideal string.
A subsequence is a string that can be derived from another string by deleting some or no characters without changing the order of the remaining characters.
Note that the alphabet order is not cyclic. For example, the absolute difference in the alphabet order of
Example 1:
Example 2:
Constraints:
•
•
•
2370. Longest Ideal Subsequence
Topic: Hash Table, String, Dynamic Programming
Difficulty: Medium
Problem:
You are given a string
s consisting of lowercase letters and an integer k. We call a string t ideal if the following conditions are satisfied:•
t is a subsequence of the string s.• The absolute difference in the alphabet order of every two adjacent letters in
t is less than or equal to k.Return the length of the longest ideal string.
A subsequence is a string that can be derived from another string by deleting some or no characters without changing the order of the remaining characters.
Note that the alphabet order is not cyclic. For example, the absolute difference in the alphabet order of
'a' and 'z' is 25, not 1.Example 1:
Input: s = "acfgbd", k = 2
Output: 4
Explanation: The longest ideal string is "acbd". The length of this string is 4, so 4 is returned.
Note that "acfgbd" is not ideal because 'c' and 'f' have a difference of 3 in alphabet order.
Example 2:
Input: s = "abcd", k = 3
Output: 4
Explanation: The longest ideal string is "abcd". The length of this string is 4, so 4 is returned.
Constraints:
•
1 <= s.length <= 10^5•
0 <= k <= 25•
s consists of lowercase English letters.2024-04-26
1289. Minimum Falling Path Sum II
Topic: Array, Dynamic Programming, Matrix
Difficulty: Hard
Problem:
Given an
A falling path with non-zero shifts is a choice of exactly one element from each row of
Example 1:
Image: https://assets.leetcode.com/uploads/2021/08/10/falling-grid.jpg
Example 2:
Constraints:
•
•
•
1289. Minimum Falling Path Sum II
Topic: Array, Dynamic Programming, Matrix
Difficulty: Hard
Problem:
Given an
n x n integer matrix grid, return the minimum sum of a falling path with non-zero shifts.A falling path with non-zero shifts is a choice of exactly one element from each row of
grid such that no two elements chosen in adjacent rows are in the same column.Example 1:
Image: https://assets.leetcode.com/uploads/2021/08/10/falling-grid.jpg
Input: grid = [[1,2,3],[4,5,6],[7,8,9]]
Output: 13
Explanation:
The possible falling paths are:
[1,5,9], [1,5,7], [1,6,7], [1,6,8],
[2,4,8], [2,4,9], [2,6,7], [2,6,8],
[3,4,8], [3,4,9], [3,5,7], [3,5,9]
The falling path with the smallest sum is [1,5,7], so the answer is 13.
Example 2:
Input: grid = [[7]]
Output: 7
Constraints:
•
n == grid.length == grid[i].length•
1 <= n <= 200•
-99 <= grid[i][j] <= 99