2025-09-28
976. Largest Perimeter Triangle
Topic: Array, Math, Greedy, Sorting
Difficulty: Easy
Problem:
Given an integer array
Example 1:
Example 2:
Constraints:
•
•
  976. Largest Perimeter Triangle
Topic: Array, Math, Greedy, Sorting
Difficulty: Easy
Problem:
Given an integer array
nums, return the largest perimeter of a triangle with a non-zero area, formed from three of these lengths. If it is impossible to form any triangle of a non-zero area, return 0.Example 1:
Input: nums = [2,1,2]
Output: 5
Explanation: You can form a triangle with three side lengths: 1, 2, and 2.
Example 2:
Input: nums = [1,2,1,10]
Output: 0
Explanation:
You cannot use the side lengths 1, 1, and 2 to form a triangle.
You cannot use the side lengths 1, 1, and 10 to form a triangle.
You cannot use the side lengths 1, 2, and 10 to form a triangle.
As we cannot use any three side lengths to form a triangle of non-zero area, we return 0.
Constraints:
•
3 <= nums.length <= 10^4•
1 <= nums[i] <= 10^62025-09-29
1039. Minimum Score Triangulation of Polygon
Topic: Array, Dynamic Programming
Difficulty: Medium
Problem:
You have a convex
Polygon triangulation is a process where you divide a polygon into a set of triangles and the vertices of each triangle must also be vertices of the original polygon. Note that no other shapes other than triangles are allowed in the division. This process will result in
You will triangulate the polygon. For each triangle, the weight of that triangle is the product of the values at its vertices. The total score of the triangulation is the sum of these weights over all
Return the minimum possible score that you can achieve with some triangulation of the polygon.
Example 1:
Image: http://127.0.0.1:49174/shape1.jpg
Input: values = 1,2,3
Output: 6
Explanation: The polygon is already triangulated, and the score of the only triangle is 6.
Example 2:
Image: http://127.0.0.1:49174/shape2.jpg
Input: values = 3,7,4,5
Output: 144
Explanation: There are two triangulations, with possible scores: 375 + 457 = 245, or 345 + 347 = 144.
The minimum score is 144.
Example 3:
Image: http://127.0.0.1:49174/shape3.jpg
Input: values = 1,3,1,4,1,5
Output: 13
Explanation: The minimum score triangulation is 113 + 114 + 115 + 111 = 13.
Constraints:
•
•
•
  1039. Minimum Score Triangulation of Polygon
Topic: Array, Dynamic Programming
Difficulty: Medium
Problem:
You have a convex
n-sided polygon where each vertex has an integer value. You are given an integer array values where values[i] is the value of the i^th vertex in clockwise order.Polygon triangulation is a process where you divide a polygon into a set of triangles and the vertices of each triangle must also be vertices of the original polygon. Note that no other shapes other than triangles are allowed in the division. This process will result in
n - 2 triangles.You will triangulate the polygon. For each triangle, the weight of that triangle is the product of the values at its vertices. The total score of the triangulation is the sum of these weights over all
n - 2 triangles.Return the minimum possible score that you can achieve with some triangulation of the polygon.
Example 1:
Image: http://127.0.0.1:49174/shape1.jpg
Input: values = 1,2,3
Output: 6
Explanation: The polygon is already triangulated, and the score of the only triangle is 6.
Example 2:
Image: http://127.0.0.1:49174/shape2.jpg
Input: values = 3,7,4,5
Output: 144
Explanation: There are two triangulations, with possible scores: 375 + 457 = 245, or 345 + 347 = 144.
The minimum score is 144.
Example 3:
Image: http://127.0.0.1:49174/shape3.jpg
Input: values = 1,3,1,4,1,5
Output: 13
Explanation: The minimum score triangulation is 113 + 114 + 115 + 111 = 13.
Constraints:
•
n == values.length•
3 <= n <= 50•
1 <= values[i] <= 1002025-09-30
2221. Find Triangular Sum of an Array
Topic: Array, Math, Simulation, Combinatorics
Difficulty: Medium
Problem:
You are given a 0-indexed integer array
The triangular sum of
1. Let
2. For each index
3. Replace the array
4. Repeat the entire process starting from step 1.
Return the triangular sum of
Example 1:
Image: https://assets.leetcode.com/uploads/2022/02/22/ex1drawio.png
Example 2:
Constraints:
•
•
  2221. Find Triangular Sum of an Array
Topic: Array, Math, Simulation, Combinatorics
Difficulty: Medium
Problem:
You are given a 0-indexed integer array
nums, where nums[i] is a digit between 0 and 9 (inclusive).The triangular sum of
nums is the value of the only element present in nums after the following process terminates:1. Let
nums comprise of n elements. If n == 1, end the process. Otherwise, create a new 0-indexed integer array newNums of length n - 1.2. For each index
i, where 0 <= i < n - 1, assign the value of newNums[i] as (nums[i] + nums[i+1]) % 10, where % denotes modulo operator.3. Replace the array
nums with newNums.4. Repeat the entire process starting from step 1.
Return the triangular sum of
nums.Example 1:
Image: https://assets.leetcode.com/uploads/2022/02/22/ex1drawio.png
Input: nums = [1,2,3,4,5]
Output: 8
Explanation:
The above diagram depicts the process from which we obtain the triangular sum of the array.
Example 2:
Input: nums = [5]
Output: 5
Explanation:
Since there is only one element in nums, the triangular sum is the value of that element itself.
Constraints:
•
1 <= nums.length <= 1000•
0 <= nums[i] <= 92025-10-01
1518. Water Bottles
Topic: Math, Simulation
Difficulty: Easy
Problem:
There are
The operation of drinking a full water bottle turns it into an empty bottle.
Given the two integers
Example 1:
Image: https://assets.leetcode.com/uploads/2020/07/01/sample_1_1875.png
Example 2:
Image: https://assets.leetcode.com/uploads/2020/07/01/sample_2_1875.png
Constraints:
•
•
  1518. Water Bottles
Topic: Math, Simulation
Difficulty: Easy
Problem:
There are
numBottles water bottles that are initially full of water. You can exchange numExchange empty water bottles from the market with one full water bottle.The operation of drinking a full water bottle turns it into an empty bottle.
Given the two integers
numBottles and numExchange, return the maximum number of water bottles you can drink.Example 1:
Image: https://assets.leetcode.com/uploads/2020/07/01/sample_1_1875.png
Input: numBottles = 9, numExchange = 3
Output: 13
Explanation: You can exchange 3 empty bottles to get 1 full water bottle.
Number of water bottles you can drink: 9 + 3 + 1 = 13.
Example 2:
Image: https://assets.leetcode.com/uploads/2020/07/01/sample_2_1875.png
Input: numBottles = 15, numExchange = 4
Output: 19
Explanation: You can exchange 4 empty bottles to get 1 full water bottle.
Number of water bottles you can drink: 15 + 3 + 1 = 19.
Constraints:
•
1 <= numBottles <= 100•
2 <= numExchange <= 1002025-10-02
3100. Water Bottles II
Topic: Math, Simulation
Difficulty: Medium
Problem:
You are given two integers
• Drink any number of full water bottles turning them into empty bottles.
• Exchange
Note that you cannot exchange multiple batches of empty bottles for the same value of
Return the maximum number of water bottles you can drink.
Example 1:
Image: https://assets.leetcode.com/uploads/2024/01/28/exampleone1.png
Example 2:
Image: https://assets.leetcode.com/uploads/2024/01/28/example231.png
Constraints:
•
•
  3100. Water Bottles II
Topic: Math, Simulation
Difficulty: Medium
Problem:
You are given two integers
numBottles and numExchange.numBottles represents the number of full water bottles that you initially have. In one operation, you can perform one of the following operations:• Drink any number of full water bottles turning them into empty bottles.
• Exchange
numExchange empty bottles with one full water bottle. Then, increase numExchange by one.Note that you cannot exchange multiple batches of empty bottles for the same value of
numExchange. For example, if numBottles == 3 and numExchange == 1, you cannot exchange 3 empty water bottles for 3 full bottles.Return the maximum number of water bottles you can drink.
Example 1:
Image: https://assets.leetcode.com/uploads/2024/01/28/exampleone1.png
Input: numBottles = 13, numExchange = 6
Output: 15
Explanation: The table above shows the number of full water bottles, empty water bottles, the value of numExchange, and the number of bottles drunk.
Example 2:
Image: https://assets.leetcode.com/uploads/2024/01/28/example231.png
Input: numBottles = 10, numExchange = 3
Output: 13
Explanation: The table above shows the number of full water bottles, empty water bottles, the value of numExchange, and the number of bottles drunk.
Constraints:
•
1 <= numBottles <= 100•
1 <= numExchange <= 1002025-10-03
407. Trapping Rain Water II
Topic: Array, Breadth-First Search, Heap (Priority Queue), Matrix
Difficulty: Hard
Problem:
Given an
Example 1:
Image: https://assets.leetcode.com/uploads/2021/04/08/trap1-3d.jpg
Example 2:
Image: https://assets.leetcode.com/uploads/2021/04/08/trap2-3d.jpg
Constraints:
•
•
•
•
  407. Trapping Rain Water II
Topic: Array, Breadth-First Search, Heap (Priority Queue), Matrix
Difficulty: Hard
Problem:
Given an
m x n integer matrix heightMap representing the height of each unit cell in a 2D elevation map, return the volume of water it can trap after raining.Example 1:
Image: https://assets.leetcode.com/uploads/2021/04/08/trap1-3d.jpg
Input: heightMap = [[1,4,3,1,3,2],[3,2,1,3,2,4],[2,3,3,2,3,1]]
Output: 4
Explanation: After the rain, water is trapped between the blocks.
We have two small ponds 1 and 3 units trapped.
The total volume of water trapped is 4.
Example 2:
Image: https://assets.leetcode.com/uploads/2021/04/08/trap2-3d.jpg
Input: heightMap = [[3,3,3,3,3],[3,2,2,2,3],[3,2,1,2,3],[3,2,2,2,3],[3,3,3,3,3]]
Output: 10
Constraints:
•
m == heightMap.length•
n == heightMap[i].length•
1 <= m, n <= 200•
0 <= heightMap[i][j] <= 2 * 10^42025-10-04
11. Container With Most Water
Topic: Array, Two Pointers, Greedy
Difficulty: Medium
Problem:
You are given an integer array
Find two lines that together with the x-axis form a container, such that the container contains the most water.
Return the maximum amount of water a container can store.
Notice that you may not slant the container.
Example 1:
Image: https://s3-lc-upload.s3.amazonaws.com/uploads/2018/07/17/question_11.jpg
Example 2:
Constraints:
•
•
•
  11. Container With Most Water
