2024-12-05
2337. Move Pieces to Obtain a String
Topic: Two Pointers, String
Difficulty: Medium
Problem:
You are given two strings
• The characters
• The character
Return
Example 1:
Example 2:
Example 3:
Constraints:
•
•
•
2337. Move Pieces to Obtain a String
Topic: Two Pointers, String
Difficulty: Medium
Problem:
You are given two strings
start and target, both of length n. Each string consists only of the characters 'L', 'R', and '_' where:• The characters
'L' and 'R' represent pieces, where a piece 'L' can move to the left only if there is a blank space directly to its left, and a piece 'R' can move to the right only if there is a blank space directly to its right.• The character
'_' represents a blank space that can be occupied by any of the 'L' or 'R' pieces.Return
true if it is possible to obtain the string target by moving the pieces of the string start any number of times. Otherwise, return false.Example 1:
Input: start = "_L__R__R_", target = "L______RR"
Output: true
Explanation: We can obtain the string target from start by doing the following moves:
- Move the first piece one step to the left, start becomes equal to "L___R__R_".
- Move the last piece one step to the right, start becomes equal to "L___R___R".
- Move the second piece three steps to the right, start becomes equal to "L______RR".
Since it is possible to get the string target from start, we return true.
Example 2:
Input: start = "R_L_", target = "__LR"
Output: false
Explanation: The 'R' piece in the string start can move one step to the right to obtain "_RL_".
After that, no pieces can move anymore, so it is impossible to obtain the string target from start.
Example 3:
Input: start = "_R", target = "R_"
Output: false
Explanation: The piece in the string start can move only to the right, so it is impossible to obtain the string target from start.
Constraints:
•
n == start.length == target.length•
1 <= n <= 10^5•
start and target consist of the characters 'L', 'R', and '_'.2024-12-06
2554. Maximum Number of Integers to Choose From a Range I
Topic: Array, Hash Table, Binary Search, Greedy, Sorting
Difficulty: Medium
Problem:
You are given an integer array
• The chosen integers have to be in the range
• Each integer can be chosen at most once.
• The chosen integers should not be in the array
• The sum of the chosen integers should not exceed
Return the maximum number of integers you can choose following the mentioned rules.
Example 1:
Example 2:
Example 3:
Constraints:
•
•
•
2554. Maximum Number of Integers to Choose From a Range I
Topic: Array, Hash Table, Binary Search, Greedy, Sorting
Difficulty: Medium
Problem:
You are given an integer array
banned and two integers n and maxSum. You are choosing some number of integers following the below rules:• The chosen integers have to be in the range
[1, n].• Each integer can be chosen at most once.
• The chosen integers should not be in the array
banned.• The sum of the chosen integers should not exceed
maxSum.Return the maximum number of integers you can choose following the mentioned rules.
Example 1:
Input: banned = [1,6,5], n = 5, maxSum = 6
Output: 2
Explanation: You can choose the integers 2 and 4.
2 and 4 are from the range [1, 5], both did not appear in banned, and their sum is 6, which did not exceed maxSum.
Example 2:
Input: banned = [1,2,3,4,5,6,7], n = 8, maxSum = 1
Output: 0
Explanation: You cannot choose any integer while following the mentioned conditions.
Example 3:
Input: banned = [11], n = 7, maxSum = 50
Output: 7
Explanation: You can choose the integers 1, 2, 3, 4, 5, 6, and 7.
They are from the range [1, 7], all did not appear in banned, and their sum is 28, which did not exceed maxSum.
Constraints:
•
1 <= banned.length <= 10^4•
1 <= banned[i], n <= 10^4•
1 <= maxSum <= 10^92024-12-07
1760. Minimum Limit of Balls in a Bag
Topic: Array, Binary Search
Difficulty: Medium
Problem:
You are given an integer array
You can perform the following operation at most
• Take any bag of balls and divide it into two new bags with a positive number of balls.
• For example, a bag of
Your penalty is the maximum number of balls in a bag. You want to minimize your penalty after the operations.
Return the minimum possible penalty after performing the operations.
Example 1:
Example 2:
Constraints:
•
•
1760. Minimum Limit of Balls in a Bag
Topic: Array, Binary Search
Difficulty: Medium
Problem:
You are given an integer array
nums where the i^th bag contains nums[i] balls. You are also given an integer maxOperations.You can perform the following operation at most
maxOperations times:• Take any bag of balls and divide it into two new bags with a positive number of balls.
• For example, a bag of
5 balls can become two new bags of 1 and 4 balls, or two new bags of 2 and 3 balls.Your penalty is the maximum number of balls in a bag. You want to minimize your penalty after the operations.
Return the minimum possible penalty after performing the operations.
Example 1:
Input: nums = [9], maxOperations = 2
Output: 3
Explanation:
- Divide the bag with 9 balls into two bags of sizes 6 and 3. [9] -> [6,3].
- Divide the bag with 6 balls into two bags of sizes 3 and 3. [6,3] -> [3,3,3].
The bag with the most number of balls has 3 balls, so your penalty is 3 and you should return 3.
Example 2:
Input: nums = [2,4,8,2], maxOperations = 4
Output: 2
Explanation:
- Divide the bag with 8 balls into two bags of sizes 4 and 4. [2,4,8,2] -> [2,4,4,4,2].
- Divide the bag with 4 balls into two bags of sizes 2 and 2. [2,4,4,4,2] -> [2,2,2,4,4,2].
- Divide the bag with 4 balls into two bags of sizes 2 and 2. [2,2,2,4,4,2] -> [2,2,2,2,2,4,2].
- Divide the bag with 4 balls into two bags of sizes 2 and 2. [2,2,2,2,2,4,2] -> [2,2,2,2,2,2,2,2].
The bag with the most number of balls has 2 balls, so your penalty is 2, and you should return 2.
Constraints:
•
1 <= nums.length <= 10^5•
1 <= maxOperations, nums[i] <= 10^92024-12-08
2054. Two Best Non-Overlapping Events
Topic: Array, Binary Search, Dynamic Programming, Sorting, Heap (Priority Queue)
Difficulty: Medium
Problem:
You are given a 0-indexed 2D integer array of
Return this maximum sum.
Note that the start time and end time is inclusive: that is, you cannot attend two events where one of them starts and the other ends at the same time. More specifically, if you attend an event with end time
Example 1:
Image: https://assets.leetcode.com/uploads/2021/09/21/picture5.png
Example 2:
Image: https://assets.leetcode.com/uploads/2021/09/21/picture1.png
Example 3:
Image: https://assets.leetcode.com/uploads/2021/09/21/picture3.png
Constraints:
•
•
•
•
2054. Two Best Non-Overlapping Events
Topic: Array, Binary Search, Dynamic Programming, Sorting, Heap (Priority Queue)
Difficulty: Medium
Problem:
You are given a 0-indexed 2D integer array of
events where events[i] = [startTime_i, endTime_i, value_i]. The i^th event starts at startTime_i and ends at endTime_i, and if you attend this event, you will receive a value of value_i. You can choose at most two non-overlapping events to attend such that the sum of their values is maximized.Return this maximum sum.
Note that the start time and end time is inclusive: that is, you cannot attend two events where one of them starts and the other ends at the same time. More specifically, if you attend an event with end time
t, the next event must start at or after t + 1.Example 1:
Image: https://assets.leetcode.com/uploads/2021/09/21/picture5.png
Input: events = [[1,3,2],[4,5,2],[2,4,3]]
Output: 4
Explanation: Choose the green events, 0 and 1 for a sum of 2 + 2 = 4.
Example 2:
Image: https://assets.leetcode.com/uploads/2021/09/21/picture1.png
Input: events = [[1,3,2],[4,5,2],[1,5,5]]
Output: 5
Explanation: Choose event 2 for a sum of 5.
Example 3:
Image: https://assets.leetcode.com/uploads/2021/09/21/picture3.png
Input: events = [[1,5,3],[1,5,1],[6,6,5]]
Output: 8
Explanation: Choose events 0 and 2 for a sum of 3 + 5 = 8.
Constraints:
•
2 <= events.length <= 10^5•
events[i].length == 3•
1 <= startTime_i <= endTime_i <= 10^9•
1 <= value_i <= 10^62024-12-09
3152. Special Array II
Topic: Array, Binary Search, Prefix Sum
Difficulty: Medium
Problem:
An array is considered special if every pair of its adjacent elements contains two numbers with different parity.
You are given an array of integer
Return an array of booleans
Example 1:
Input: nums = 3,4,1,2,6, queries = [0,4]
Output: false
Explanation:
The subarray is
Example 2:
Input: nums = 4,3,1,6, queries = [0,2,2,3]
Output: false,true
Explanation:
1. The subarray is
2. The subarray is
Constraints:
•
•
•
•
•
3152. Special Array II
Topic: Array, Binary Search, Prefix Sum
Difficulty: Medium
Problem:
An array is considered special if every pair of its adjacent elements contains two numbers with different parity.
You are given an array of integer
nums and a 2D integer matrix queries, where for queries[i] = [from_i, to_i] your task is to check that subarray nums[from_i..to_i] is special or not.Return an array of booleans
answer such that answer[i] is true if nums[from_i..to_i] is special.Example 1:
Input: nums = 3,4,1,2,6, queries = [0,4]
Output: false
Explanation:
The subarray is
[3,4,1,2,6]. 2 and 6 are both even.Example 2:
Input: nums = 4,3,1,6, queries = [0,2,2,3]
Output: false,true
Explanation:
1. The subarray is
[4,3,1]. 3 and 1 are both odd. So the answer to this query is false.2. The subarray is
[1,6]. There is only one pair: (1,6) and it contains numbers with different parity. So the answer to this query is true.Constraints:
•
1 <= nums.length <= 10^5•
1 <= nums[i] <= 10^5•
1 <= queries.length <= 10^5•
queries[i].length == 2•
0 <= queries[i][0] <= queries[i][1] <= nums.length - 12024-12-10
2981. Find Longest Special Substring That Occurs Thrice I
Topic: Hash Table, String, Binary Search, Sliding Window, Counting
Difficulty: Medium
Problem:
You are given a string
A string is called special if it is made up of only a single character. For example, the string
Return the length of the longest special substring of
A substring is a contiguous non-empty sequence of characters within a string.
Example 1:
Example 2:
Example 3:
Constraints:
•
•
2981. Find Longest Special Substring That Occurs Thrice I
Topic: Hash Table, String, Binary Search, Sliding Window, Counting
Difficulty: Medium
Problem:
You are given a string
s that consists of lowercase English letters.A string is called special if it is made up of only a single character. For example, the string
"abc" is not special, whereas the strings "ddd", "zz", and "f" are special.Return the length of the longest special substring of
s which occurs at least thrice, or -1 if no special substring occurs at least thrice.A substring is a contiguous non-empty sequence of characters within a string.
Example 1:
Input: s = "aaaa"
Output: 2
Explanation: The longest special substring which occurs thrice is "aa": substrings "aaaa", "aaaa", and "aaaa".
It can be shown that the maximum length achievable is 2.
Example 2:
Input: s = "abcdef"
Output: -1
Explanation: There exists no special substring which occurs at least thrice. Hence return -1.
Example 3:
Input: s = "abcaba"
Output: 1
Explanation: The longest special substring which occurs thrice is "a": substrings "abcaba", "abcaba", and "abcaba".
It can be shown that the maximum length achievable is 1.
Constraints:
•
3 <= s.length <= 50•
s consists of only lowercase English letters.2024-12-11
2779. Maximum Beauty of an Array After Applying Operation
Topic: Array, Binary Search, Sliding Window, Sorting
Difficulty: Medium
Problem:
You are given a 0-indexed array
In one operation, you can do the following:
• Choose an index
• Replace
The beauty of the array is the length of the longest subsequence consisting of equal elements.
Return the maximum possible beauty of the array
Note that you can apply the operation to each index only once.
A subsequence of an array is a new array generated from the original array by deleting some elements (possibly none) without changing the order of the remaining elements.
Example 1:
Example 2:
Constraints:
•
•
2779. Maximum Beauty of an Array After Applying Operation
Topic: Array, Binary Search, Sliding Window, Sorting
Difficulty: Medium
Problem:
You are given a 0-indexed array
nums and a non-negative integer k.In one operation, you can do the following:
• Choose an index
i that hasn't been chosen before from the range [0, nums.length - 1].• Replace
nums[i] with any integer from the range [nums[i] - k, nums[i] + k].The beauty of the array is the length of the longest subsequence consisting of equal elements.
Return the maximum possible beauty of the array
nums after applying the operation any number of times.Note that you can apply the operation to each index only once.
A subsequence of an array is a new array generated from the original array by deleting some elements (possibly none) without changing the order of the remaining elements.
Example 1:
Input: nums = [4,6,1,2], k = 2
Output: 3
Explanation: In this example, we apply the following operations:
- Choose index 1, replace it with 4 (from range [4,8]), nums = [4,4,1,2].
- Choose index 3, replace it with 4 (from range [0,4]), nums = [4,4,1,4].
After the applied operations, the beauty of the array nums is 3 (subsequence consisting of indices 0, 1, and 3).
It can be proven that 3 is the maximum possible length we can achieve.
Example 2:
Input: nums = [1,1,1,1], k = 10
Output: 4
Explanation: In this example we don't have to apply any operations.
The beauty of the array nums is 4 (whole array).
Constraints:
•
1 <= nums.length <= 10^5•
0 <= nums[i], k <= 10^52024-12-12
2558. Take Gifts From the Richest Pile
Topic: Array, Heap (Priority Queue), Simulation
Difficulty: Easy
Problem:
You are given an integer array
• Choose the pile with the maximum number of gifts.
• If there is more than one pile with the maximum number of gifts, choose any.
• Leave behind the floor of the square root of the number of gifts in the pile. Take the rest of the gifts.
Return the number of gifts remaining after
Example 1:
Example 2:
Constraints:
•
•
•
2558. Take Gifts From the Richest Pile
Topic: Array, Heap (Priority Queue), Simulation
Difficulty: Easy
Problem:
You are given an integer array
gifts denoting the number of gifts in various piles. Every second, you do the following:• Choose the pile with the maximum number of gifts.
• If there is more than one pile with the maximum number of gifts, choose any.
• Leave behind the floor of the square root of the number of gifts in the pile. Take the rest of the gifts.
Return the number of gifts remaining after
k seconds.Example 1:
Input: gifts = [25,64,9,4,100], k = 4
Output: 29
Explanation:
The gifts are taken in the following way:
- In the first second, the last pile is chosen and 10 gifts are left behind.
- Then the second pile is chosen and 8 gifts are left behind.
- After that the first pile is chosen and 5 gifts are left behind.
- Finally, the last pile is chosen again and 3 gifts are left behind.
The final remaining gifts are [5,8,9,4,3], so the total number of gifts remaining is 29.
Example 2:
Input: gifts = [1,1,1,1], k = 4
Output: 4
Explanation:
In this case, regardless which pile you choose, you have to leave behind 1 gift in each pile.
That is, you can't take any pile with you.
So, the total gifts remaining are 4.
Constraints:
•
1 <= gifts.length <= 10^3•
1 <= gifts[i] <= 10^9•
1 <= k <= 10^32024-12-13
2593. Find Score of an Array After Marking All Elements
Topic: Array, Hash Table, Sorting, Heap (Priority Queue), Simulation
Difficulty: Medium
Problem:
You are given an array
Starting with
• Choose the smallest integer of the array that is not marked. If there is a tie, choose the one with the smallest index.
• Add the value of the chosen integer to
• Mark the chosen element and its two adjacent elements if they exist.
• Repeat until all the array elements are marked.
Return the score you get after applying the above algorithm.
Example 1:
Example 2:
Constraints:
•
•
2593. Find Score of an Array After Marking All Elements
Topic: Array, Hash Table, Sorting, Heap (Priority Queue), Simulation
Difficulty: Medium
Problem:
You are given an array
nums consisting of positive integers.Starting with
score = 0, apply the following algorithm:• Choose the smallest integer of the array that is not marked. If there is a tie, choose the one with the smallest index.
• Add the value of the chosen integer to
score.• Mark the chosen element and its two adjacent elements if they exist.
• Repeat until all the array elements are marked.
Return the score you get after applying the above algorithm.
Example 1:
Input: nums = [2,1,3,4,5,2]
Output: 7
Explanation: We mark the elements as follows:
- 1 is the smallest unmarked element, so we mark it and its two adjacent elements: [2,1,3,4,5,2].
- 2 is the smallest unmarked element, so we mark it and its left adjacent element: [2,1,3,4,5,2].
- 4 is the only remaining unmarked element, so we mark it: [2,1,3,4,5,2].
Our score is 1 + 2 + 4 = 7.
Example 2:
Input: nums = [2,3,5,1,3,2]
Output: 5
Explanation: We mark the elements as follows:
- 1 is the smallest unmarked element, so we mark it and its two adjacent elements: [2,3,5,1,3,2].
- 2 is the smallest unmarked element, since there are two of them, we choose the left-most one, so we mark the one at index 0 and its right adjacent element: [2,3,5,1,3,2].
- 2 is the only remaining unmarked element, so we mark it: [2,3,5,1,3,2].
Our score is 1 + 2 + 2 = 5.
Constraints:
•
1 <= nums.length <= 10^5•
1 <= nums[i] <= 10^62024-12-14
2762. Continuous Subarrays
Topic: Array, Queue, Sliding Window, Heap (Priority Queue), Ordered Set, Monotonic Queue
Difficulty: Medium
Problem:
You are given a 0-indexed integer array
• Let
Return the total number of continuous subarrays.
A subarray is a contiguous non-empty sequence of elements within an array.
Example 1:
Example 2:
Constraints:
•
•
2762. Continuous Subarrays
Topic: Array, Queue, Sliding Window, Heap (Priority Queue), Ordered Set, Monotonic Queue
Difficulty: Medium
Problem:
You are given a 0-indexed integer array
nums. A subarray of nums is called continuous if:• Let
i, i + 1, ..., j be the indices in the subarray. Then, for each pair of indices i <= i_1, i_2 <= j, 0 <= |nums[i_1] - nums[i_2]| <= 2.Return the total number of continuous subarrays.
A subarray is a contiguous non-empty sequence of elements within an array.
Example 1:
Input: nums = [5,4,2,4]
Output: 8
Explanation:
Continuous subarray of size 1: [5], [4], [2], [4].
Continuous subarray of size 2: [5,4], [4,2], [2,4].
Continuous subarray of size 3: [4,2,4].
Thereare no subarrys of size 4.
Total continuous subarrays = 4 + 3 + 1 = 8.
It can be shown that there are no more continuous subarrays.
Example 2:
Input: nums = [1,2,3]
Output: 6
Explanation:
Continuous subarray of size 1: [1], [2], [3].
Continuous subarray of size 2: [1,2], [2,3].
Continuous subarray of size 3: [1,2,3].
Total continuous subarrays = 3 + 2 + 1 = 6.
Constraints:
•
1 <= nums.length <= 10^5•
1 <= nums[i] <= 10^92024-12-15
1792. Maximum Average Pass Ratio
Topic: Array, Greedy, Heap (Priority Queue)
Difficulty: Medium
Problem:
There is a school that has classes of students and each class will be having a final exam. You are given a 2D integer array
You are also given an integer
The pass ratio of a class is equal to the number of students of the class that will pass the exam divided by the total number of students of the class. The average pass ratio is the sum of pass ratios of all the classes divided by the number of the classes.
Return the maximum possible average pass ratio after assigning the
Example 1:
Example 2:
Constraints:
•
•
•
•
1792. Maximum Average Pass Ratio
Topic: Array, Greedy, Heap (Priority Queue)
Difficulty: Medium
Problem:
There is a school that has classes of students and each class will be having a final exam. You are given a 2D integer array
classes, where classes[i] = [pass_i, total_i]. You know beforehand that in the i^th class, there are total_i total students, but only pass_i number of students will pass the exam.You are also given an integer
extraStudents. There are another extraStudents brilliant students that are guaranteed to pass the exam of any class they are assigned to. You want to assign each of the extraStudents students to a class in a way that maximizes the average pass ratio across all the classes.The pass ratio of a class is equal to the number of students of the class that will pass the exam divided by the total number of students of the class. The average pass ratio is the sum of pass ratios of all the classes divided by the number of the classes.
Return the maximum possible average pass ratio after assigning the
extraStudents students. Answers within 10^-5 of the actual answer will be accepted.Example 1:
Input: classes = [[1,2],[3,5],[2,2]], extraStudents = 2
Output: 0.78333
Explanation: You can assign the two extra students to the first class. The average pass ratio will be equal to (3/4 + 3/5 + 2/2) / 3 = 0.78333.
Example 2:
Input: classes = [[2,4],[3,9],[4,5],[2,10]], extraStudents = 4
Output: 0.53485
Constraints:
•
1 <= classes.length <= 10^5•
classes[i].length == 2•
1 <= pass_i <= total_i <= 10^5•
1 <= extraStudents <= 10^52024-12-16
3264. Final Array State After K Multiplication Operations I
Topic: Array, Math, Heap (Priority Queue), Simulation
Difficulty: Easy
Problem:
You are given an integer array
You need to perform
• Find the minimum value
• Replace the selected minimum value
Return an integer array denoting the final state of
Example 1:
Input: nums = 2,1,3,5,6, k = 5, multiplier = 2
Output: 8,4,6,5,6
Explanation:
OperationResultAfter operation 12, 2, 3, 5, 6After operation 24, 2, 3, 5, 6After operation 34, 4, 3, 5, 6After operation 44, 4, 6, 5, 6After operation 58, 4, 6, 5, 6
Example 2:
Input: nums = 1,2, k = 3, multiplier = 4
Output: 16,8
Explanation:
OperationResultAfter operation 14, 2After operation 24, 8After operation 316, 8
Constraints:
•
•
•
•
3264. Final Array State After K Multiplication Operations I
Topic: Array, Math, Heap (Priority Queue), Simulation
Difficulty: Easy
Problem:
You are given an integer array
nums, an integer k, and an integer multiplier.You need to perform
k operations on nums. In each operation:• Find the minimum value
x in nums. If there are multiple occurrences of the minimum value, select the one that appears first.• Replace the selected minimum value
x with x * multiplier.Return an integer array denoting the final state of
nums after performing all k operations.Example 1:
Input: nums = 2,1,3,5,6, k = 5, multiplier = 2
Output: 8,4,6,5,6
Explanation:
OperationResultAfter operation 12, 2, 3, 5, 6After operation 24, 2, 3, 5, 6After operation 34, 4, 3, 5, 6After operation 44, 4, 6, 5, 6After operation 58, 4, 6, 5, 6
Example 2:
Input: nums = 1,2, k = 3, multiplier = 4
Output: 16,8
Explanation:
OperationResultAfter operation 14, 2After operation 24, 8After operation 316, 8
Constraints:
•
1 <= nums.length <= 100•
1 <= nums[i] <= 100•
1 <= k <= 10•
1 <= multiplier <= 52024-12-17
2182. Construct String With Repeat Limit
Topic: Hash Table, String, Greedy, Heap (Priority Queue), Counting
Difficulty: Medium
Problem:
You are given a string
Return the lexicographically largest
A string
Example 1:
Example 2:
Constraints:
•
•
2182. Construct String With Repeat Limit
Topic: Hash Table, String, Greedy, Heap (Priority Queue), Counting
Difficulty: Medium
Problem:
You are given a string
s and an integer repeatLimit. Construct a new string repeatLimitedString using the characters of s such that no letter appears more than repeatLimit times in a row. You do not have to use all characters from s.Return the lexicographically largest
repeatLimitedString possible.A string
a is lexicographically larger than a string b if in the first position where a and b differ, string a has a letter that appears later in the alphabet than the corresponding letter in b. If the first min(a.length, b.length) characters do not differ, then the longer string is the lexicographically larger one.Example 1:
Input: s = "cczazcc", repeatLimit = 3
Output: "zzcccac"
Explanation: We use all of the characters from s to construct the repeatLimitedString "zzcccac".
The letter 'a' appears at most 1 time in a row.
The letter 'c' appears at most 3 times in a row.
The letter 'z' appears at most 2 times in a row.
Hence, no letter appears more than repeatLimit times in a row and the string is a valid repeatLimitedString.
The string is the lexicographically largest repeatLimitedString possible so we return "zzcccac".
Note that the string "zzcccca" is lexicographically larger but the letter 'c' appears more than 3 times in a row, so it is not a valid repeatLimitedString.
Example 2:
Input: s = "aababab", repeatLimit = 2
Output: "bbabaa"
Explanation: We use only some of the characters from s to construct the repeatLimitedString "bbabaa".
The letter 'a' appears at most 2 times in a row.
The letter 'b' appears at most 2 times in a row.
Hence, no letter appears more than repeatLimit times in a row and the string is a valid repeatLimitedString.
The string is the lexicographically largest repeatLimitedString possible so we return "bbabaa".
Note that the string "bbabaaa" is lexicographically larger but the letter 'a' appears more than 2 times in a row, so it is not a valid repeatLimitedString.
Constraints:
•
1 <= repeatLimit <= s.length <= 10^5•
s consists of lowercase English letters.2024-12-18
1475. Final Prices With a Special Discount in a Shop
Topic: Array, Stack, Monotonic Stack
Difficulty: Easy
Problem:
You are given an integer array
There is a special discount for items in the shop. If you buy the
Return an integer array
Example 1:
Example 2:
Example 3:
Constraints:
•
•
1475. Final Prices With a Special Discount in a Shop
Topic: Array, Stack, Monotonic Stack
Difficulty: Easy
Problem:
You are given an integer array
prices where prices[i] is the price of the i^th item in a shop.There is a special discount for items in the shop. If you buy the
i^th item, then you will receive a discount equivalent to prices[j] where j is the minimum index such that j > i and prices[j] <= prices[i]. Otherwise, you will not receive any discount at all.Return an integer array
answer where answer[i] is the final price you will pay for the i^th item of the shop, considering the special discount.Example 1:
Input: prices = [8,4,6,2,3]
Output: [4,2,4,2,3]
Explanation:
For item 0 with price[0]=8 you will receive a discount equivalent to prices[1]=4, therefore, the final price you will pay is 8 - 4 = 4.
For item 1 with price[1]=4 you will receive a discount equivalent to prices[3]=2, therefore, the final price you will pay is 4 - 2 = 2.
For item 2 with price[2]=6 you will receive a discount equivalent to prices[3]=2, therefore, the final price you will pay is 6 - 2 = 4.
For items 3 and 4 you will not receive any discount at all.
Example 2:
Input: prices = [1,2,3,4,5]
Output: [1,2,3,4,5]
Explanation: In this case, for all items, you will not receive any discount at all.
Example 3:
Input: prices = [10,1,1,6]
Output: [9,0,1,6]
Constraints:
•
1 <= prices.length <= 500•
1 <= prices[i] <= 10002024-12-19
769. Max Chunks To Make Sorted
Topic: Array, Stack, Greedy, Sorting, Monotonic Stack
Difficulty: Medium
Problem:
You are given an integer array
We split
Return the largest number of chunks we can make to sort the array.
Example 1:
Example 2:
Constraints:
•
•
•
• All the elements of
769. Max Chunks To Make Sorted
Topic: Array, Stack, Greedy, Sorting, Monotonic Stack
Difficulty: Medium
Problem:
You are given an integer array
arr of length n that represents a permutation of the integers in the range [0, n - 1].We split
arr into some number of chunks (i.e., partitions), and individually sort each chunk. After concatenating them, the result should equal the sorted array.Return the largest number of chunks we can make to sort the array.
Example 1:
Input: arr = [4,3,2,1,0]
Output: 1
Explanation:
Splitting into two or more chunks will not return the required result.
For example, splitting into [4, 3], [2, 1, 0] will result in [3, 4, 0, 1, 2], which isn't sorted.
Example 2:
Input: arr = [1,0,2,3,4]
Output: 4
Explanation:
We can split into two chunks, such as [1, 0], [2, 3, 4].
However, splitting into [1, 0], [2], [3], [4] is the highest number of chunks possible.
Constraints:
•
n == arr.length•
1 <= n <= 10•
0 <= arr[i] < n• All the elements of
arr are unique.2024-12-20
2415. Reverse Odd Levels of Binary Tree
Topic: Tree, Depth-First Search, Breadth-First Search, Binary Tree
Difficulty: Medium
Problem:
Given the
• For example, suppose the node values at level 3 are
Return the root of the reversed tree.
A binary tree is perfect if all parent nodes have two children and all leaves are on the same level.
The level of a node is the number of edges along the path between it and the root node.
Example 1:
Image: https://assets.leetcode.com/uploads/2022/07/28/first_case1.png
Example 2:
Image: https://assets.leetcode.com/uploads/2022/07/28/second_case3.png
Example 3:
Constraints:
• The number of nodes in the tree is in the range
•
•
2415. Reverse Odd Levels of Binary Tree
Topic: Tree, Depth-First Search, Breadth-First Search, Binary Tree
Difficulty: Medium
Problem:
Given the
root of a perfect binary tree, reverse the node values at each odd level of the tree.• For example, suppose the node values at level 3 are
[2,1,3,4,7,11,29,18], then it should become [18,29,11,7,4,3,1,2].Return the root of the reversed tree.
A binary tree is perfect if all parent nodes have two children and all leaves are on the same level.
The level of a node is the number of edges along the path between it and the root node.
Example 1:
Image: https://assets.leetcode.com/uploads/2022/07/28/first_case1.png
Input: root = [2,3,5,8,13,21,34]
Output: [2,5,3,8,13,21,34]
Explanation:
The tree has only one odd level.
The nodes at level 1 are 3, 5 respectively, which are reversed and become 5, 3.
Example 2:
Image: https://assets.leetcode.com/uploads/2022/07/28/second_case3.png
Input: root = [7,13,11]
Output: [7,11,13]
Explanation:
The nodes at level 1 are 13, 11, which are reversed and become 11, 13.
Example 3:
Input: root = [0,1,2,0,0,0,0,1,1,1,1,2,2,2,2]
Output: [0,2,1,0,0,0,0,2,2,2,2,1,1,1,1]
Explanation:
The odd levels have non-zero values.
The nodes at level 1 were 1, 2, and are 2, 1 after the reversal.
The nodes at level 3 were 1, 1, 1, 1, 2, 2, 2, 2, and are 2, 2, 2, 2, 1, 1, 1, 1 after the reversal.
Constraints:
• The number of nodes in the tree is in the range
[1, 2^14].•
0 <= Node.val <= 10^5•
root is a perfect binary tree.2024-12-21
2872. Maximum Number of K-Divisible Components
Topic: Tree, Depth-First Search
Difficulty: Hard
Problem:
There is an undirected tree with
You are also given a 0-indexed integer array
A valid split of the tree is obtained by removing any set of edges, possibly empty, from the tree such that the resulting components all have values that are divisible by
Return the maximum number of components in any valid split.
Example 1:
Image: https://assets.leetcode.com/uploads/2023/08/07/example12-cropped2svg.jpg
Example 2:
Image: https://assets.leetcode.com/uploads/2023/08/07/example21svg-1.jpg
Constraints:
•
•
•
•
•
•
•
• Sum of
• The input is generated such that
2872. Maximum Number of K-Divisible Components
Topic: Tree, Depth-First Search
Difficulty: Hard
Problem:
There is an undirected tree with
n nodes labeled from 0 to n - 1. You are given the integer n and a 2D integer array edges of length n - 1, where edges[i] = [a_i, b_i] indicates that there is an edge between nodes a_i and b_i in the tree.You are also given a 0-indexed integer array
values of length n, where values[i] is the value associated with the i^th node, and an integer k.A valid split of the tree is obtained by removing any set of edges, possibly empty, from the tree such that the resulting components all have values that are divisible by
k, where the value of a connected component is the sum of the values of its nodes.Return the maximum number of components in any valid split.
Example 1:
Image: https://assets.leetcode.com/uploads/2023/08/07/example12-cropped2svg.jpg
Input: n = 5, edges = [[0,2],[1,2],[1,3],[2,4]], values = [1,8,1,4,4], k = 6
Output: 2
Explanation: We remove the edge connecting node 1 with 2. The resulting split is valid because:
- The value of the component containing nodes 1 and 3 is values[1] + values[3] = 12.
- The value of the component containing nodes 0, 2, and 4 is values[0] + values[2] + values[4] = 6.
It can be shown that no other valid split has more than 2 connected components.
Example 2:
Image: https://assets.leetcode.com/uploads/2023/08/07/example21svg-1.jpg
Input: n = 7, edges = [[0,1],[0,2],[1,3],[1,4],[2,5],[2,6]], values = [3,0,6,1,5,2,1], k = 3
Output: 3
Explanation: We remove the edge connecting node 0 with 2, and the edge connecting node 0 with 1. The resulting split is valid because:
- The value of the component containing node 0 is values[0] = 3.
- The value of the component containing nodes 2, 5, and 6 is values[2] + values[5] + values[6] = 9.
- The value of the component containing nodes 1, 3, and 4 is values[1] + values[3] + values[4] = 6.
It can be shown that no other valid split has more than 3 connected components.
Constraints:
•
1 <= n <= 3 * 10^4•
edges.length == n - 1•
edges[i].length == 2•
0 <= a_i, b_i < n•
values.length == n•
0 <= values[i] <= 10^9•
1 <= k <= 10^9• Sum of
values is divisible by k.• The input is generated such that
edges represents a valid tree.2024-12-22
2940. Find Building Where Alice and Bob Can Meet
Topic: Array, Binary Search, Stack, Binary Indexed Tree, Segment Tree, Heap (Priority Queue), Monotonic Stack
Difficulty: Hard
Problem:
You are given a 0-indexed array
If a person is in building
You are also given another array
Return an array
Example 1:
Example 2:
Constraints:
•
•
•
•
•
2940. Find Building Where Alice and Bob Can Meet
Topic: Array, Binary Search, Stack, Binary Indexed Tree, Segment Tree, Heap (Priority Queue), Monotonic Stack
Difficulty: Hard
Problem:
You are given a 0-indexed array
heights of positive integers, where heights[i] represents the height of the i^th building.If a person is in building
i, they can move to any other building j if and only if i < j and heights[i] < heights[j].You are also given another array
queries where queries[i] = [a_i, b_i]. On the i^th query, Alice is in building a_i while Bob is in building b_i.Return an array
ans where ans[i] is the index of the leftmost building where Alice and Bob can meet on the i^th query. If Alice and Bob cannot move to a common building on query i, set ans[i] to -1.Example 1:
Input: heights = [6,4,8,5,2,7], queries = [[0,1],[0,3],[2,4],[3,4],[2,2]]
Output: [2,5,-1,5,2]
Explanation: In the first query, Alice and Bob can move to building 2 since heights[0] < heights[2] and heights[1] < heights[2].
In the second query, Alice and Bob can move to building 5 since heights[0] < heights[5] and heights[3] < heights[5].
In the third query, Alice cannot meet Bob since Alice cannot move to any other building.
In the fourth query, Alice and Bob can move to building 5 since heights[3] < heights[5] and heights[4] < heights[5].
In the fifth query, Alice and Bob are already in the same building.
For ans[i] != -1, It can be shown that ans[i] is the leftmost building where Alice and Bob can meet.
For ans[i] == -1, It can be shown that there is no building where Alice and Bob can meet.
Example 2:
Input: heights = [5,3,8,2,6,1,4,6], queries = [[0,7],[3,5],[5,2],[3,0],[1,6]]
Output: [7,6,-1,4,6]
Explanation: In the first query, Alice can directly move to Bob's building since heights[0] < heights[7].
In the second query, Alice and Bob can move to building 6 since heights[3] < heights[6] and heights[5] < heights[6].
In the third query, Alice cannot meet Bob since Bob cannot move to any other building.
In the fourth query, Alice and Bob can move to building 4 since heights[3] < heights[4] and heights[0] < heights[4].
In the fifth query, Alice can directly move to Bob's building since heights[1] < heights[6].
For ans[i] != -1, It can be shown that ans[i] is the leftmost building where Alice and Bob can meet.
For ans[i] == -1, It can be shown that there is no building where Alice and Bob can meet.
Constraints:
•
1 <= heights.length <= 5 * 10^4•
1 <= heights[i] <= 10^9•
1 <= queries.length <= 5 * 10^4•
queries[i] = [a_i, b_i]•
0 <= a_i, b_i <= heights.length - 12024-12-23
2471. Minimum Number of Operations to Sort a Binary Tree by Level
Topic: Tree, Breadth-First Search, Binary Tree
Difficulty: Medium
Problem:
You are given the
In one operation, you can choose any two nodes at the same level and swap their values.
Return the minimum number of operations needed to make the values at each level sorted in a strictly increasing order.
The level of a node is the number of edges along the path between it and the root node.
Example 1:
Image: https://assets.leetcode.com/uploads/2022/09/18/image-20220918174006-2.png
Example 2:
Image: https://assets.leetcode.com/uploads/2022/09/18/image-20220918174026-3.png
Example 3:
Image: https://assets.leetcode.com/uploads/2022/09/18/image-20220918174052-4.png
Constraints:
• The number of nodes in the tree is in the range
•
• All the values of the tree are unique.
2471. Minimum Number of Operations to Sort a Binary Tree by Level
Topic: Tree, Breadth-First Search, Binary Tree
Difficulty: Medium
Problem:
You are given the
root of a binary tree with unique values.In one operation, you can choose any two nodes at the same level and swap their values.
Return the minimum number of operations needed to make the values at each level sorted in a strictly increasing order.
The level of a node is the number of edges along the path between it and the root node.
Example 1:
Image: https://assets.leetcode.com/uploads/2022/09/18/image-20220918174006-2.png
Input: root = [1,4,3,7,6,8,5,null,null,null,null,9,null,10]
Output: 3
Explanation:
- Swap 4 and 3. The 2^nd level becomes [3,4].
- Swap 7 and 5. The 3^rd level becomes [5,6,8,7].
- Swap 8 and 7. The 3^rd level becomes [5,6,7,8].
We used 3 operations so return 3.
It can be proven that 3 is the minimum number of operations needed.
Example 2:
Image: https://assets.leetcode.com/uploads/2022/09/18/image-20220918174026-3.png
Input: root = [1,3,2,7,6,5,4]
Output: 3
Explanation:
- Swap 3 and 2. The 2^nd level becomes [2,3].
- Swap 7 and 4. The 3^rd level becomes [4,6,5,7].
- Swap 6 and 5. The 3^rd level becomes [4,5,6,7].
We used 3 operations so return 3.
It can be proven that 3 is the minimum number of operations needed.
Example 3:
Image: https://assets.leetcode.com/uploads/2022/09/18/image-20220918174052-4.png
Input: root = [1,2,3,4,5,6]
Output: 0
Explanation: Each level is already sorted in increasing order so return 0.
Constraints:
• The number of nodes in the tree is in the range
[1, 10^5].•
1 <= Node.val <= 10^5• All the values of the tree are unique.
2024-12-24
3203. Find Minimum Diameter After Merging Two Trees
Topic: Tree, Depth-First Search, Breadth-First Search, Graph
Difficulty: Hard
Problem:
There exist two undirected trees with
You must connect one node from the first tree with another node from the second tree with an edge.
Return the minimum possible diameter of the resulting tree.
The diameter of a tree is the length of the longest path between any two nodes in the tree.
Example 1:
Image: https://assets.leetcode.com/uploads/2024/04/22/example11-transformed.png
Input: edges1 = [0,1,0,2,0,3], edges2 = [0,1]
Output: 3
Explanation:
We can obtain a tree of diameter 3 by connecting node 0 from the first tree with any node from the second tree.
Example 2:
Image: https://assets.leetcode.com/uploads/2024/04/22/example211.png
Input: edges1 = [0,1,0,2,0,3,2,4,2,5,3,6,2,7], edges2 = [0,1,0,2,0,3,2,4,2,5,3,6,2,7]
Output: 5
Explanation:
We can obtain a tree of diameter 5 by connecting node 0 from the first tree with node 0 from the second tree.
Constraints:
•
•
•
•
•
•
•
•
• The input is generated such that
3203. Find Minimum Diameter After Merging Two Trees
Topic: Tree, Depth-First Search, Breadth-First Search, Graph
Difficulty: Hard
Problem:
There exist two undirected trees with
n and m nodes, numbered from 0 to n - 1 and from 0 to m - 1, respectively. You are given two 2D integer arrays edges1 and edges2 of lengths n - 1 and m - 1, respectively, where edges1[i] = [a_i, b_i] indicates that there is an edge between nodes a_i and b_i in the first tree and edges2[i] = [u_i, v_i] indicates that there is an edge between nodes u_i and v_i in the second tree.You must connect one node from the first tree with another node from the second tree with an edge.
Return the minimum possible diameter of the resulting tree.
The diameter of a tree is the length of the longest path between any two nodes in the tree.
Example 1:
Image: https://assets.leetcode.com/uploads/2024/04/22/example11-transformed.png
Input: edges1 = [0,1,0,2,0,3], edges2 = [0,1]
Output: 3
Explanation:
We can obtain a tree of diameter 3 by connecting node 0 from the first tree with any node from the second tree.
Example 2:
Image: https://assets.leetcode.com/uploads/2024/04/22/example211.png
Input: edges1 = [0,1,0,2,0,3,2,4,2,5,3,6,2,7], edges2 = [0,1,0,2,0,3,2,4,2,5,3,6,2,7]
Output: 5
Explanation:
We can obtain a tree of diameter 5 by connecting node 0 from the first tree with node 0 from the second tree.
Constraints:
•
1 <= n, m <= 10^5•
edges1.length == n - 1•
edges2.length == m - 1•
edges1[i].length == edges2[i].length == 2•
edges1[i] = [a_i, b_i]•
0 <= a_i, b_i < n•
edges2[i] = [u_i, v_i]•
0 <= u_i, v_i < m• The input is generated such that
edges1 and edges2 represent valid trees.