2024-10-06
1813. Sentence Similarity III
Topic: Array, Two Pointers, String
Difficulty: Medium
Problem:
You are given two strings
Two sentences
For example,
•
•
Given two sentences
Example 1:
Input: sentence1 = "My name is Haley", sentence2 = "My Haley"
Output: true
Explanation:
Example 2:
Input: sentence1 = "of", sentence2 = "A lot of words"
Output: false
Explanation:
No single sentence can be inserted inside one of the sentences to make it equal to the other.
Example 3:
Input: sentence1 = "Eating right now", sentence2 = "Eating"
Output: true
Explanation:
Constraints:
•
•
• The words in
1813. Sentence Similarity III
Topic: Array, Two Pointers, String
Difficulty: Medium
Problem:
You are given two strings
sentence1 and sentence2, each representing a sentence composed of words. A sentence is a list of words that are separated by a single space with no leading or trailing spaces. Each word consists of only uppercase and lowercase English characters.Two sentences
s1 and s2 are considered similar if it is possible to insert an arbitrary sentence (possibly empty) inside one of these sentences such that the two sentences become equal. Note that the inserted sentence must be separated from existing words by spaces.For example,
•
s1 = "Hello Jane" and s2 = "Hello my name is Jane" can be made equal by inserting "my name is" between "Hello" and "Jane" in s1.•
s1 = "Frog cool" and s2 = "Frogs are cool" are not similar, since although there is a sentence "s are" inserted into s1, it is not separated from "Frog" by a space.Given two sentences
sentence1 and sentence2, return true if sentence1 and sentence2 are similar. Otherwise, return false.Example 1:
Input: sentence1 = "My name is Haley", sentence2 = "My Haley"
Output: true
Explanation:
sentence2 can be turned to sentence1 by inserting "name is" between "My" and "Haley".Example 2:
Input: sentence1 = "of", sentence2 = "A lot of words"
Output: false
Explanation:
No single sentence can be inserted inside one of the sentences to make it equal to the other.
Example 3:
Input: sentence1 = "Eating right now", sentence2 = "Eating"
Output: true
Explanation:
sentence2 can be turned to sentence1 by inserting "right now" at the end of the sentence.Constraints:
•
1 <= sentence1.length, sentence2.length <= 100•
sentence1 and sentence2 consist of lowercase and uppercase English letters and spaces.• The words in
sentence1 and sentence2 are separated by a single space.2024-10-07
2696. Minimum String Length After Removing Substrings
Topic: String, Stack, Simulation
Difficulty: Easy
Problem:
You are given a string
You can apply some operations to this string where, in one operation, you can remove any occurrence of one of the substrings
Return the minimum possible length of the resulting string that you can obtain.
Note that the string concatenates after removing the substring and could produce new
Example 1:
Example 2:
Constraints:
•
•
2696. Minimum String Length After Removing Substrings
Topic: String, Stack, Simulation
Difficulty: Easy
Problem:
You are given a string
s consisting only of uppercase English letters.You can apply some operations to this string where, in one operation, you can remove any occurrence of one of the substrings
"AB" or "CD" from s.Return the minimum possible length of the resulting string that you can obtain.
Note that the string concatenates after removing the substring and could produce new
"AB" or "CD" substrings.Example 1:
Input: s = "ABFCACDB"
Output: 2
Explanation: We can do the following operations:
- Remove the substring "ABFCACDB", so s = "FCACDB".
- Remove the substring "FCACDB", so s = "FCAB".
- Remove the substring "FCAB", so s = "FC".
So the resulting length of the string is 2.
It can be shown that it is the minimum length that we can obtain.
Example 2:
Input: s = "ACBBD"
Output: 5
Explanation: We cannot do any operations on the string so the length remains the same.
Constraints:
•
1 <= s.length <= 100•
s consists only of uppercase English letters.2024-10-08
1963. Minimum Number of Swaps to Make the String Balanced
Topic: Two Pointers, String, Stack, Greedy
Difficulty: Medium
Problem:
You are given a 0-indexed string
A string is called balanced if and only if:
• It is the empty string, or
• It can be written as
• It can be written as
You may swap the brackets at any two indices any number of times.
Return the minimum number of swaps to make
Example 1:
Example 2:
Example 3:
Constraints:
•
•
•
•
• The number of opening brackets
1963. Minimum Number of Swaps to Make the String Balanced
Topic: Two Pointers, String, Stack, Greedy
Difficulty: Medium
Problem:
You are given a 0-indexed string
s of even length n. The string consists of exactly n / 2 opening brackets '[' and n / 2 closing brackets ']'.A string is called balanced if and only if:
• It is the empty string, or
• It can be written as
AB, where both A and B are balanced strings, or• It can be written as
[C], where C is a balanced string.You may swap the brackets at any two indices any number of times.
Return the minimum number of swaps to make
s balanced.Example 1:
Input: s = "][]["
Output: 1
Explanation: You can make the string balanced by swapping index 0 with index 3.
The resulting string is "[[]]".
Example 2:
Input: s = "]]][[["
Output: 2
Explanation: You can do the following to make the string balanced:
- Swap index 0 with index 4. s = "[]][][".
- Swap index 1 with index 5. s = "[[][]]".
The resulting string is "[[][]]".
Example 3:
Input: s = "[]"
Output: 0
Explanation: The string is already balanced.
Constraints:
•
n == s.length•
2 <= n <= 10^6•
n is even.•
s[i] is either '[' or ']'.• The number of opening brackets
'[' equals n / 2, and the number of closing brackets ']' equals n / 2.2024-10-09
921. Minimum Add to Make Parentheses Valid
Topic: String, Stack, Greedy
Difficulty: Medium
Problem:
A parentheses string is valid if and only if:
• It is the empty string,
• It can be written as
• It can be written as
You are given a parentheses string
• For example, if
Return the minimum number of moves required to make
Example 1:
Example 2:
Constraints:
•
•
921. Minimum Add to Make Parentheses Valid
Topic: String, Stack, Greedy
Difficulty: Medium
Problem:
A parentheses string is valid if and only if:
• It is the empty string,
• 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.You are given a parentheses string
s. In one move, you can insert a parenthesis at any position of the string.• For example, if
s = "()))", you can insert an opening parenthesis to be "(()))" or a closing parenthesis to be "())))".Return the minimum number of moves required to make
s valid.Example 1:
Input: s = "())"
Output: 1
Example 2:
Input: s = "((("
Output: 3
Constraints:
•
1 <= s.length <= 1000•
s[i] is either '(' or ')'.2024-10-10
962. Maximum Width Ramp
Topic: Array, Stack, Monotonic Stack
Difficulty: Medium
Problem:
A ramp in an integer array
Given an integer array
Example 1:
Example 2:
Constraints:
•
•
962. Maximum Width Ramp
Topic: Array, Stack, Monotonic Stack
Difficulty: Medium
Problem:
A ramp in an integer array
nums is a pair (i, j) for which i < j and nums[i] <= nums[j]. The width of such a ramp is j - i.Given an integer array
nums, return the maximum width of a ramp in nums. If there is no ramp in nums, return 0.Example 1:
Input: nums = [6,0,8,2,1,5]
Output: 4
Explanation: The maximum width ramp is achieved at (i, j) = (1, 5): nums[1] = 0 and nums[5] = 5.
Example 2:
Input: nums = [9,8,1,0,1,9,4,0,4,1]
Output: 7
Explanation: The maximum width ramp is achieved at (i, j) = (2, 9): nums[2] = 1 and nums[9] = 1.
Constraints:
•
2 <= nums.length <= 5 * 10^4•
0 <= nums[i] <= 5 * 10^42024-10-11
1942. The Number of the Smallest Unoccupied Chair
Topic: Array, Hash Table, Heap (Priority Queue)
Difficulty: Medium
Problem:
There is a party where
• For example, if chairs
When a friend leaves the party, their chair becomes unoccupied at the moment they leave. If another friend arrives at that same moment, they can sit in that chair.
You are given a 0-indexed 2D integer array
Return the chair number that the friend numbered
Example 1:
Example 2:
Constraints:
•
•
•
•
•
• Each
1942. The Number of the Smallest Unoccupied Chair
Topic: Array, Hash Table, Heap (Priority Queue)
Difficulty: Medium
Problem:
There is a party where
n friends numbered from 0 to n - 1 are attending. There is an infinite number of chairs in this party that are numbered from 0 to infinity. When a friend arrives at the party, they sit on the unoccupied chair with the smallest number.• For example, if chairs
0, 1, and 5 are occupied when a friend comes, they will sit on chair number 2.When a friend leaves the party, their chair becomes unoccupied at the moment they leave. If another friend arrives at that same moment, they can sit in that chair.
You are given a 0-indexed 2D integer array
times where times[i] = [arrival_i, leaving_i], indicating the arrival and leaving times of the i^th friend respectively, and an integer targetFriend. All arrival times are distinct.Return the chair number that the friend numbered
targetFriend will sit on.Example 1:
Input: times = [[1,4],[2,3],[4,6]], targetFriend = 1
Output: 1
Explanation:
- Friend 0 arrives at time 1 and sits on chair 0.
- Friend 1 arrives at time 2 and sits on chair 1.
- Friend 1 leaves at time 3 and chair 1 becomes empty.
- Friend 0 leaves at time 4 and chair 0 becomes empty.
- Friend 2 arrives at time 4 and sits on chair 0.
Since friend 1 sat on chair 1, we return 1.
Example 2:
Input: times = [[3,10],[1,5],[2,6]], targetFriend = 0
Output: 2
Explanation:
- Friend 1 arrives at time 1 and sits on chair 0.
- Friend 2 arrives at time 2 and sits on chair 1.
- Friend 0 arrives at time 3 and sits on chair 2.
- Friend 1 leaves at time 5 and chair 0 becomes empty.
- Friend 2 leaves at time 6 and chair 1 becomes empty.
- Friend 0 leaves at time 10 and chair 2 becomes empty.
Since friend 0 sat on chair 2, we return 2.
Constraints:
•
n == times.length•
2 <= n <= 10^4•
times[i].length == 2•
1 <= arrival_i < leaving_i <= 10^5•
0 <= targetFriend <= n - 1• Each
arrival_i time is distinct.2024-10-12
2406. Divide Intervals Into Minimum Number of Groups
Topic: Array, Two Pointers, Greedy, Sorting, Heap (Priority Queue), Prefix Sum
Difficulty: Medium
Problem:
You are given a 2D integer array
You have to divide the intervals into one or more groups such that each interval is in exactly one group, and no two intervals that are in the same group intersect each other.
Return the minimum number of groups you need to make.
Two intervals intersect if there is at least one common number between them. For example, the intervals
Example 1:
Example 2:
Constraints:
•
•
•
2406. Divide Intervals Into Minimum Number of Groups
Topic: Array, Two Pointers, Greedy, Sorting, Heap (Priority Queue), Prefix Sum
Difficulty: Medium
Problem:
You are given a 2D integer array
intervals where intervals[i] = [left_i, right_i] represents the inclusive interval [left_i, right_i].You have to divide the intervals into one or more groups such that each interval is in exactly one group, and no two intervals that are in the same group intersect each other.
Return the minimum number of groups you need to make.
Two intervals intersect if there is at least one common number between them. For example, the intervals
[1, 5] and [5, 8] intersect.Example 1:
Input: intervals = [[5,10],[6,8],[1,5],[2,3],[1,10]]
Output: 3
Explanation: We can divide the intervals into the following groups:
- Group 1: [1, 5], [6, 8].
- Group 2: [2, 3], [5, 10].
- Group 3: [1, 10].
It can be proven that it is not possible to divide the intervals into fewer than 3 groups.
Example 2:
Input: intervals = [[1,3],[5,6],[8,10],[11,13]]
Output: 1
Explanation: None of the intervals overlap, so we can put all of them in one group.
Constraints:
•
1 <= intervals.length <= 10^5•
intervals[i].length == 2•
1 <= left_i <= right_i <= 10^62024-10-13
632. Smallest Range Covering Elements from K Lists
Topic: Array, Hash Table, Greedy, Sliding Window, Sorting, Heap (Priority Queue)
Difficulty: Hard
Problem:
You have
We define the range
Example 1:
Example 2:
Constraints:
•
•
•
•
•
632. Smallest Range Covering Elements from K Lists
Topic: Array, Hash Table, Greedy, Sliding Window, Sorting, Heap (Priority Queue)
Difficulty: Hard
Problem:
You have
k lists of sorted integers in non-decreasing order. Find the smallest range that includes at least one number from each of the k lists.We define the range
[a, b] is smaller than range [c, d] if b - a < d - c or a < c if b - a == d - c.Example 1:
Input: nums = [[4,10,15,24,26],[0,9,12,20],[5,18,22,30]]
Output: [20,24]
Explanation:
List 1: [4, 10, 15, 24,26], 24 is in range [20,24].
List 2: [0, 9, 12, 20], 20 is in range [20,24].
List 3: [5, 18, 22, 30], 22 is in range [20,24].
Example 2:
Input: nums = [[1,2,3],[1,2,3],[1,2,3]]
Output: [1,1]
Constraints:
•
nums.length == k•
1 <= k <= 3500•
1 <= nums[i].length <= 50•
-10^5 <= nums[i][j] <= 10^5•
nums[i] is sorted in non-decreasing order.2024-10-14
2530. Maximal Score After Applying K Operations
Topic: Array, Greedy, Heap (Priority Queue)
Difficulty: Medium
Problem:
You are given a 0-indexed integer array
In one operation:
1. choose an index
2. increase your score by
3. replace
Return the maximum possible score you can attain after applying exactly
The ceiling function
Example 1:
Example 2:
Constraints:
•
•
2530. Maximal Score After Applying K Operations
Topic: Array, Greedy, Heap (Priority Queue)
Difficulty: Medium
Problem:
You are given a 0-indexed integer array
nums and an integer k. You have a starting score of 0.In one operation:
1. choose an index
i such that 0 <= i < nums.length,2. increase your score by
nums[i], and3. replace
nums[i] with ceil(nums[i] / 3).Return the maximum possible score you can attain after applying exactly
k operations.The ceiling function
ceil(val) is the least integer greater than or equal to val.Example 1:
Input: nums = [10,10,10,10,10], k = 5
Output: 50
Explanation: Apply the operation to each array element exactly once. The final score is 10 + 10 + 10 + 10 + 10 = 50.
Example 2:
Input: nums = [1,10,3,3,3], k = 3
Output: 17
Explanation: You can do the following operations:
Operation 1: Select i = 1, so nums becomes [1,4,3,3,3]. Your score increases by 10.
Operation 2: Select i = 1, so nums becomes [1,2,3,3,3]. Your score increases by 4.
Operation 3: Select i = 2, so nums becomes [1,1,1,3,3]. Your score increases by 3.
The final score is 10 + 4 + 3 = 17.
Constraints:
•
1 <= nums.length, k <= 10^5•
1 <= nums[i] <= 10^92024-10-15
2938. Separate Black and White Balls
Topic: Two Pointers, String, Greedy
Difficulty: Medium
Problem:
There are
You are given a 0-indexed binary string
In each step, you can choose two adjacent balls and swap them.
Return the minimum number of steps to group all the black balls to the right and all the white balls to the left.
Example 1:
Example 2:
Example 3:
Constraints:
•
•
2938. Separate Black and White Balls
Topic: Two Pointers, String, Greedy
Difficulty: Medium
Problem:
There are
n balls on a table, each ball has a color black or white.You are given a 0-indexed binary string
s of length n, where 1 and 0 represent black and white balls, respectively.In each step, you can choose two adjacent balls and swap them.
Return the minimum number of steps to group all the black balls to the right and all the white balls to the left.
Example 1:
Input: s = "101"
Output: 1
Explanation: We can group all the black balls to the right in the following way:
- Swap s[0] and s[1], s = "011".
Initially, 1s are not grouped together, requiring at least 1 step to group them to the right.
Example 2:
Input: s = "100"
Output: 2
Explanation: We can group all the black balls to the right in the following way:
- Swap s[0] and s[1], s = "010".
- Swap s[1] and s[2], s = "001".
It can be proven that the minimum number of steps needed is 2.
Example 3:
Input: s = "0111"
Output: 0
Explanation: All the black balls are already grouped to the right.
Constraints:
•
1 <= n == s.length <= 10^5•
s[i] is either '0' or '1'.2024-10-16
1405. Longest Happy String
Topic: String, Greedy, Heap (Priority Queue)
Difficulty: Medium
Problem:
A string
•
•
•
•
•
Given three integers
A substring is a contiguous sequence of characters within a string.
Example 1:
Example 2:
Constraints:
•
•
1405. Longest Happy String
Topic: String, Greedy, Heap (Priority Queue)
Difficulty: Medium
Problem:
A string
s is called happy if it satisfies the following conditions:•
s only contains the letters 'a', 'b', and 'c'.•
s does not contain any of "aaa", "bbb", or "ccc" as a substring.•
s contains at most a occurrences of the letter 'a'.•
s contains at most b occurrences of the letter 'b'.•
s contains at most c occurrences of the letter 'c'.Given three integers
a, b, and c, return the longest possible happy string. If there are multiple longest happy strings, return any of them. If there is no such string, return the empty string "".A substring is a contiguous sequence of characters within a string.
Example 1:
Input: a = 1, b = 1, c = 7
Output: "ccaccbcc"
Explanation: "ccbccacc" would also be a correct answer.
Example 2:
Input: a = 7, b = 1, c = 0
Output: "aabaa"
Explanation: It is the only correct answer in this case.
Constraints:
•
0 <= a, b, c <= 100•
a + b + c > 02024-10-17
670. Maximum Swap
Topic: Math, Greedy
Difficulty: Medium
Problem:
You are given an integer
Return the maximum valued number you can get.
Example 1:
Example 2:
Constraints:
•
670. Maximum Swap
Topic: Math, Greedy
Difficulty: Medium
Problem:
You are given an integer
num. You can swap two digits at most once to get the maximum valued number.Return the maximum valued number you can get.
Example 1:
Input: num = 2736
Output: 7236
Explanation: Swap the number 2 and the number 7.
Example 2:
Input: num = 9973
Output: 9973
Explanation: No swap.
Constraints:
•
0 <= num <= 10^82024-10-18
2044. Count Number of Maximum Bitwise-OR Subsets
Topic: Array, Backtracking, Bit Manipulation, Enumeration
Difficulty: Medium
Problem:
Given an integer array
An array
The bitwise OR of an array
Example 1:
Example 2:
Example 3:
Constraints:
•
•
2044. Count Number of Maximum Bitwise-OR Subsets
Topic: Array, Backtracking, Bit Manipulation, Enumeration
Difficulty: Medium
Problem:
Given an integer array
nums, find the maximum possible bitwise OR of a subset of nums and return the number of different non-empty subsets with the maximum bitwise OR.An array
a is a subset of an array b if a can be obtained from b by deleting some (possibly zero) elements of b. Two subsets are considered different if the indices of the elements chosen are different.The bitwise OR of an array
a is equal to a[0] OR a[1] OR ... OR a[a.length - 1] (0-indexed).Example 1:
Input: nums = [3,1]
Output: 2
Explanation: The maximum possible bitwise OR of a subset is 3. There are 2 subsets with a bitwise OR of 3:
- [3]
- [3,1]
Example 2:
Input: nums = [2,2,2]
Output: 7
Explanation: All non-empty subsets of [2,2,2] have a bitwise OR of 2. There are 2^3 - 1 = 7 total subsets.
Example 3:
Input: nums = [3,2,1,5]
Output: 6
Explanation: The maximum possible bitwise OR of a subset is 7. There are 6 subsets with a bitwise OR of 7:
- [3,5]
- [3,1,5]
- [3,2,5]
- [3,2,1,5]
- [2,5]
- [2,1,5]
Constraints:
•
1 <= nums.length <= 16•
1 <= nums[i] <= 10^52024-10-19
1545. Find Kth Bit in Nth Binary String
Topic: String, Recursion, Simulation
Difficulty: Medium
Problem:
Given two positive integers
•
•
Where
For example, the first four strings in the above sequence are:
•
•
•
•
Return the
Example 1:
Example 2:
Constraints:
•
•
1545. Find Kth Bit in Nth Binary String
Topic: String, Recursion, Simulation
Difficulty: Medium
Problem:
Given two positive integers
n and k, the binary string S_n is formed as follows:•
S_1 = "0"•
S_i = S_i - 1 + "1" + reverse(invert(S_i - 1)) for i > 1Where
+ denotes the concatenation operation, reverse(x) returns the reversed string x, and invert(x) inverts all the bits in x (0 changes to 1 and 1 changes to 0).For example, the first four strings in the above sequence are:
•
S_1 = "0"•
S_2 = "011"•
S_3 = "0111001"•
S_4 = "011100110110001"Return the
k^th bit in S_n. It is guaranteed that k is valid for the given n.Example 1:
Input: n = 3, k = 1
Output: "0"
Explanation: S_3 is "0111001".
The 1^st bit is "0".
Example 2:
Input: n = 4, k = 11
Output: "1"
Explanation: S_4 is "011100110110001".
The 11^th bit is "1".
Constraints:
•
1 <= n <= 20•
1 <= k <= 2^n - 12024-10-20
1106. Parsing A Boolean Expression
Topic: String, Stack, Recursion
Difficulty: Hard
Problem:
A boolean expression is an expression that evaluates to either
•
•
•
•
•
Given a string
It is guaranteed that the given expression is valid and follows the given rules.
Example 1:
Example 2:
Example 3:
Constraints:
•
• expressioni is one following characters:
1106. Parsing A Boolean Expression
Topic: String, Stack, Recursion
Difficulty: Hard
Problem:
A boolean expression is an expression that evaluates to either
true or false. It can be in one of the following shapes:•
't' that evaluates to true.•
'f' that evaluates to false.•
'!(subExpr)' that evaluates to the logical NOT of the inner expression subExpr.•
'&(subExpr_1, subExpr_2, ..., subExpr_n)' that evaluates to the logical AND of the inner expressions subExpr_1, subExpr_2, ..., subExpr_n where n >= 1.•
'|(subExpr_1, subExpr_2, ..., subExpr_n)' that evaluates to the logical OR of the inner expressions subExpr_1, subExpr_2, ..., subExpr_n where n >= 1.Given a string
expression that represents a boolean expression, return the evaluation of that expression.It is guaranteed that the given expression is valid and follows the given rules.
Example 1:
Input: expression = "&(|(f))"
Output: false
Explanation:
First, evaluate |(f) --> f. The expression is now "&(f)".
Then, evaluate &(f) --> f. The expression is now "f".
Finally, return false.
Example 2:
Input: expression = "|(f,f,f,t)"
Output: true
Explanation: The evaluation of (false OR false OR false OR true) is true.
Example 3:
Input: expression = "!(&(f,t))"
Output: true
Explanation:
First, evaluate &(f,t) --> (false AND true) --> false --> f. The expression is now "!(f)".
Then, evaluate !(f) --> NOT false --> true. We return true.
Constraints:
•
1 <= expression.length <= 2 * 10^4• expressioni is one following characters:
'(', ')', '&', '|', '!', 't', 'f', and ','.2024-10-21
1593. Split a String Into the Max Number of Unique Substrings
Topic: Hash Table, String, Backtracking
Difficulty: Medium
Problem:
Given a string
You can split string
A substring is a contiguous sequence of characters within a string.
Example 1:
Example 2:
Example 3:
Constraints:
•
•
1593. Split a String Into the Max Number of Unique Substrings
Topic: Hash Table, String, Backtracking
Difficulty: Medium
Problem:
Given a string
s, return the maximum number of unique substrings that the given string can be split into.You can split string
s into any list of non-empty substrings, where the concatenation of the substrings forms the original string. However, you must split the substrings such that all of them are unique.A substring is a contiguous sequence of characters within a string.
Example 1:
Input: s = "ababccc"
Output: 5
Explanation: One way to split maximally is ['a', 'b', 'ab', 'c', 'cc']. Splitting like ['a', 'b', 'a', 'b', 'c', 'cc'] is not valid as you have 'a' and 'b' multiple times.
Example 2:
Input: s = "aba"
Output: 2
Explanation: One way to split maximally is ['a', 'ba'].
Example 3:
Input: s = "aa"
Output: 1
Explanation: It is impossible to split the string any further.
Constraints:
•
1 <= s.length <= 16•
s contains only lower case English letters.2024-10-22
2583. Kth Largest Sum in a Binary Tree
Topic: Tree, Breadth-First Search, Sorting, Binary Tree
Difficulty: Medium
Problem:
You are given the
The level sum in the tree is the sum of the values of the nodes that are on the same level.
Return the
Note that two nodes are on the same level if they have the same distance from the root.
Example 1:
Image: https://assets.leetcode.com/uploads/2022/12/14/binaryytreeedrawio-2.png
Example 2:
Image: https://assets.leetcode.com/uploads/2022/12/14/treedrawio-3.png
Constraints:
• The number of nodes in the tree is
•
•
•
2583. Kth Largest Sum in a Binary Tree
Topic: Tree, Breadth-First Search, Sorting, Binary Tree
Difficulty: Medium
Problem:
You are given the
root of a binary tree and a positive integer k.The level sum in the tree is the sum of the values of the nodes that are on the same level.
Return the
k^th largest level sum in the tree (not necessarily distinct). If there are fewer than k levels in the tree, return -1.Note that two nodes are on the same level if they have the same distance from the root.
Example 1:
Image: https://assets.leetcode.com/uploads/2022/12/14/binaryytreeedrawio-2.png
Input: root = [5,8,9,2,1,3,7,4,6], k = 2
Output: 13
Explanation: The level sums are the following:
- Level 1: 5.
- Level 2: 8 + 9 = 17.
- Level 3: 2 + 1 + 3 + 7 = 13.
- Level 4: 4 + 6 = 10.
The 2^nd largest level sum is 13.
Example 2:
Image: https://assets.leetcode.com/uploads/2022/12/14/treedrawio-3.png
Input: root = [1,2,null,3], k = 1
Output: 3
Explanation: The largest level sum is 3.
Constraints:
• The number of nodes in the tree is
n.•
2 <= n <= 10^5•
1 <= Node.val <= 10^6•
1 <= k <= n2024-10-23
2641. Cousins in Binary Tree II
Topic: Hash Table, Tree, Depth-First Search, Breadth-First Search, Binary Tree
Difficulty: Medium
Problem:
Given the
Two nodes of a binary tree are cousins if they have the same depth with different parents.
Return the
Note that the depth of a node is the number of edges in the path from the root node to it.
Example 1:
Image: https://assets.leetcode.com/uploads/2023/01/11/example11.png
Example 2:
Image: https://assets.leetcode.com/uploads/2023/01/11/diagram33.png
Constraints:
• The number of nodes in the tree is in the range
•
2641. Cousins in Binary Tree II
Topic: Hash Table, Tree, Depth-First Search, Breadth-First Search, Binary Tree
Difficulty: Medium
Problem:
Given the
root of a binary tree, replace the value of each node in the tree with the sum of all its cousins' values.Two nodes of a binary tree are cousins if they have the same depth with different parents.
Return the
root of the modified tree.Note that the depth of a node is the number of edges in the path from the root node to it.
Example 1:
Image: https://assets.leetcode.com/uploads/2023/01/11/example11.png
Input: root = [5,4,9,1,10,null,7]
Output: [0,0,0,7,7,null,11]
Explanation: The diagram above shows the initial binary tree and the binary tree after changing the value of each node.
- Node with value 5 does not have any cousins so its sum is 0.
- Node with value 4 does not have any cousins so its sum is 0.
- Node with value 9 does not have any cousins so its sum is 0.
- Node with value 1 has a cousin with value 7 so its sum is 7.
- Node with value 10 has a cousin with value 7 so its sum is 7.
- Node with value 7 has cousins with values 1 and 10 so its sum is 11.
Example 2:
Image: https://assets.leetcode.com/uploads/2023/01/11/diagram33.png
Input: root = [3,1,2]
Output: [0,0,0]
Explanation: The diagram above shows the initial binary tree and the binary tree after changing the value of each node.
- Node with value 3 does not have any cousins so its sum is 0.
- Node with value 1 does not have any cousins so its sum is 0.
- Node with value 2 does not have any cousins so its sum is 0.
Constraints:
• The number of nodes in the tree is in the range
[1, 10^5].•
1 <= Node.val <= 10^42024-10-24
951. Flip Equivalent Binary Trees
Topic: Tree, Depth-First Search, Binary Tree
Difficulty: Medium
Problem:
For a binary tree T, we can define a flip operation as follows: choose any node, and swap the left and right child subtrees.
A binary tree X is flip equivalent to a binary tree Y if and only if we can make X equal to Y after some number of flip operations.
Given the roots of two binary trees
Example 1:
Image: https://assets.leetcode.com/uploads/2018/11/29/tree_ex.png
Example 2:
Example 3:
Constraints:
• The number of nodes in each tree is in the range
• Each tree will have unique node values in the range
951. Flip Equivalent Binary Trees
Topic: Tree, Depth-First Search, Binary Tree
Difficulty: Medium
Problem:
For a binary tree T, we can define a flip operation as follows: choose any node, and swap the left and right child subtrees.
A binary tree X is flip equivalent to a binary tree Y if and only if we can make X equal to Y after some number of flip operations.
Given the roots of two binary trees
root1 and root2, return true if the two trees are flip equivalent or false otherwise.Example 1:
Image: https://assets.leetcode.com/uploads/2018/11/29/tree_ex.png
Input: root1 = [1,2,3,4,5,6,null,null,null,7,8], root2 = [1,3,2,null,6,4,5,null,null,null,null,8,7]
Output: true
Explanation: We flipped at nodes with values 1, 3, and 5.
Example 2:
Input: root1 = [], root2 = []
Output: true
Example 3:
Input: root1 = [], root2 = [1]
Output: false
Constraints:
• The number of nodes in each tree is in the range
[0, 100].• Each tree will have unique node values in the range
[0, 99].2024-10-25
1233. Remove Sub-Folders from the Filesystem
Topic: Array, String, Depth-First Search, Trie
Difficulty: Medium
Problem:
Given a list of folders
If a
The format of a path is one or more concatenated strings of the form:
• For example,
Example 1:
Example 2:
Example 3:
Constraints:
•
•
•
•
• Each folder name is unique.
1233. Remove Sub-Folders from the Filesystem
Topic: Array, String, Depth-First Search, Trie
Difficulty: Medium
Problem:
Given a list of folders
folder, return the folders after removing all sub-folders in those folders. You may return the answer in any order.If a
folder[i] is located within another folder[j], it is called a sub-folder of it. A sub-folder of folder[j] must start with folder[j], followed by a "/". For example, "/a/b" is a sub-folder of "/a", but "/b" is not a sub-folder of "/a/b/c".The format of a path is one or more concatenated strings of the form:
'/' followed by one or more lowercase English letters.• For example,
"/leetcode" and "/leetcode/problems" are valid paths while an empty string and "/" are not.Example 1:
Input: folder = ["/a","/a/b","/c/d","/c/d/e","/c/f"]
Output: ["/a","/c/d","/c/f"]
Explanation: Folders "/a/b" is a subfolder of "/a" and "/c/d/e" is inside of folder "/c/d" in our filesystem.
Example 2:
Input: folder = ["/a","/a/b/c","/a/b/d"]
Output: ["/a"]
Explanation: Folders "/a/b/c" and "/a/b/d" will be removed because they are subfolders of "/a".
Example 3:
Input: folder = ["/a/b/c","/a/b/ca","/a/b/d"]
Output: ["/a/b/c","/a/b/ca","/a/b/d"]
Constraints:
•
1 <= folder.length <= 4 * 10^4•
2 <= folder[i].length <= 100•
folder[i] contains only lowercase letters and '/'.•
folder[i] always starts with the character '/'.• Each folder name is unique.