4008. Minimum Initial Strength to Defeat All Monsters
Description
You are given an integer array monsters, where monsters[i] represents the strength of the ith monster.
You are also given a 2D integer array boosts, where boosts[i] = [li, ri, vi] indicates that vi is added to your temporary bonus while fighting any monster whose index lies in [li, ri]. 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. Since30 + 0 >= 5, this monster can be defeated. The strength becomes30 - 5 = 25.monsters[1] = 10: At index 1, the bonus is 10. Since25 + 10 >= 10, this monster can be defeated. The strength becomes25 - 10 = 15.monsters[2] = 15: At index 2, the bonus is 0. Since15 + 0 >= 15, this monster can be defeated. The strength becomes15 - 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. Since5 + 0 >= 5, the monster can be defeated. The strength becomes5 - 5 = 0.monsters[1] = 10: The two overlapping boosts providebonus = 10 + 5 = 15. Since0 + 15 >= 10, the monster can be defeated. The strength remains 0.monsters[2] = 15: The two overlapping boosts again providebonus = 15. Since0 + 15 >= 15, the monster can be defeated. The strength remains 0.
Thus, the minimum initial strength required is 5.
Β
Constraints:
1 <= monsters.length <= 5 * 1041 <= monsters[i] <= 1090 <= boosts.length <= 5 * 104boosts[i] == [li, ri, vi]0 <= li <= ri < monsters.length1 <= vi <= 109βββββββ
Solutions
Solution 1: Difference Array + Binary Search
Each boost adds a value to an entire index range \([l, r]\), so we first apply all boosts using a difference array \(d\). The \(\textit{bonus}\) when fighting the \(i\)-th monster is then the prefix sum \(\sum_{j=0}^{i} d[j]\).
Next, we binary search the initial strength \(v\). For a given \(v\), we simulate the fights from left to right: maintain the current \(\textit{bonus}\) (the prefix sum of the difference array); if \(v + \textit{bonus} < \textit{monsters}[i]\), the monster cannot be defeated and \(v\) is infeasible; otherwise, we defeat it, decrease \(v\) by \(\textit{monsters}[i]\), and reset \(v\) to \(0\) if it becomes negative. If all monsters can be defeated, \(v\) is feasible.
A larger initial strength never makes it harder to defeat all monsters, so feasibility is monotonic in \(v\), and we can binary search the minimum feasible initial strength. The upper bound of the search is set to \(10^{15}\) (the total strength of all monsters is at most \(5 \times 10^4 \times 10^9 = 5 \times 10^{13}\)).
The time complexity is \(O((n + m) \times \log M)\), and the space complexity is \(O(n)\), where \(n\) is the number of monsters, \(m\) is the number of boosts, and \(M = 10^{15}\) is the upper bound of the binary search.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | |