4008. Minimum Initial Strength to Defeat All Monsters
You are given an integer array
monsters, wheremonsters[i]represents the strength of thei^thmonster.You are also given a 2D integer array
boosts, whereboosts[i] = [l_i, r_i, v_i]indicates thatv_iis 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.
1 |
|