2025-07-23
1717. Maximum Score From Removing Substrings
Topic: String, Stack, Greedy
Difficulty: Medium
Problem:
You are given a string
• Remove substring
• For example, when removing
• Remove substring
• For example, when removing
Return the maximum points you can gain after applying the above operations on
Example 1:
Example 2:
Constraints:
•
•
•
  1717. Maximum Score From Removing Substrings
Topic: String, Stack, Greedy
Difficulty: Medium
Problem:
You are given a string
s and two integers x and y. You can perform two types of operations any number of times.• Remove substring
"ab" and gain x points.• For example, when removing
"ab" from "cabxbae" it becomes "cxbae".• Remove substring
"ba" and gain y points.• For example, when removing
"ba" from "cabxbae" it becomes "cabxe".Return the maximum points you can gain after applying the above operations on
s.Example 1:
Input: s = "cdbcbbaaabab", x = 4, y = 5
Output: 19
Explanation:
- Remove the "ba" underlined in "cdbcbbaaabab". Now, s = "cdbcbbaaab" and 5 points are added to the score.
- Remove the "ab" underlined in "cdbcbbaaab". Now, s = "cdbcbbaa" and 4 points are added to the score.
- Remove the "ba" underlined in "cdbcbbaa". Now, s = "cdbcba" and 5 points are added to the score.
- Remove the "ba" underlined in "cdbcba". Now, s = "cdbc" and 5 points are added to the score.
Total score = 5 + 4 + 5 + 5 = 19.
Example 2:
Input: s = "aabbaaxybbaabb", x = 5, y = 4
Output: 20
Constraints:
•
1 <= s.length <= 10^5•
1 <= x, y <= 10^4•
s consists of lowercase English letters.2025-07-24
2322. Minimum Score After Removals on a Tree
Topic: Array, Bit Manipulation, Tree, Depth-First Search
Difficulty: Hard
Problem:
There is an undirected connected tree with
You are given a 0-indexed integer array
Remove two distinct edges of the tree to form three connected components. For a pair of removed edges, the following steps are defined:
1. Get the XOR of all the values of the nodes for each of the three components respectively.
2. The difference between the largest XOR value and the smallest XOR value is the score of the pair.
• For example, say the three components have the node values:
Return the minimum score of any possible pair of edge removals on the given tree.
Example 1:
Image: https://assets.leetcode.com/uploads/2022/05/03/ex1drawio.png
Example 2:
Image: https://assets.leetcode.com/uploads/2022/05/03/ex2drawio.png
Constraints:
•
•
•
•
•
•
•
•
  2322. Minimum Score After Removals on a Tree
Topic: Array, Bit Manipulation, Tree, Depth-First Search
Difficulty: Hard
Problem:
There is an undirected connected tree with
n nodes labeled from 0 to n - 1 and n - 1 edges.You are given a 0-indexed integer array
nums of length n where nums[i] represents the value of the i^th node. You are also given 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.Remove two distinct edges of the tree to form three connected components. For a pair of removed edges, the following steps are defined:
1. Get the XOR of all the values of the nodes for each of the three components respectively.
2. The difference between the largest XOR value and the smallest XOR value is the score of the pair.
• For example, say the three components have the node values:
[4,5,7], [1,9], and [3,3,3]. The three XOR values are 4 ^ 5 ^ 7 = 6, 1 ^ 9 = 8, and 3 ^ 3 ^ 3 = 3. The largest XOR value is 8 and the smallest XOR value is 3. The score is then 8 - 3 = 5.Return the minimum score of any possible pair of edge removals on the given tree.
Example 1:
Image: https://assets.leetcode.com/uploads/2022/05/03/ex1drawio.png
Input: nums = [1,5,5,4,11], edges = [[0,1],[1,2],[1,3],[3,4]]
Output: 9
Explanation: The diagram above shows a way to make a pair of removals.
- The 1^st component has nodes [1,3,4] with values [5,4,11]. Its XOR value is 5 ^ 4 ^ 11 = 10.
- The 2^nd component has node [0] with value [1]. Its XOR value is 1 = 1.
- The 3^rd component has node [2] with value [5]. Its XOR value is 5 = 5.
The score is the difference between the largest and smallest XOR value which is 10 - 1 = 9.
It can be shown that no other pair of removals will obtain a smaller score than 9.
Example 2:
Image: https://assets.leetcode.com/uploads/2022/05/03/ex2drawio.png
Input: nums = [5,5,2,4,4,2], edges = [[0,1],[1,2],[5,2],[4,3],[1,3]]
Output: 0
Explanation: The diagram above shows a way to make a pair of removals.
- The 1^st component has nodes [3,4] with values [4,4]. Its XOR value is 4 ^ 4 = 0.
- The 2^nd component has nodes [1,0] with values [5,5]. Its XOR value is 5 ^ 5 = 0.
- The 3^rd component has nodes [2,5] with values [2,2]. Its XOR value is 2 ^ 2 = 0.
The score is the difference between the largest and smallest XOR value which is 0 - 0 = 0.
We cannot obtain a smaller score than 0.
Constraints:
•
n == nums.length•
3 <= n <= 1000•
1 <= nums[i] <= 10^8•
edges.length == n - 1•
edges[i].length == 2•
0 <= a_i, b_i < n•
a_i != b_i•
edges represents a valid tree.2025-07-25
3487. Maximum Unique Subarray Sum After Deletion
Topic: Array, Hash Table, Greedy
Difficulty: Easy
Problem:
You are given an integer array
You are allowed to delete any number of elements from
1. All elements in the subarray are unique.
2. The sum of the elements in the subarray is maximized.
Return the maximum sum of such a subarray.
Example 1:
Input: nums = 1,2,3,4,5
Output: 15
Explanation:
Select the entire array without deleting any element to obtain the maximum sum.
Example 2:
Input: nums = 1,1,0,1,1
Output: 1
Explanation:
Delete the element
Example 3:
Input: nums = 1,2,-1,-2,1,0,-1
Output: 3
Explanation:
Delete the elements
Constraints:
•
•
  3487. Maximum Unique Subarray Sum After Deletion
Topic: Array, Hash Table, Greedy
Difficulty: Easy
Problem:
You are given an integer array
nums.You are allowed to delete any number of elements from
nums without making it empty. After performing the deletions, select a subarray of nums such that:1. All elements in the subarray are unique.
2. The sum of the elements in the subarray is maximized.
Return the maximum sum of such a subarray.
Example 1:
Input: nums = 1,2,3,4,5
Output: 15
Explanation:
Select the entire array without deleting any element to obtain the maximum sum.
Example 2:
Input: nums = 1,1,0,1,1
Output: 1
Explanation:
Delete the element
nums[0] == 1, nums[1] == 1, nums[2] == 0, and nums[3] == 1. Select the entire array [1] to obtain the maximum sum.Example 3:
Input: nums = 1,2,-1,-2,1,0,-1
Output: 3
Explanation:
Delete the elements
nums[2] == -1 and nums[3] == -2, and select the subarray [2, 1] from [1, 2, 1, 0, -1] to obtain the maximum sum.Constraints:
•
1 <= nums.length <= 100•
-100 <= nums[i] <= 1002025-07-26
3480. Maximize Subarrays After Removing One Conflicting Pair
Topic: Array, Segment Tree, Enumeration, Prefix Sum
Difficulty: Hard
Problem:
You are given an integer
Remove exactly one element from
Return the maximum number of subarrays possible after removing exactly one conflicting pair.
Example 1:
Input: n = 4, conflictingPairs = [2,3,1,4]
Output: 9
Explanation:
• Remove
• There are 9 subarrays in
• The maximum number of subarrays we can achieve after removing one element from
Example 2:
Input: n = 5, conflictingPairs = [1,2,2,5,3,5]
Output: 12
Explanation:
• Remove
• There are 12 subarrays in
• The maximum number of subarrays we can achieve after removing one element from
Constraints:
•
•
•
•
•
  3480. Maximize Subarrays After Removing One Conflicting Pair
Topic: Array, Segment Tree, Enumeration, Prefix Sum
Difficulty: Hard
Problem:
You are given an integer
n which represents an array nums containing the numbers from 1 to n in order. Additionally, you are given a 2D array conflictingPairs, where conflictingPairs[i] = [a, b] indicates that a and b form a conflicting pair.Remove exactly one element from
conflictingPairs. Afterward, count the number of non-empty subarrays of nums which do not contain both a and b for any remaining conflicting pair [a, b].Return the maximum number of subarrays possible after removing exactly one conflicting pair.
Example 1:
Input: n = 4, conflictingPairs = [2,3,1,4]
Output: 9
Explanation:
• Remove
[2, 3] from conflictingPairs. Now, conflictingPairs = [[1, 4]].• There are 9 subarrays in
nums where [1, 4] do not appear together. They are [1], [2], [3], [4], [1, 2], [2, 3], [3, 4], [1, 2, 3] and [2, 3, 4].• The maximum number of subarrays we can achieve after removing one element from
conflictingPairs is 9.Example 2:
Input: n = 5, conflictingPairs = [1,2,2,5,3,5]
Output: 12
Explanation:
• Remove
[1, 2] from conflictingPairs. Now, conflictingPairs = [[2, 5], [3, 5]].• There are 12 subarrays in
nums where [2, 5] and [3, 5] do not appear together.• The maximum number of subarrays we can achieve after removing one element from
conflictingPairs is 12.Constraints:
•
2 <= n <= 10^5•
1 <= conflictingPairs.length <= 2 * n•
conflictingPairs[i].length == 2•
1 <= conflictingPairs[i][j] <= n•
conflictingPairs[i][0] != conflictingPairs[i][1]2025-07-27
2210. Count Hills and Valleys in an Array
Topic: Array
Difficulty: Easy
Problem:
You are given a 0-indexed integer array
Note that for an index to be part of a hill or valley, it must have a non-equal neighbor on both the left and right of the index.
Return the number of hills and valleys in
Example 1:
Example 2:
Constraints:
•
•
  2210. Count Hills and Valleys in an Array