Topic: Array, Two Pointers, Greedy
Difficulty: Medium
Problem:
You are given an integer array
height of length n. There are n vertical lines drawn such that the two endpoints of the i^th line are (i, 0) and (i, height[i]).Find two lines that together with the x-axis form a container, such that the container contains the most water.
Return the maximum amount of water a container can store.
Notice that you may not slant the container.
Example 1:
Image: https://s3-lc-upload.s3.amazonaws.com/uploads/2018/07/17/question_11.jpg
Input: height = [1,8,6,2,5,4,8,3,7]
Output: 49
Explanation: The above vertical lines are represented by array [1,8,6,2,5,4,8,3,7]. In this case, the max area of water (blue section) the container can contain is 49.
Example 2:
Input: height = [1,1]
Output: 1
Constraints:
•
n == height.length•
2 <= n <= 10^5•
0 <= height[i] <= 10^42025-10-05
417. Pacific Atlantic Water Flow
Topic: Array, Depth-First Search, Breadth-First Search, Matrix
Difficulty: Medium
Problem:
There is an
The island is partitioned into a grid of square cells. You are given an
The island receives a lot of rain, and the rain water can flow to neighboring cells directly north, south, east, and west if the neighboring cell's height is less than or equal to the current cell's height. Water can flow from any cell adjacent to an ocean into the ocean.
Return a 2D list of grid coordinates
Example 1:
Image: https://assets.leetcode.com/uploads/2021/06/08/waterflow-grid.jpg
Example 2:
Constraints:
•
•
•
•
  417. Pacific Atlantic Water Flow
Topic: Array, Depth-First Search, Breadth-First Search, Matrix
Difficulty: Medium
Problem:
There is an
m x n rectangular island that borders both the Pacific Ocean and Atlantic Ocean. The Pacific Ocean touches the island's left and top edges, and the Atlantic Ocean touches the island's right and bottom edges.The island is partitioned into a grid of square cells. You are given an
m x n integer matrix heights where heights[r][c] represents the height above sea level of the cell at coordinate (r, c).The island receives a lot of rain, and the rain water can flow to neighboring cells directly north, south, east, and west if the neighboring cell's height is less than or equal to the current cell's height. Water can flow from any cell adjacent to an ocean into the ocean.
Return a 2D list of grid coordinates
result where result[i] = [r_i, c_i] denotes that rain water can flow from cell (r_i, c_i) to both the Pacific and Atlantic oceans.Example 1:
Image: https://assets.leetcode.com/uploads/2021/06/08/waterflow-grid.jpg
Input: heights = [[1,2,2,3,5],[3,2,3,4,4],[2,4,5,3,1],[6,7,1,4,5],[5,1,1,2,4]]
Output: [[0,4],[1,3],[1,4],[2,2],[3,0],[3,1],[4,0]]
Explanation: The following cells can flow to the Pacific and Atlantic oceans, as shown below:
[0,4]: [0,4] -> Pacific Ocean
[0,4] -> Atlantic Ocean
[1,3]: [1,3] -> [0,3] -> Pacific Ocean
[1,3] -> [1,4] -> Atlantic Ocean
[1,4]: [1,4] -> [1,3] -> [0,3] -> Pacific Ocean
[1,4] -> Atlantic Ocean
[2,2]: [2,2] -> [1,2] -> [0,2] -> Pacific Ocean
[2,2] -> [2,3] -> [2,4] -> Atlantic Ocean
[3,0]: [3,0] -> Pacific Ocean
[3,0] -> [4,0] -> Atlantic Ocean
[3,1]: [3,1] -> [3,0] -> Pacific Ocean
[3,1] -> [4,1] -> Atlantic Ocean
[4,0]: [4,0] -> Pacific Ocean
[4,0] -> Atlantic Ocean
Note that there are other possible paths for these cells to flow to the Pacific and Atlantic oceans.
Example 2:
Input: heights = [[1]]
Output: [[0,0]]
Explanation: The water can flow from the only cell to the Pacific and Atlantic oceans.
Constraints:
•
m == heights.length•
n == heights[r].length•
1 <= m, n <= 200•
0 <= heights[r][c] <= 10^52025-10-06
778. Swim in Rising Water
Topic: Array, Binary Search, Depth-First Search, Breadth-First Search, Union Find, Heap (Priority Queue), Matrix
Difficulty: Hard
Problem:
You are given an
It starts raining, and water gradually rises over time. At time
You can swim from a square to another 4-directionally adjacent square if and only if the elevation of both squares individually are at most
Return the minimum time until you can reach the bottom right square
Example 1:
Image: https://assets.leetcode.com/uploads/2021/06/29/swim1-grid.jpg
Example 2:
Image: https://assets.leetcode.com/uploads/2021/06/29/swim2-grid-1.jpg
Constraints:
•
•
•
•
• Each value
  778. Swim in Rising Water
