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 gain increases 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
Monster Defeat Strategy: power = [3,1,4]Bad Order: [3,1,4]Days: 3+1+2 = 6 wrongActually: 3+1+4 = 8 daysGood Order: [4,3,1]Days: 4+2+1 = 7 days ✓Optimal strategyAnother Order: [1,3,4]Days: 1+2+2 = 5 wrongActually: 1+2+4 = 7 daysSimulation:Days 1-4: Build manaDay 4: Defeat power=4Days 5-6: Build manaDay 6: Defeat power=3mana: 1→2→3→4gain: 1→2, mana→0mana: 2→4gain: 2→3, mana→0Day 7: Defeat power=1Total: 7 Days
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.
Asked in
Google 45 Amazon 38 Microsoft 32
28.5K Views
Medium Frequency
~35 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen