You are a hero in a game, initially having HP health points and K magic potions.
You have to fight N monsters in the order they are given, knowing the damage each monster deals to you.
damage[i] represents the damage the monster deals to you when you fight them. If the damage is negative, it means your HP increases by that amount.
If you use a magic potion on a monster, it will kill the monster without you fighting or losing health points.
If you reach zero HP, you die. Therefore you can't win agains a monster that has damage[i] >= HP
Find the maximum number of monsters you can kill, considering you are playing optimally.
Example
Input: K = 2, HP = 10,
damage = [-3, 2, 3, -2, 8, 8, 6, 4, 3, 3]
Output: 8
Explanation: You kill the first 8 monsters by using the two potions on the 6th and 7th monstersWalkthroughs of the solution. Try the steps first, then watch to compare approaches.
Solution #1 · O(n log n)
Free plan: 30 tutor messages. Premium is unlimited.