Topic: Array
Difficulty: Easy
Problem:
You are given a 0-indexed integer array
nums. An index i is part of a hill in nums if the closest non-equal neighbors of i are smaller than nums[i]. Similarly, an index i is part of a valley in nums if the closest non-equal neighbors of i are larger than nums[i]. Adjacent indices i and j are part of the same hill or valley if nums[i] == nums[j].Note that for an index to be part of a hill or valley, it must have a non-equal neighbor on both the left and right of the index.
Return the number of hills and valleys in
nums.Example 1:
Input: nums = [2,4,1,1,6,5]
Output: 3
Explanation:
At index 0: There is no non-equal neighbor of 2 on the left, so index 0 is neither a hill nor a valley.
At index 1: The closest non-equal neighbors of 4 are 2 and 1. Since 4 > 2 and 4 > 1, index 1 is a hill.
At index 2: The closest non-equal neighbors of 1 are 4 and 6. Since 1 < 4 and 1 < 6, index 2 is a valley.
At index 3: The closest non-equal neighbors of 1 are 4 and 6. Since 1 < 4 and 1 < 6, index 3 is a valley, but note that it is part of the same valley as index 2.
At index 4: The closest non-equal neighbors of 6 are 1 and 5. Since 6 > 1 and 6 > 5, index 4 is a hill.
At index 5: There is no non-equal neighbor of 5 on the right, so index 5 is neither a hill nor a valley.
There are 3 hills and valleys so we return 3.
Example 2:
Input: nums = [6,6,5,5,4,1]
Output: 0
Explanation:
At index 0: There is no non-equal neighbor of 6 on the left, so index 0 is neither a hill nor a valley.
At index 1: There is no non-equal neighbor of 6 on the left, so index 1 is neither a hill nor a valley.
At index 2: The closest non-equal neighbors of 5 are 6 and 4. Since 5 < 6 and 5 > 4, index 2 is neither a hill nor a valley.
At index 3: The closest non-equal neighbors of 5 are 6 and 4. Since 5 < 6 and 5 > 4, index 3 is neither a hill nor a valley.
At index 4: The closest non-equal neighbors of 4 are 5 and 1. Since 4 < 5 and 4 > 1, index 4 is neither a hill nor a valley.
At index 5: There is no non-equal neighbor of 1 on the right, so index 5 is neither a hill nor a valley.
There are 0 hills and valleys so we return 0.
Constraints:
•
3 <= nums.length <= 100•
1 <= nums[i] <= 1002025-07-28
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^52025-07-29
2411. Smallest Subarrays With Maximum Bitwise OR
Topic: Array, Binary Search, Bit Manipulation, Sliding Window
Difficulty: Medium
Problem:
You are given a 0-indexed array
• In other words, let
The bitwise OR of an array is the bitwise OR of all the numbers in it.
Return an integer array
A subarray is a contiguous non-empty sequence of elements within an array.
Example 1:
Example 2:
Constraints:
•
•
•
  2411. Smallest Subarrays With Maximum Bitwise OR
Topic: Array, Binary Search, Bit Manipulation, Sliding Window
Difficulty: Medium
Problem:
You are given a 0-indexed array
nums of length n, consisting of non-negative integers. For each index i from 0 to n - 1, you must determine the size of the minimum sized non-empty subarray of nums starting at i (inclusive) that has the maximum possible bitwise OR.• In other words, let
B_ij be the bitwise OR of the subarray nums[i...j]. You need to find the smallest subarray starting at i, such that bitwise OR of this subarray is equal to max(B_ik) where i <= k <= n - 1.The bitwise OR of an array is the bitwise OR of all the numbers in it.
Return an integer array
answer of size n where answer[i] is the length of the minimum sized subarray starting at i with maximum bitwise OR.A subarray is a contiguous non-empty sequence of elements within an array.
Example 1:
Input: nums = [1,0,2,1,3]
Output: [3,3,2,2,1]
Explanation:
The maximum possible bitwise OR starting at any index is 3.
- Starting at index 0, the shortest subarray that yields it is [1,0,2].
- Starting at index 1, the shortest subarray that yields the maximum bitwise OR is [0,2,1].
- Starting at index 2, the shortest subarray that yields the maximum bitwise OR is [2,1].
- Starting at index 3, the shortest subarray that yields the maximum bitwise OR is [1,3].
- Starting at index 4, the shortest subarray that yields the maximum bitwise OR is [3].
Therefore, we return [3,3,2,2,1].
Example 2:
Input: nums = [1,2]
Output: [2,1]
Explanation:
Starting at index 0, the shortest subarray that yields the maximum bitwise OR is of length 2.
Starting at index 1, the shortest subarray that yields the maximum bitwise OR is of length 1.
Therefore, we return [2,1].
Constraints:
•
n == nums.length•
1 <= n <= 10^5•
0 <= nums[i] <= 10^92025-07-30
2419. Longest Subarray With Maximum Bitwise AND
Topic: Array, Bit Manipulation, Brainteaser
Difficulty: Medium
Problem:
You are given an integer array
Consider a non-empty subarray from
• In other words, let
Return the length of the longest such subarray.
The bitwise AND of an array is the bitwise AND of all the numbers in it.
A subarray is a contiguous sequence of elements within an array.
Example 1:
Example 2:
Constraints:
•
•
  2419. Longest Subarray With Maximum Bitwise AND
Topic: Array, Bit Manipulation, Brainteaser
Difficulty: Medium
Problem:
You are given an integer array
nums of size n.Consider a non-empty subarray from
nums that has the maximum possible bitwise AND.• In other words, let
k be the maximum value of the bitwise AND of any subarray of nums. Then, only subarrays with a bitwise AND equal to k should be considered.Return the length of the longest such subarray.
The bitwise AND of an array is the bitwise AND of all the numbers in it.
A subarray is a contiguous sequence of elements within an array.
Example 1:
Input: nums = [1,2,3,3,2,2]
Output: 2
Explanation:
The maximum possible bitwise AND of a subarray is 3.
The longest subarray with that value is [3,3], so we return 2.
Example 2:
Input: nums = [1,2,3,4]
Output: 1
Explanation:
The maximum possible bitwise AND of a subarray is 4.
The longest subarray with that value is [4], so we return 1.
Constraints:
•
1 <= nums.length <= 10^5•
1 <= nums[i] <= 10^62025-07-31
898. Bitwise ORs of Subarrays
Topic: Array, Dynamic Programming, Bit Manipulation
Difficulty: Medium
Problem:
Given an integer array
The bitwise OR of a subarray is the bitwise OR of each integer in the subarray. The bitwise OR of a subarray of one integer is that integer.
A subarray is a contiguous non-empty sequence of elements within an array.
Example 1:
Example 2:
Example 3:
Constraints:
•
•
  898. Bitwise ORs of Subarrays
Topic: Array, Dynamic Programming, Bit Manipulation
Difficulty: Medium
Problem:
Given an integer array
arr, return the number of distinct bitwise ORs of all the non-empty subarrays of arr.The bitwise OR of a subarray is the bitwise OR of each integer in the subarray. The bitwise OR of a subarray of one integer is that integer.
A subarray is a contiguous non-empty sequence of elements within an array.
Example 1:
Input: arr = [0]
Output: 1
Explanation: There is only one possible result: 0.
Example 2:
Input: arr = [1,1,2]
Output: 3
Explanation: The possible subarrays are [1], [1], [2], [1, 1], [1, 2], [1, 1, 2].
These yield the results 1, 1, 2, 1, 3, 3.
There are 3 unique values, so the answer is 3.
Example 3:
Input: arr = [1,2,4]
Output: 6
Explanation: The possible results are 1, 2, 3, 4, 6, and 7.
Constraints:
•
1 <= arr.length <= 5 * 10^4•
0 <= arr[i] <= 10^92025-08-01
118. Pascal's Triangle
Topic: Array, Dynamic Programming
Difficulty: Easy
Problem:
Given an integer
In Pascal's triangle, each number is the sum of the two numbers directly above it as shown:
Image: https://upload.wikimedia.org/wikipedia/commons/0/0d/PascalTriangleAnimated2.gif
Example 1:
Example 2:
Constraints:
•
  118. Pascal's Triangle
Topic: Array, Dynamic Programming
Difficulty: Easy
Problem:
Given an integer
numRows, return the first numRows of Pascal's triangle.In Pascal's triangle, each number is the sum of the two numbers directly above it as shown:
Image: https://upload.wikimedia.org/wikipedia/commons/0/0d/PascalTriangleAnimated2.gif
Example 1:
Input: numRows = 5
Output: [[1],[1,1],[1,2,1],[1,3,3,1],[1,4,6,4,1]]
Example 2:
Input: numRows = 1
Output: [[1]]
Constraints:
•
1 <= numRows <= 302025-08-02
2561. Rearranging Fruits
Topic: Array, Hash Table, Greedy, Sort
Difficulty: Hard
Problem:
You have two fruit baskets containing
• Chose two indices
• The cost of the swap is
Two baskets are considered equal if sorting them according to the fruit cost makes them exactly the same baskets.
Return the minimum cost to make both the baskets equal or
Example 1:
Example 2:
Constraints:
•
•
•
  2561. Rearranging Fruits
Topic: Array, Hash Table, Greedy, Sort
Difficulty: Hard
Problem:
You have two fruit baskets containing
n fruits each. You are given two 0-indexed integer arrays basket1 and basket2 representing the cost of fruit in each basket. You want to make both baskets equal. To do so, you can use the following operation as many times as you want:• Chose two indices
i and j, and swap the ithfruit of basket1 with the jth fruit of basket2.• The cost of the swap is
min(basket1[i],basket2[j]).Two baskets are considered equal if sorting them according to the fruit cost makes them exactly the same baskets.
Return the minimum cost to make both the baskets equal or
-1 if impossible.Example 1:
Input: basket1 = [4,2,2,2], basket2 = [1,4,1,2]
Output: 1
Explanation: Swap index 1 of basket1 with index 0 of basket2, which has cost 1. Now basket1 = [4,1,2,2] and basket2 = [2,4,1,2]. Rearranging both the arrays makes them equal.
Example 2:
Input: basket1 = [2,3,4,1], basket2 = [3,2,5,1]
Output: -1
Explanation: It can be shown that it is impossible to make both the baskets equal.
Constraints:
•
basket1.length == basket2.length•
1 <= basket1.length <= 10^5•
1 <= basket1[i],basket2[i] <= 10^92025-08-03
2106. Maximum Fruits Harvested After at Most K Steps
Topic: Array, Binary Search, Sliding Window, Prefix Sum
Difficulty: Hard
Problem:
Fruits are available at some positions on an infinite x-axis. You are given a 2D integer array
You are also given an integer
Return the maximum total number of fruits you can harvest.
Example 1:
Image: https://assets.leetcode.com/uploads/2021/11/21/1.png
Example 2:
Image: https://assets.leetcode.com/uploads/2021/11/21/2.png
Example 3:
Image: https://assets.leetcode.com/uploads/2021/11/21/3.png
Constraints:
•
•
•
•
•
•
  2106. Maximum Fruits Harvested After at Most K Steps
Topic: Array, Binary Search, Sliding Window, Prefix Sum
Difficulty: Hard
Problem:
Fruits are available at some positions on an infinite x-axis. You are given a 2D integer array
fruits where fruits[i] = [position_i, amount_i] depicts amount_i fruits at the position position_i. fruits is already sorted by position_i in ascending order, and each position_i is unique.You are also given an integer
startPos and an integer k. Initially, you are at the position startPos. From any position, you can either walk to the left or right. It takes one step to move one unit on the x-axis, and you can walk at most k steps in total. For every position you reach, you harvest all the fruits at that position, and the fruits will disappear from that position.Return the maximum total number of fruits you can harvest.
Example 1:
Image: https://assets.leetcode.com/uploads/2021/11/21/1.png
Input: fruits = [[2,8],[6,3],[8,6]], startPos = 5, k = 4
Output: 9
Explanation:
The optimal way is to:
- Move right to position 6 and harvest 3 fruits
- Move right to position 8 and harvest 6 fruits
You moved 3 steps and harvested 3 + 6 = 9 fruits in total.
Example 2:
Image: https://assets.leetcode.com/uploads/2021/11/21/2.png
Input: fruits = [[0,9],[4,1],[5,7],[6,2],[7,4],[10,9]], startPos = 5, k = 4
Output: 14
Explanation:
You can move at most k = 4 steps, so you cannot reach position 0 nor 10.
The optimal way is to:
- Harvest the 7 fruits at the starting position 5
- Move left to position 4 and harvest 1 fruit
- Move right to position 6 and harvest 2 fruits
- Move right to position 7 and harvest 4 fruits
You moved 1 + 3 = 4 steps and harvested 7 + 1 + 2 + 4 = 14 fruits in total.
Example 3:
Image: https://assets.leetcode.com/uploads/2021/11/21/3.png
Input: fruits = [[0,3],[6,4],[8,5]], startPos = 3, k = 2
Output: 0
Explanation:
You can move at most k = 2 steps and cannot reach any position with fruits.
Constraints:
•
1 <= fruits.length <= 10^5•
fruits[i].length == 2•
0 <= startPos, position_i <= 2 * 10^5•
position_i-1 < position_i for any i > 0 (0-indexed)•
1 <= amount_i <= 10^4•
0 <= k <= 2 * 10^52025-08-04
904. Fruit Into Baskets
Topic: Array, Hash Table, Sliding Window
Difficulty: Medium
Problem:
You are visiting a farm that has a single row of fruit trees arranged from left to right. The trees are represented by an integer array
You want to collect as much fruit as possible. However, the owner has some strict rules that you must follow:
• You only have two baskets, and each basket can only hold a single type of fruit. There is no limit on the amount of fruit each basket can hold.
• Starting from any tree of your choice, you must pick exactly one fruit from every tree (including the start tree) while moving to the right. The picked fruits must fit in one of your baskets.
• Once you reach a tree with fruit that cannot fit in your baskets, you must stop.
Given the integer array
Example 1:
Example 2:
Example 3:
Constraints:
•
•
  904. Fruit Into Baskets
