650. Minimum Time to Kill All Monsters

An integer array called power is provided to you, with power[i] representing the ith monster's power.

You begin with zero mana points, and you acquire one every day, with the initial gain being one.

You can beat a monster every day after you've gained mana if your mana points are greater than or equal to the monster's power. When a monster is vanquished:

  • Your mana points will be reset to 0, and
  • The value of gain increases by 1.

Return the minimum number of days needed to defeat all the monsters.

Example 1:

Input : power = [1,2,4,9]

Output : 6

Explanation : The optimal way to beat all the monsters is to:

- Day 1: Gain 1 mana point to get a total of 1 mana point. Spend all mana points to kill the 1st monster.

- Day 2: Gain 2 mana points to get a total of 2 mana points. Spend all mana points to kill the 2nd monster.

- Day 3: Gain 3 mana points to get a total of 3 mana points.

- Day 4: Gain 3 mana points to get a total of 6 mana points.

- Day 5: Gain 3 mana points to get a total of 9 mana points. Spend all mana points to kill the 4th monster.

- Day 6: Gain 4 mana points to get a total of 4 mana points. Spend all mana points to kill the 3rd monster.

It can be proven that 6 is the minimum number of days needed.

Example 2:

Input : power = [3,1,4]

Output : 4

Explanation : The optimal way to beat all the monsters is to:

- Day 1: Gain 1 mana point to get a total of 1 mana point. Spend all mana points to kill the 2nd monster.

- Day 2: Gain 2 mana points to get a total of 2 mana points.

- Day 3: Gain 2 mana points to get a total of 4 mana points. Spend all mana points to kill the 3rd monster.

- Day 4: Gain 3 mana points to get a total of 3 mana points. Spend all mana points to kill the 1st monster.

It can be proven that 4 is the minimum number of days needed. 

Now Your Turn!

Pick the correct output for the given input

Input : nums = [4, 3, 10, 2]

Still unsure what the problem is asking ?

Let’s go through a few more examples, step by step, to make it clearer.

Constraints:

  • 1 <= power.length <= 17
  • 1 <= power[i] <= 109

Hints

Frequently Occurring Doubts

Interview Follow-up Questions

0
class Solution {
public:
long long minimumTime(vector<int>& power) {
//your code goes here
}
};
Test Case

Input:

Power