Minimum Time to Kill All Monsters - Problem
You are given an integer array power where power[i] is the power of the ith monster.
You start with 0 mana points, and each day you increase your mana points by gain where gain initially is equal to 1.
Each day, after gaining gain mana, you can defeat a monster if your mana points are greater than or equal to the power of that monster. When you defeat a monster:
- Your mana points will be reset to 0
- The value of
gainincreases by 1
Return the minimum number of days needed to defeat all the monsters.
Input & Output
Example 1 — Basic Case
$
Input:
power = [3,1,4]
›
Output:
7
💡 Note:
Optimal order [4,3,1]: Day 1-4 build mana to defeat monster with power 4 (gain becomes 2), Day 5-6 build mana to defeat monster with power 3 (gain becomes 3), Day 7 defeat monster with power 1. Total: 7 days.
Example 2 — Small Array
$
Input:
power = [1,1,4]
›
Output:
4
💡 Note:
Optimal order [1,1,4]: Day 1 defeat first monster (gain=2), Day 2 defeat second monster (gain=3), Day 3-4 build mana to defeat monster with power 4. Total: 4 days.
Example 3 — Single Monster
$
Input:
power = [2]
›
Output:
2
💡 Note:
Only one monster with power 2. Need 2 days to build enough mana (gain=1 each day). Total: 2 days.
Constraints
- 1 ≤ power.length ≤ 17
- 1 ≤ power[i] ≤ 109
Visualization
Tap to expand
Understanding the Visualization
1
Input
Array of monster powers: [3,1,4]
2
Process
Try different defeat orders, mana resets after each kill
3
Output
Minimum days needed: 7
Key Takeaway
🎯 Key Insight: Order matters! Defeating stronger monsters first can be optimal when it saves more days overall.
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code