Topic: Array, Hash Table, Sliding Window
Difficulty: Medium
Problem:
You are visiting a farm that has a single row of fruit trees arranged from left to right. The trees are represented by an integer array
fruits where fruits[i] is the type of fruit the i^th tree produces.You want to collect as much fruit as possible. However, the owner has some strict rules that you must follow:
• You only have two baskets, and each basket can only hold a single type of fruit. There is no limit on the amount of fruit each basket can hold.
• Starting from any tree of your choice, you must pick exactly one fruit from every tree (including the start tree) while moving to the right. The picked fruits must fit in one of your baskets.
• Once you reach a tree with fruit that cannot fit in your baskets, you must stop.
Given the integer array
fruits, return the maximum number of fruits you can pick.Example 1:
Input: fruits = [1,2,1]
Output: 3
Explanation: We can pick from all 3 trees.
Example 2:
Input: fruits = [0,1,2,2]
Output: 3
Explanation: We can pick from trees [1,2,2].
If we had started at the first tree, we would only pick from trees [0,1].
Example 3:
Input: fruits = [1,2,3,2,2]
Output: 4
Explanation: We can pick from trees [2,3,2,2].
If we had started at the first tree, we would only pick from trees [1,2].
Constraints:
•
1 <= fruits.length <= 10^5•
0 <= fruits[i] < fruits.length2025-08-05
3477. Fruits Into Baskets II
Topic: Array, Binary Search, Segment Tree, Simulation, Ordered Set
Difficulty: Easy
Problem:
You are given two arrays of integers,
From left to right, place the fruits according to these rules:
• Each fruit type must be placed in the leftmost available basket with a capacity greater than or equal to the quantity of that fruit type.
• Each basket can hold only one type of fruit.
• If a fruit type cannot be placed in any basket, it remains unplaced.
Return the number of fruit types that remain unplaced after all possible allocations are made.
Example 1:
Input: fruits = 4,2,5, baskets = 3,5,4
Output: 1
Explanation:
•
•
•
Since one fruit type remains unplaced, we return 1.
Example 2:
Input: fruits = 3,6,1, baskets = 6,4,7
Output: 0
Explanation:
•
•
•
Since all fruits are successfully placed, we return 0.
Constraints:
•
•
•
  3477. Fruits Into Baskets II
Topic: Array, Binary Search, Segment Tree, Simulation, Ordered Set
Difficulty: Easy
Problem:
You are given two arrays of integers,
fruits and baskets, each of length n, where fruits[i] represents the quantity of the i^th type of fruit, and baskets[j] represents the capacity of the j^th basket.From left to right, place the fruits according to these rules:
• Each fruit type must be placed in the leftmost available basket with a capacity greater than or equal to the quantity of that fruit type.
• Each basket can hold only one type of fruit.
• If a fruit type cannot be placed in any basket, it remains unplaced.
Return the number of fruit types that remain unplaced after all possible allocations are made.
Example 1:
Input: fruits = 4,2,5, baskets = 3,5,4
Output: 1
Explanation:
•
fruits[0] = 4 is placed in baskets[1] = 5.•
fruits[1] = 2 is placed in baskets[0] = 3.•
fruits[2] = 5 cannot be placed in baskets[2] = 4.Since one fruit type remains unplaced, we return 1.
Example 2:
Input: fruits = 3,6,1, baskets = 6,4,7
Output: 0
Explanation:
•
fruits[0] = 3 is placed in baskets[0] = 6.•
fruits[1] = 6 cannot be placed in baskets[1] = 4 (insufficient capacity) but can be placed in the next available basket, baskets[2] = 7.•
fruits[2] = 1 is placed in baskets[1] = 4.Since all fruits are successfully placed, we return 0.
Constraints:
•
n == fruits.length == baskets.length•
1 <= n <= 100•
1 <= fruits[i], baskets[i] <= 10002025-08-06
3479. Fruits Into Baskets III
Topic: Array, Binary Search, Segment Tree, Ordered Set
Difficulty: Medium
Problem:
You are given two arrays of integers,
From left to right, place the fruits according to these rules:
• Each fruit type must be placed in the leftmost available basket with a capacity greater than or equal to the quantity of that fruit type.
• Each basket can hold only one type of fruit.
• If a fruit type cannot be placed in any basket, it remains unplaced.
Return the number of fruit types that remain unplaced after all possible allocations are made.
Example 1:
Input: fruits = 4,2,5, baskets = 3,5,4
Output: 1
Explanation:
•
•
•
Since one fruit type remains unplaced, we return 1.
Example 2:
Input: fruits = 3,6,1, baskets = 6,4,7
Output: 0
Explanation:
•
•
•
Since all fruits are successfully placed, we return 0.
Constraints:
•
•
•
  3479. Fruits Into Baskets III
Topic: Array, Binary Search, Segment Tree, Ordered Set
Difficulty: Medium
Problem:
You are given two arrays of integers,
fruits and baskets, each of length n, where fruits[i] represents the quantity of the i^th type of fruit, and baskets[j] represents the capacity of the j^th basket.From left to right, place the fruits according to these rules:
• Each fruit type must be placed in the leftmost available basket with a capacity greater than or equal to the quantity of that fruit type.
• Each basket can hold only one type of fruit.
• If a fruit type cannot be placed in any basket, it remains unplaced.
Return the number of fruit types that remain unplaced after all possible allocations are made.
Example 1:
Input: fruits = 4,2,5, baskets = 3,5,4
Output: 1
Explanation:
•
fruits[0] = 4 is placed in baskets[1] = 5.•
fruits[1] = 2 is placed in baskets[0] = 3.•
fruits[2] = 5 cannot be placed in baskets[2] = 4.Since one fruit type remains unplaced, we return 1.
Example 2:
Input: fruits = 3,6,1, baskets = 6,4,7
Output: 0
Explanation:
•
fruits[0] = 3 is placed in baskets[0] = 6.•
fruits[1] = 6 cannot be placed in baskets[1] = 4 (insufficient capacity) but can be placed in the next available basket, baskets[2] = 7.•
fruits[2] = 1 is placed in baskets[1] = 4.Since all fruits are successfully placed, we return 0.
Constraints:
•
n == fruits.length == baskets.length•
1 <= n <= 10^5•
1 <= fruits[i], baskets[i] <= 10^92025-08-07
3363. Find the Maximum Number of Fruits Collected
Topic: Array, Dynamic Programming, Matrix
Difficulty: Hard
Problem:
There is a game dungeon comprised of
You are given a 2D array
The children will make exactly
• The child starting from
• The child starting from
• The child starting from
When a child enters a room, they will collect all the fruits there. If two or more children enter the same room, only one child will collect the fruits, and the room will be emptied after they leave.
Return the maximum number of fruits the children can collect from the dungeon.
Example 1:
Input: fruits = [1,2,3,4,5,6,8,7,9,10,11,12,13,14,15,16]
Output: 100
Explanation:
Image: https://assets.leetcode.com/uploads/2024/10/15/example_1.gif
In this example:
• The 1^st child (green) moves on the path
• The 2^nd child (red) moves on the path
• The 3^rd child (blue) moves on the path
In total they collect
Example 2:
Input: fruits = [1,1,1,1]
Output: 4
Explanation:
In this example:
• The 1^st child moves on the path
• The 2^nd child moves on the path
• The 3^rd child moves on the path
In total they collect
Constraints:
•
•
  3363. Find the Maximum Number of Fruits Collected
Topic: Array, Dynamic Programming, Matrix
Difficulty: Hard
Problem:
There is a game dungeon comprised of
n x n rooms arranged in a grid.You are given a 2D array
fruits of size n x n, where fruits[i][j] represents the number of fruits in the room (i, j). Three children will play in the game dungeon, with initial positions at the corner rooms (0, 0), (0, n - 1), and (n - 1, 0).The children will make exactly
n - 1 moves according to the following rules to reach the room (n - 1, n - 1):• The child starting from
(0, 0) must move from their current room (i, j) to one of the rooms (i + 1, j + 1), (i + 1, j), and (i, j + 1) if the target room exists.• The child starting from
(0, n - 1) must move from their current room (i, j) to one of the rooms (i + 1, j - 1), (i + 1, j), and (i + 1, j + 1) if the target room exists.• The child starting from
(n - 1, 0) must move from their current room (i, j) to one of the rooms (i - 1, j + 1), (i, j + 1), and (i + 1, j + 1) if the target room exists.When a child enters a room, they will collect all the fruits there. If two or more children enter the same room, only one child will collect the fruits, and the room will be emptied after they leave.
Return the maximum number of fruits the children can collect from the dungeon.
Example 1:
Input: fruits = [1,2,3,4,5,6,8,7,9,10,11,12,13,14,15,16]
Output: 100
Explanation:
Image: https://assets.leetcode.com/uploads/2024/10/15/example_1.gif
In this example:
• The 1^st child (green) moves on the path
(0,0) -> (1,1) -> (2,2) -> (3, 3).• The 2^nd child (red) moves on the path
(0,3) -> (1,2) -> (2,3) -> (3, 3).• The 3^rd child (blue) moves on the path
(3,0) -> (3,1) -> (3,2) -> (3, 3).In total they collect
1 + 6 + 11 + 16 + 4 + 8 + 12 + 13 + 14 + 15 = 100 fruits.Example 2:
Input: fruits = [1,1,1,1]
Output: 4
Explanation:
In this example:
• The 1^st child moves on the path
(0,0) -> (1,1).• The 2^nd child moves on the path
(0,1) -> (1,1).• The 3^rd child moves on the path
(1,0) -> (1,1).In total they collect
1 + 1 + 1 + 1 = 4 fruits.Constraints:
•
2 <= n == fruits.length == fruits[i].length <= 1000•
0 <= fruits[i][j] <= 10002025-08-08
808. Soup Servings
Topic: Math, Dynamic Programming, Probability and Statistics
Difficulty: Medium
Problem:
You have two soups, A and B, each starting with
• pour 100 mL from type A and 0 mL from type B
• pour 75 mL from type A and 25 mL from type B
• pour 50 mL from type A and 50 mL from type B
• pour 25 mL from type A and 75 mL from type B
Note:
• There is no operation that pours 0 mL from A and 100 mL from B.
• The amounts from A and B are poured simultaneously during the turn.
• If an operation asks you to pour more than you have left of a soup, pour all that remains of that soup.
The process stops immediately after any turn in which one of the soups is used up.
Return the probability that A is used up before B, plus half the probability that both soups are used up in the same turn. Answers within
Example 1:
Example 2:
Constraints:
•
  808. Soup Servings
