Minimum Initial Strength to Defeat All Monsters Problem
Description
LeetCode Problem 4008.
You are given an integer array monsters, where monsters[i] represents the strength of the i^th monster.
You are also given a 2D integer array boosts, where boosts[i] = [l_i, r_i, v_i] indicates that v_i is added to your temporary bonus while fighting any monster whose index lies in [l_i, r_i]. Boost ranges may overlap, and the values of all applicable boosts are added together.
You start with a non-negative initial strength and fight the monsters from left to right.
For each monster at index i:
- Let
bonusbe the sum of the values of all boosts that apply to monsteri. - You can defeat the monster only if your current strength plus
bonusis at leastmonsters[i]. - After defeating the monster, only your current strength decreases by
monsters[i]. If it becomes negative, it is set to 0.
Return the minimum initial strength required to defeat all monsters.
Note: The temporary bonus is used only to determine whether the current monster can be defeated. It does not otherwise change your current strength.
Example 1:
Input: monsters = [5,10,15], boosts = [[1,1,10]]
Output: 30
Explanation:
Let's start with an initial strength of 30.
- monsters[0] = 5: At index 0, the bonus is 0. Since 30 + 0 >= 5, this monster can be defeated. The strength becomes 30 - 5 = 25.
- monsters[1] = 10: At index 1, the bonus is 10. Since 25 + 10 >= 10, this monster can be defeated. The strength becomes 25 - 10 = 15.
- monsters[2] = 15: At index 2, the bonus is 0. Since 15 + 0 >= 15, this monster can be defeated. The strength becomes 15 - 15 = 0.
Thus, the minimum initial strength required is 30.
Example 2:
Input: monsters = [5,10,15], boosts = [[1,2,10],[1,2,5]]
Output: 5
Explanation:
Let's start with an initial strength of 5.
- monsters[0] = 5: The bonus is 0. Since 5 + 0 >= 5, the monster can be defeated. The strength becomes 5 - 5 = 0.
- monsters[1] = 10: The two overlapping boosts provide bonus = 10 + 5 = 15. Since 0 + 15 >= 10, the monster can be defeated. The strength remains 0.
- monsters[2] = 15: The two overlapping boosts again provide bonus = 15. Since 0 + 15 >= 15, the monster can be defeated. The strength remains 0.
Thus, the minimum initial strength required is 5.
Constraints:
1 <= monsters.length <= 5 * 10^41 <= monsters[i] <= 10^90 <= boosts.length <= 5 * 10^4boosts[i] == [l_i, r_i, v_i]0 <= l_i <= r_i < monsters.length1 <= v_i <= 10^9
Solution
Approach: Difference Array + Binary Search
Every boost adds a value to a whole index range, so we apply all boosts to a difference array d. The bonus for monster i is then the prefix sum d[0] + ... + d[i].
Next we binary search the initial strength v. To test a value, simulate the fights from left to right while keeping the running bonus. If v + bonus < monsters[i], the monster wins and v is too small. Otherwise subtract monsters[i] from v and clamp it at 0. If every monster is beaten, v is enough.
More starting strength never hurts, so feasibility is monotonic and binary search finds the smallest feasible value. The sum of all monsters is at most 5 * 10^13, so 10^15 is a safe upper bound.
Complexity: time O((n + m) log M), space O(n), where n is the number of monsters, m the number of boosts and M = 10^15.
class Solution:
def minInitialStrength(self, monsters: List[int], boosts: List[List[int]]) -> int:
def check(v: int) -> bool:
bonus = 0
for a, b in zip(monsters, d):
bonus += b
if v + bonus < a:
return False
v -= a
v = max(v, 0)
return True
n = len(monsters)
d = [0] * (n + 1)
for l, r, v in boosts:
d[l] += v
d[r + 1] -= v
l, r = 0, 10**15
while l < r:
mid = (l + r) >> 1
if check(mid):
r = mid
else:
l = mid + 1
return l