Stealth Interview
  • Features
  • Pricing
  • Blog
  • Login
  • Sign up

Leetcode #2403: Minimum Time to Kill All Monsters

In this guide, we solve Leetcode #2403 Minimum Time to Kill All Monsters in Python and focus on the core idea that makes the solution efficient.

You will see the intuition, the step-by-step method, and a clean Python implementation you can use in interviews.

Leetcode

Problem Statement

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.

Quick Facts

  • Difficulty: Hard
  • Premium: Yes
  • Tags: Bit Manipulation, Array, Dynamic Programming, Bitmask

Intuition

The problem breaks into overlapping subproblems, so caching results prevents exponential repetition.

A carefully chosen DP state captures exactly what we need to build the final answer.

Approach

Define the DP state and recurrence, then compute states in the correct order.

Optionally compress space once the recurrence is clear.

Steps:

  • Choose a DP state definition.
  • Write the recurrence and base cases.
  • Compute states in the correct order.

Example

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.

Python Solution

class Solution: def minimumTime(self, power: List[int]) -> int: @cache def dfs(mask: int) -> int: if mask == 0: return 0 ans = inf gain = 1 + (n - mask.bit_count()) for i, x in enumerate(power): if mask >> i & 1: ans = min(ans, dfs(mask ^ (1 << i)) + (x + gain - 1) // gain) return ans n = len(power) return dfs((1 << n) - 1)

Complexity

The time complexity is O(2n×n)O(2^n \times n)O(2n×n), and the space complexity is O(2n)O(2^n)O(2n). The space complexity is O(2n)O(2^n)O(2n).

Edge Cases and Pitfalls

Watch for boundary values, empty inputs, and duplicate values where applicable. If the problem involves ordering or constraints, confirm the invariant is preserved at every step.

Summary

This Python solution focuses on the essential structure of the problem and keeps the implementation interview-friendly while meeting the constraints.


Ace your next coding interview

We're here to help you ace your next coding interview.

Subscribe
Stealth Interview
© 2026 Stealth Interview®Stealth Interview is a registered trademark. All rights reserved.
Product
  • Blog
  • Pricing
Company
  • Terms of Service
  • Privacy Policy