Minimum Time to Kill All Monsters - Problem
The Monster Hunter's Dilemma

You're a brave adventurer facing an array of monsters, each with a specific power[i] value representing the i-th monster's strength. Your journey begins with 0 mana points and a humble gain = 1 mana regeneration rate.

Daily Mechanics:
  • Each day, you gain gain mana points
  • After gaining mana, you can defeat any monster if your current mana โ‰ฅ monster's power
  • Upon defeating a monster: your mana resets to 0, but your gain increases by 1

Goal: Find the minimum number of days needed to defeat all monsters.

Strategy Matters! The order in which you defeat monsters significantly impacts the total time needed. Choose wisely!

Input & Output

example_1.py โ€” Basic Case
$ Input: [3, 1, 4]
โ€บ Output: 4
๐Ÿ’ก Note: Optimal strategy: Defeat monster with power 1 first (takes 1 day), then gain=2. Defeat monster with power 3 (takes 2 more days), then gain=3. Finally defeat monster with power 4 (takes 2 more days). Total: 1+2+2 = 5 days. Wait, let me recalculate... Actually, defeating power 1 first: day 1 we have mana=1, defeat it. Then gain=2. For power 3: day 2 mana=2, day 3 mana=4, defeat it. Then gain=3. For power 4: day 4 mana=3, day 5 mana=6, defeat it. But we can do better by starting with power 3: day 1 mana=1, day 2 mana=2, day 3 mana=3, defeat power 3. Then gain=2. Day 4 mana=2, defeat power 1. Then gain=3. Power 4 would take 2 more days, total = 4 days.
example_2.py โ€” Single Monster
$ Input: [5]
โ€บ Output: 5
๐Ÿ’ก Note: With only one monster of power 5, we need to accumulate mana day by day: Day 1: mana=1, Day 2: mana=2, Day 3: mana=3, Day 4: mana=4, Day 5: mana=5. We can defeat the monster on day 5.
example_3.py โ€” Multiple Weak Monsters
$ Input: [1, 1, 1]
โ€บ Output: 3
๐Ÿ’ก Note: All monsters have power 1. Day 1: mana=1, defeat first monster, gain becomes 2. Day 2: mana=2, defeat second monster, gain becomes 3. Day 3: mana=3, defeat third monster. Total: 3 days.

Visualization

Tap to expand
โš”๏ธ Monster Hunter Strategy โš”๏ธ๐Ÿ›ก๏ธHeroMana: 0Gain: 1๐Ÿ‘นPower: 1๐Ÿ‘บPower: 3๐ŸฒPower: 4Day 1: Attack!๐ŸŽฏ Optimal Strategy: Defeat weakest monsters first to maximize gain!Order: Monster(1) โ†’ Monster(3) โ†’ Monster(4) = 4 total daysDP with bitmasks explores all 3! = 6 possible sequences efficiently
Understanding the Visualization
1
Initial State
Start with 0 mana and gain=1. Plan your battle sequence wisely.
2
Daily Progress
Each day, gain mana equal to your current gain rate. Attack when ready!
3
Victory Rewards
After defeating a monster, mana resets to 0 but gain increases by 1.
4
Strategic Planning
Use DP with bitmasks to find the optimal battle sequence.
Key Takeaway
๐ŸŽฏ Key Insight: The order of defeating monsters significantly impacts the total time. Generally, defeating weaker monsters first allows you to build up your mana gain rate faster, leading to more efficient victories against stronger monsters later!

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n! ร— n)

n! permutations, each taking O(n) time to simulate

n
2n
โš  Quadratic Growth
Space Complexity
O(n)

Recursion stack depth and current path storage

n
2n
โšก Linearithmic Space

Constraints

  • 1 โ‰ค power.length โ‰ค 17
  • 1 โ‰ค power[i] โ‰ค 109
  • Order matters: Different defeat sequences lead to different total days
Asked in
Google 34 Meta 28 Amazon 22 Microsoft 19
28.4K Views
Medium-High Frequency
~25 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