Topic: Array, Binary Search, Depth-First Search, Breadth-First Search, Union Find, Heap (Priority Queue), Matrix
Difficulty: Hard
Problem:
You are given an
n x n integer matrix grid where each value grid[i][j] represents the elevation at that point (i, j).It starts raining, and water gradually rises over time. At time
t, the water level is t, meaning any cell with elevation less than equal to t is submerged or reachable.You can swim from a square to another 4-directionally adjacent square if and only if the elevation of both squares individually are at most
t. You can swim infinite distances in zero time. Of course, you must stay within the boundaries of the grid during your swim.Return the minimum time until you can reach the bottom right square
(n - 1, n - 1) if you start at the top left square (0, 0).Example 1:
Image: https://assets.leetcode.com/uploads/2021/06/29/swim1-grid.jpg
Input: grid = [[0,2],[1,3]]
Output: 3
Explanation:
At time 0, you are in grid location (0, 0).
You cannot go anywhere else because 4-directionally adjacent neighbors have a higher elevation than t = 0.
You cannot reach point (1, 1) until time 3.
When the depth of water is 3, we can swim anywhere inside the grid.
Example 2:
Image: https://assets.leetcode.com/uploads/2021/06/29/swim2-grid-1.jpg
Input: grid = [[0,1,2,3,4],[24,23,22,21,5],[12,13,14,15,16],[11,17,18,19,20],[10,9,8,7,6]]
Output: 16
Explanation: The final route is shown.
We need to wait until time 16 so that (0, 0) and (4, 4) are connected.
Constraints:
•
n == grid.length•
n == grid[i].length•
1 <= n <= 50•
0 <= grid[i][j] < n^2• Each value
grid[i][j] is unique.2025-10-07
1488. Avoid Flood in The City
Topic: Array, Hash Table, Binary Search, Greedy, Heap (Priority Queue)
Difficulty: Medium
Problem:
Your country has an infinite number of lakes. Initially, all the lakes are empty, but when it rains over the
Given an integer array
•
•
Return an array
•
•
•
If there are multiple valid answers return any of them. If it is impossible to avoid flood return an empty array.
Notice that if you chose to dry a full lake, it becomes empty, but if you chose to dry an empty lake, nothing changes.
Example 1:
Example 2:
Example 3:
Constraints:
•
•
  1488. Avoid Flood in The City
Topic: Array, Hash Table, Binary Search, Greedy, Heap (Priority Queue)
Difficulty: Medium
Problem:
Your country has an infinite number of lakes. Initially, all the lakes are empty, but when it rains over the
nth lake, the nth lake becomes full of water. If it rains over a lake that is full of water, there will be a flood. Your goal is to avoid floods in any lake.Given an integer array
rains where:•
rains[i] > 0 means there will be rains over the rains[i] lake.•
rains[i] == 0 means there are no rains this day and you can choose one lake this day and dry it.Return an array
ans where:•
ans.length == rains.length•
ans[i] == -1 if rains[i] > 0.•
ans[i] is the lake you choose to dry in the ith day if rains[i] == 0.If there are multiple valid answers return any of them. If it is impossible to avoid flood return an empty array.
Notice that if you chose to dry a full lake, it becomes empty, but if you chose to dry an empty lake, nothing changes.
Example 1:
Input: rains = [1,2,3,4]
Output: [-1,-1,-1,-1]
Explanation: After the first day full lakes are [1]
After the second day full lakes are [1,2]
After the third day full lakes are [1,2,3]
After the fourth day full lakes are [1,2,3,4]
There's no day to dry any lake and there is no flood in any lake.
Example 2:
Input: rains = [1,2,0,0,2,1]
Output: [-1,-1,2,1,-1,-1]
Explanation: After the first day full lakes are [1]
After the second day full lakes are [1,2]
After the third day, we dry lake 2. Full lakes are [1]
After the fourth day, we dry lake 1. There is no full lakes.
After the fifth day, full lakes are [2].
After the sixth day, full lakes are [1,2].
It is easy that this scenario is flood-free. [-1,-1,1,2,-1,-1] is another acceptable scenario.
Example 3:
Input: rains = [1,2,0,1,2]
Output: []
Explanation: After the second day, full lakes are [1,2]. We have to dry one lake in the third day.
After that, it will rain over lakes [1,2]. It's easy to prove that no matter which lake you choose to dry in the 3rd day, the other one will flood.
Constraints:
•
1 <= rains.length <= 10^5•
0 <= rains[i] <= 10^92025-10-08
2300. Successful Pairs of Spells and Potions
Topic: Array, Two Pointers, Binary Search, Sorting
Difficulty: Medium
Problem:
You are given two positive integer arrays
You are also given an integer
Return an integer array
Example 1:
Example 2:
Constraints:
•
•
•
•
•
  2300. Successful Pairs of Spells and Potions
Topic: Array, Two Pointers, Binary Search, Sorting
Difficulty: Medium
Problem:
You are given two positive integer arrays
spells and potions, of length n and m respectively, where spells[i] represents the strength of the i^th spell and potions[j] represents the strength of the j^th potion.You are also given an integer
success. A spell and potion pair is considered successful if the product of their strengths is at least success.Return an integer array
pairs of length n where pairs[i] is the number of potions that will form a successful pair with the i^th spell.Example 1:
Input: spells = [5,1,3], potions = [1,2,3,4,5], success = 7
Output: [4,0,3]
Explanation:
- 0^th spell: 5 * [1,2,3,4,5] = [5,10,15,20,25]. 4 pairs are successful.
- 1^st spell: 1 * [1,2,3,4,5] = [1,2,3,4,5]. 0 pairs are successful.
- 2^nd spell: 3 * [1,2,3,4,5] = [3,6,9,12,15]. 3 pairs are successful.
Thus, [4,0,3] is returned.
Example 2:
Input: spells = [3,1,2], potions = [8,5,8], success = 16
Output: [2,0,2]
Explanation:
- 0^th spell: 3 * [8,5,8] = [24,15,24]. 2 pairs are successful.
- 1^st spell: 1 * [8,5,8] = [8,5,8]. 0 pairs are successful.
- 2^nd spell: 2 * [8,5,8] = [16,10,16]. 2 pairs are successful.
Thus, [2,0,2] is returned.
Constraints:
•
n == spells.length•
m == potions.length•
1 <= n, m <= 10^5•
1 <= spells[i], potions[i] <= 10^5•
1 <= success <= 10^102025-10-09
3494. Find the Minimum Amount of Time to Brew Potions
Topic: Array, Simulation, Prefix Sum
Difficulty: Medium
Problem:
You are given two integer arrays,
In a laboratory,
Since the brewing process is delicate, a potion must be passed to the next wizard immediately after the current wizard completes their work. This means the timing must be synchronized so that each wizard begins working on a potion exactly when it arrives. 
Return the minimum amount of time required for the potions to be brewed properly.
Example 1:
Input: skill = 1,5,2,4, mana = 5,1,4,2
Output: 110
Explanation:
Potion NumberStart timeWizard 0 done byWizard 1 done byWizard 2 done byWizard 3 done by005304060152535860642545878861023868898102110
As an example for why wizard 0 cannot start working on the 1^st potion before time
Example 2:
Input: skill = 1,1,1, mana = 1,1,1
Output: 5
Explanation:
1. Preparation of the 0^th potion begins at time
2. Preparation of the 1^st potion begins at time
3. Preparation of the 2^nd potion begins at time
Example 3:
Input: skill = 1,2,3,4, mana = 1,2
Output: 21
Constraints:
•
•
•
•
  3494. Find the Minimum Amount of Time to Brew Potions
Topic: Array, Simulation, Prefix Sum
Difficulty: Medium
Problem:
You are given two integer arrays,
skill and mana, of length n and m, respectively.In a laboratory,
n wizards must brew m potions in order. Each potion has a mana capacity mana[j] and must pass through all the wizards sequentially to be brewed properly. The time taken by the i^th wizard on the j^th potion is time_ij = skill[i] * mana[j].Since the brewing process is delicate, a potion must be passed to the next wizard immediately after the current wizard completes their work. This means the timing must be synchronized so that each wizard begins working on a potion exactly when it arrives. 
Return the minimum amount of time required for the potions to be brewed properly.
Example 1:
Input: skill = 1,5,2,4, mana = 5,1,4,2
Output: 110
Explanation:
Potion NumberStart timeWizard 0 done byWizard 1 done byWizard 2 done byWizard 3 done by005304060152535860642545878861023868898102110
As an example for why wizard 0 cannot start working on the 1^st potion before time
t = 52, consider the case where the wizards started preparing the 1^st potion at time t = 50. At time t = 58, wizard 2 is done with the 1^st potion, but wizard 3 will still be working on the 0^th potion till time t = 60.Example 2:
Input: skill = 1,1,1, mana = 1,1,1
Output: 5
Explanation:
1. Preparation of the 0^th potion begins at time
t = 0, and is completed by time t = 3.2. Preparation of the 1^st potion begins at time
t = 1, and is completed by time t = 4.3. Preparation of the 2^nd potion begins at time
t = 2, and is completed by time t = 5.Example 3:
Input: skill = 1,2,3,4, mana = 1,2
Output: 21
Constraints:
•
n == skill.length•
m == mana.length•
1 <= n, m <= 5000•
1 <= mana[i], skill[i] <= 50002025-10-10
3147. Taking Maximum Energy From the Mystic Dungeon
Topic: Array, Prefix Sum
Difficulty: Medium
Problem:
In a mystic dungeon,
You have been cursed in such a way that after absorbing energy from magician
In other words, you will choose a starting point and then teleport with
You are given an array
Note that when you are reach a magician, you must take energy from them, whether it is negative or positive energy.
Example 1:
Input: energy = 5,2,-10,-5,1, k = 3
Output: 3
Explanation: We can gain a total energy of 3 by starting from magician 1 absorbing 2 + 1 = 3.
Example 2:
Input: energy = -2,-3,-1, k = 2
Output: -1
Explanation: We can gain a total energy of -1 by starting from magician 2.
Constraints:
•
•
•
  3147. Taking Maximum Energy From the Mystic Dungeon