Topic: Math, Dynamic Programming, Probability and Statistics
Difficulty: Medium
Problem:
You have two soups, A and B, each starting with
n mL. On every turn, one of the following four serving operations is chosen at random, each with probability 0.25 independent of all previous turns:• pour 100 mL from type A and 0 mL from type B
• pour 75 mL from type A and 25 mL from type B
• pour 50 mL from type A and 50 mL from type B
• pour 25 mL from type A and 75 mL from type B
Note:
• There is no operation that pours 0 mL from A and 100 mL from B.
• The amounts from A and B are poured simultaneously during the turn.
• If an operation asks you to pour more than you have left of a soup, pour all that remains of that soup.
The process stops immediately after any turn in which one of the soups is used up.
Return the probability that A is used up before B, plus half the probability that both soups are used up in the same turn. Answers within
10^-5 of the actual answer will be accepted.Example 1:
Input: n = 50
Output: 0.62500
Explanation:
If we perform either of the first two serving operations, soup A will become empty first.
If we perform the third operation, A and B will become empty at the same time.
If we perform the fourth operation, B will become empty first.
So the total probability of A becoming empty first plus half the probability that A and B become empty at the same time, is 0.25 * (1 + 1 + 0.5 + 0) = 0.625.
Example 2:
Input: n = 100
Output: 0.71875
Explanation:
If we perform the first serving operation, soup A will become empty first.
If we perform the second serving operations, A will become empty on performing operation [1, 2, 3], and both A and B become empty on performing operation 4.
If we perform the third operation, A will become empty on performing operation [1, 2], and both A and B become empty on performing operation 3.
If we perform the fourth operation, A will become empty on performing operation 1, and both A and B become empty on performing operation 2.
So the total probability of A becoming empty first plus half the probability that A and B become empty at the same time, is 0.71875.
Constraints:
•
0 <= n <= 10^92025-08-09
231. Power of Two
Topic: Math, Bit Manipulation, Recursion
Difficulty: Easy
Problem:
Given an integer
An integer
Example 1:
Example 2:
Example 3:
Constraints:
•
Follow up: Could you solve it without loops/recursion?
  231. Power of Two
Topic: Math, Bit Manipulation, Recursion
Difficulty: Easy
Problem:
Given an integer
n, return true if it is a power of two. Otherwise, return false.An integer
n is a power of two, if there exists an integer x such that n == 2^x.Example 1:
Input: n = 1
Output: true
Explanation: 2^0 = 1
Example 2:
Input: n = 16
Output: true
Explanation: 2^4 = 16
Example 3:
Input: n = 3
Output: false
Constraints:
•
-2^31 <= n <= 2^31 - 1Follow up: Could you solve it without loops/recursion?
2025-08-10
869. Reordered Power of 2
Topic: Hash Table, Math, Sorting, Counting, Enumeration
Difficulty: Medium
Problem:
You are given an integer
Return
Example 1:
Example 2:
Constraints:
•
  869. Reordered Power of 2
Topic: Hash Table, Math, Sorting, Counting, Enumeration
Difficulty: Medium
Problem:
You are given an integer
n. We reorder the digits in any order (including the original order) such that the leading digit is not zero.Return
true if and only if we can do this so that the resulting number is a power of two.Example 1:
Input: n = 1
Output: true
Example 2:
Input: n = 10
Output: false
Constraints:
•
1 <= n <= 10^92025-08-11
2438. Range Product Queries of Powers
Topic: Array, Bit Manipulation, Prefix Sum
Difficulty: Medium
Problem:
Given a positive integer
You are also given a 0-indexed 2D integer array
Return an array
Example 1:
Example 2:
Constraints:
•
•
•
  2438. Range Product Queries of Powers
Topic: Array, Bit Manipulation, Prefix Sum
Difficulty: Medium
Problem:
Given a positive integer
n, there exists a 0-indexed array called powers, composed of the minimum number of powers of 2 that sum to n. The array is sorted in non-decreasing order, and there is only one way to form the array.You are also given a 0-indexed 2D integer array
queries, where queries[i] = [left_i, right_i]. Each queries[i] represents a query where you have to find the product of all powers[j] with left_i <= j <= right_i.Return an array
answers, equal in length to queries, where answers[i] is the answer to the i^th query. Since the answer to the i^th query may be too large, each answers[i] should be returned modulo 10^9 + 7.Example 1:
Input: n = 15, queries = [[0,1],[2,2],[0,3]]
Output: [2,4,64]
Explanation:
For n = 15, powers = [1,2,4,8]. It can be shown that powers cannot be a smaller size.
Answer to 1st query: powers[0] * powers[1] = 1 * 2 = 2.
Answer to 2nd query: powers[2] = 4.
Answer to 3rd query: powers[0] * powers[1] * powers[2] * powers[3] = 1 * 2 * 4 * 8 = 64.
Each answer modulo 10^9 + 7 yields the same answer, so [2,4,64] is returned.
Example 2:
Input: n = 2, queries = [[0,0]]
Output: [2]
Explanation:
For n = 2, powers = [2].
The answer to the only query is powers[0] = 2. The answer modulo 10^9 + 7 is the same, so [2] is returned.
Constraints:
•
1 <= n <= 10^9•
1 <= queries.length <= 10^5•
0 <= start_i <= end_i < powers.length