Topic: Array, Prefix Sum
Difficulty: Medium
Problem:
In a mystic dungeon,
n magicians are standing in a line. Each magician has an attribute that gives you energy. Some magicians can give you negative energy, which means taking energy from you.You have been cursed in such a way that after absorbing energy from magician
i, you will be instantly transported to magician (i + k). This process will be repeated until you reach the magician where (i + k) does not exist.In other words, you will choose a starting point and then teleport with
k jumps until you reach the end of the magicians' sequence, absorbing all the energy during the journey.You are given an array
energy and an integer k. Return the maximum possible energy you can gain.Note that when you are reach a magician, you must take energy from them, whether it is negative or positive energy.
Example 1:
Input: energy = 5,2,-10,-5,1, k = 3
Output: 3
Explanation: We can gain a total energy of 3 by starting from magician 1 absorbing 2 + 1 = 3.
Example 2:
Input: energy = -2,-3,-1, k = 2
Output: -1
Explanation: We can gain a total energy of -1 by starting from magician 2.
Constraints:
•
1 <= energy.length <= 10^5•
-1000 <= energy[i] <= 1000•
1 <= k <= energy.length - 1
2025-10-11
3186. Maximum Total Damage With Spell Casting
Topic: Array, Hash Table, Two Pointers, Binary Search, Dynamic Programming, Sorting, Counting
Difficulty: Medium
Problem:
A magician has various spells.
You are given an array
It is a known fact that if a magician decides to cast a spell with a damage of
Each spell can be cast only once.
Return the maximum possible total damage that a magician can cast.
Example 1:
Input: power = 1,1,3,4
Output: 6
Explanation:
The maximum possible damage of 6 is produced by casting spells 0, 1, 3 with damage 1, 1, 4.
Example 2:
Input: power = 7,1,6,6
Output: 13
Explanation:
The maximum possible damage of 13 is produced by casting spells 1, 2, 3 with damage 1, 6, 6.
Constraints:
•
•
  3186. Maximum Total Damage With Spell Casting
Topic: Array, Hash Table, Two Pointers, Binary Search, Dynamic Programming, Sorting, Counting
Difficulty: Medium
Problem:
A magician has various spells.
You are given an array
power, where each element represents the damage of a spell. Multiple spells can have the same damage value.It is a known fact that if a magician decides to cast a spell with a damage of
power[i], they cannot cast any spell with a damage of power[i] - 2, power[i] - 1, power[i] + 1, or power[i] + 2.Each spell can be cast only once.
Return the maximum possible total damage that a magician can cast.
Example 1:
Input: power = 1,1,3,4
Output: 6
Explanation:
The maximum possible damage of 6 is produced by casting spells 0, 1, 3 with damage 1, 1, 4.
Example 2:
Input: power = 7,1,6,6
Output: 13
Explanation:
The maximum possible damage of 13 is produced by casting spells 1, 2, 3 with damage 1, 6, 6.
Constraints:
•
1 <= power.length <= 10^5•
1 <= power[i] <= 10^92025-10-12
3539. Find Sum of Array Product of Magical Sequences
Topic: Array, Math, Dynamic Programming, Bit Manipulation, Combinatorics, Bitmask
Difficulty: Hard
Problem:
You are given two integers,
A sequence of integers
•
•
• The binary representation of
The array product of this sequence is defined as
Return the sum of the array products for all valid magical sequences.
Since the answer may be large, return it modulo
A set bit refers to a bit in the binary representation of a number that has a value of 1.
Example 1:
Input: m = 5, k = 5, nums = 1,10,100,10000,1000000
Output: 991600007
Explanation:
All permutations of
Example 2:
Input: m = 2, k = 2, nums = 5,4,3,2,1
Output: 170
Explanation:
The magical sequences are
Example 3:
Input: m = 1, k = 1, nums = 28
Output: 28
Explanation:
The only magical sequence is
Constraints:
•
•
•
  3539. Find Sum of Array Product of Magical Sequences
Topic: Array, Math, Dynamic Programming, Bit Manipulation, Combinatorics, Bitmask
Difficulty: Hard
Problem:
You are given two integers,
m and k, and an integer array nums.A sequence of integers
seq is called magical if:•
seq has a size of m.•
0 <= seq[i] < nums.length• The binary representation of
2^seq[0] + 2^seq[1] + ... + 2^seq[m - 1] has k set bits.The array product of this sequence is defined as
prod(seq) = (nums[seq[0]] * nums[seq[1]] * ... * nums[seq[m - 1]]).Return the sum of the array products for all valid magical sequences.
Since the answer may be large, return it modulo
10^9 + 7.A set bit refers to a bit in the binary representation of a number that has a value of 1.
Example 1:
Input: m = 5, k = 5, nums = 1,10,100,10000,1000000
Output: 991600007
Explanation:
All permutations of
[0, 1, 2, 3, 4] are magical sequences, each with an array product of 10^13.Example 2:
Input: m = 2, k = 2, nums = 5,4,3,2,1
Output: 170
Explanation:
The magical sequences are
[0, 1], [0, 2], [0, 3], [0, 4], [1, 0], [1, 2], [1, 3], [1, 4], [2, 0], [2, 1], [2, 3], [2, 4], [3, 0], [3, 1], [3, 2], [3, 4], [4, 0], [4, 1], [4, 2], and [4, 3].Example 3:
Input: m = 1, k = 1, nums = 28
Output: 28
Explanation:
The only magical sequence is
[0].Constraints:
•
1 <= k <= m <= 30•
1 <= nums.length <= 50•
1 <= nums[i] <= 10^82025-10-13
2273. Find Resultant Array After Removing Anagrams
Topic: Array, Hash Table, String, Sorting
Difficulty: Easy
Problem:
You are given a 0-indexed string array
In one operation, select any index
Return
An Anagram is a word or phrase formed by rearranging the letters of a different word or phrase using all the original letters exactly once. For example,
Example 1:
Example 2:
Constraints:
•
•
•
  2273. Find Resultant Array After Removing Anagrams
Topic: Array, Hash Table, String, Sorting
Difficulty: Easy
Problem:
You are given a 0-indexed string array
words, where words[i] consists of lowercase English letters.In one operation, select any index
i such that 0 < i < words.length and words[i - 1] and words[i] are anagrams, and delete words[i] from words. Keep performing this operation as long as you can select an index that satisfies the conditions.Return
words after performing all operations. It can be shown that selecting the indices for each operation in any arbitrary order will lead to the same result.An Anagram is a word or phrase formed by rearranging the letters of a different word or phrase using all the original letters exactly once. For example,
"dacb" is an anagram of "abdc".Example 1:
Input: words = ["abba","baba","bbaa","cd","cd"]
Output: ["abba","cd"]
Explanation:
One of the ways we can obtain the resultant array is by using the following operations:
- Since words[2] = "bbaa" and words[1] = "baba" are anagrams, we choose index 2 and delete words[2].
Now words = ["abba","baba","cd","cd"].
- Since words[1] = "baba" and words[0] = "abba" are anagrams, we choose index 1 and delete words[1].
Now words = ["abba","cd","cd"].
- Since words[2] = "cd" and words[1] = "cd" are anagrams, we choose index 2 and delete words[2].
Now words = ["abba","cd"].
We can no longer perform any operations, so ["abba","cd"] is the final answer.
Example 2:
Input: words = ["a","b","c","d","e"]
Output: ["a","b","c","d","e"]
Explanation:
No two adjacent strings in words are anagrams of each other, so no operations are performed.
Constraints:
•
1 <= words.length <= 100•
1 <= words[i].length <= 10•
words[i] consists of lowercase English letters.2025-10-14
3349. Adjacent Increasing Subarrays Detection I
Topic: Array
Difficulty: Easy
Problem:
Given an array
• Both subarrays
• The subarrays must be adjacent, meaning
Return
Example 1:
Input: nums = 2,5,7,8,9,2,3,4,3,1, k = 3
Output: true
Explanation:
• The subarray starting at index
• The subarray starting at index
• These two subarrays are adjacent, so the result is
Example 2:
Input: nums = 1,2,3,4,4,4,4,5,6,7, k = 5
Output: false
Constraints:
•
•
•
  3349. Adjacent Increasing Subarrays Detection I
Topic: Array
Difficulty: Easy
Problem:
Given an array
nums of n integers and an integer k, determine whether there exist two adjacent subarrays of length k such that both subarrays are strictly increasing. Specifically, check if there are two subarrays starting at indices a and b (a < b), where:• Both subarrays
nums[a..a + k - 1] and nums[b..b + k - 1] are strictly increasing.• The subarrays must be adjacent, meaning
b = a + k.Return
true if it is possible to find two such subarrays, and false otherwise.Example 1:
Input: nums = 2,5,7,8,9,2,3,4,3,1, k = 3
Output: true
Explanation:
• The subarray starting at index
2 is [7, 8, 9], which is strictly increasing.• The subarray starting at index
5 is [2, 3, 4], which is also strictly increasing.• These two subarrays are adjacent, so the result is
true.Example 2:
Input: nums = 1,2,3,4,4,4,4,5,6,7, k = 5
Output: false
Constraints:
•
2 <= nums.length <= 100•
1 < 2 * k <= nums.length•
-1000 <= nums[i] <= 10002025-10-15
3350. Adjacent Increasing Subarrays Detection II
Topic: Array, Binary Search
Difficulty: Medium
Problem:
Given an array
• Both subarrays
• The subarrays must be adjacent, meaning
Return the maximum possible value of
A subarray is a contiguous non-empty sequence of elements within an array.
Example 1:
Input: nums = 2,5,7,8,9,2,3,4,3,1
Output: 3
Explanation:
• The subarray starting at index 2 is
• The subarray starting at index 5 is
• These two subarrays are adjacent, and 3 is the maximum possible value of
Example 2:
Input: nums = 1,2,3,4,4,4,4,5,6,7
Output: 2
Explanation:
• The subarray starting at index 0 is
• The subarray starting at index 2 is
• These two subarrays are adjacent, and 2 is the maximum possible value of
Constraints:
•
•
  3350. Adjacent Increasing Subarrays Detection II
Topic: Array, Binary Search
Difficulty: Medium
Problem:
Given an array
nums of n integers, your task is to find the maximum value of k for which there exist two adjacent subarrays of length k each, such that both subarrays are strictly increasing. Specifically, check if there are two subarrays of length k starting at indices a and b (a < b), where:• Both subarrays
nums[a..a + k - 1] and nums[b..b + k - 1] are strictly increasing.• The subarrays must be adjacent, meaning
b = a + k.Return the maximum possible value of
k.A subarray is a contiguous non-empty sequence of elements within an array.
Example 1:
Input: nums = 2,5,7,8,9,2,3,4,3,1
Output: 3
Explanation:
• The subarray starting at index 2 is
[7, 8, 9], which is strictly increasing.• The subarray starting at index 5 is
[2, 3, 4], which is also strictly increasing.• These two subarrays are adjacent, and 3 is the maximum possible value of
k for which two such adjacent strictly increasing subarrays exist.Example 2:
Input: nums = 1,2,3,4,4,4,4,5,6,7
Output: 2
Explanation:
• The subarray starting at index 0 is
[1, 2], which is strictly increasing.• The subarray starting at index 2 is
[3, 4], which is also strictly increasing.• These two subarrays are adjacent, and 2 is the maximum possible value of
k for which two such adjacent strictly increasing subarrays exist.Constraints:
•
2 <= nums.length <= 2 * 10^5•
-10^9 <= nums[i] <= 10^92025-10-16
2598. Smallest Missing Non-negative Integer After Operations
Topic: Array, Hash Table, Math, Greedy
Difficulty: Medium
Problem:
You are given a 0-indexed integer array
In one operation, you can add or subtract
• For example, if
The MEX (minimum excluded) of an array is the smallest missing non-negative integer in it.
• For example, the MEX of
Return the maximum MEX of
Example 1:
Example 2:
Constraints:
•
•
  2598. Smallest Missing Non-negative Integer After Operations
Topic: Array, Hash Table, Math, Greedy
Difficulty: Medium
Problem:
You are given a 0-indexed integer array
nums and an integer value.In one operation, you can add or subtract
value from any element of nums.• For example, if
nums = [1,2,3] and value = 2, you can choose to subtract value from nums[0] to make nums = [-1,2,3].The MEX (minimum excluded) of an array is the smallest missing non-negative integer in it.
• For example, the MEX of
[-1,2,3] is 0 while the MEX of [1,0,3] is 2.Return the maximum MEX of
nums after applying the mentioned operation any number of times.Example 1:
Input: nums = [1,-10,7,13,6,8], value = 5
Output: 4
Explanation: One can achieve this result by applying the following operations:
- Add value to nums[1] twice to make nums = [1,0,7,13,6,8]
- Subtract value from nums[2] once to make nums = [1,0,2,13,6,8]
- Subtract value from nums[3] twice to make nums = [1,0,2,3,6,8]
The MEX of nums is 4. It can be shown that 4 is the maximum MEX we can achieve.
Example 2:
Input: nums = [1,-10,7,13,6,8], value = 7
Output: 2
Explanation: One can achieve this result by applying the following operation:
- subtract value from nums[2] once to make nums = [1,-10,0,13,6,8]
The MEX of nums is 2. It can be shown that 2 is the maximum MEX we can achieve.
Constraints:
•
1 <= nums.length, value <= 10^5•
-10^9 <= nums[i] <= 10^92025-10-17
3003. Maximize the Number of Partitions After Operations
Topic: String, Dynamic Programming, Bit Manipulation, Bitmask
Difficulty: Hard
Problem:
You are given a string
First, you are allowed to change at most one index in
After that, do the following partitioning operation until
• Choose the longest prefix of
• Delete the prefix from
Return an integer denoting the maximum number of resulting partitions after the operations by optimally choosing at most one index to change.
Example 1:
Input: s = "accca", k = 2
Output: 3
Explanation:
The optimal way is to change
Then we perform the operations:
1. The longest prefix containing at most 2 distinct characters is
2. Now The longest prefix containing at most 2 distinct characters is
3. Finally, we remove
Doing the operations, the string is divided into 3 partitions, so the answer is 3.
Example 2:
Input: s = "aabaab", k = 3
Output: 1
Explanation:
Initially
Example 3:
Input: s = "xxyz", k = 1
Output: 4
Explanation:
The optimal way is to change
Then
Constraints:
•
•
•
  3003. Maximize the Number of Partitions After Operations
Topic: String, Dynamic Programming, Bit Manipulation, Bitmask
Difficulty: Hard
Problem:
You are given a string
s and an integer k.First, you are allowed to change at most one index in
s to another lowercase English letter.After that, do the following partitioning operation until
s is empty:• Choose the longest prefix of
s containing at most k distinct characters.• Delete the prefix from
s and increase the number of partitions by one. The remaining characters (if any) in s maintain their initial order.Return an integer denoting the maximum number of resulting partitions after the operations by optimally choosing at most one index to change.
Example 1:
Input: s = "accca", k = 2
Output: 3
Explanation:
The optimal way is to change
s[2] to something other than a and c, for example, b. then it becomes "acbca".Then we perform the operations:
1. The longest prefix containing at most 2 distinct characters is
"ac", we remove it and s becomes "bca".2. Now The longest prefix containing at most 2 distinct characters is
"bc", so we remove it and s becomes "a".3. Finally, we remove
"a" and s becomes empty, so the procedure ends.Doing the operations, the string is divided into 3 partitions, so the answer is 3.
Example 2:
Input: s = "aabaab", k = 3
Output: 1
Explanation:
Initially
s contains 2 distinct characters, so whichever character we change, it will contain at most 3 distinct characters, so the longest prefix with at most 3 distinct characters would always be all of it, therefore the answer is 1.Example 3:
Input: s = "xxyz", k = 1
Output: 4
Explanation:
The optimal way is to change
s[0] or s[1] to something other than characters in s, for example, to change s[0] to w.Then
s becomes "wxyz", which consists of 4 distinct characters, so as k is 1, it will divide into 4 partitions.Constraints:
•
1 <= s.length <= 10^4•
s consists only of lowercase English letters.•
1 <= k <= 26