Eliminate Maximum Number of Monsters - Problem

You're the last defender of your city, facing a horde of n monsters approaching from different distances! Each monster starts at a specific distance from your city and moves toward it at a constant speed.

You have a powerful weapon that can eliminate any monster with a single shot, but it takes exactly 1 minute to charge between shots. The weapon starts fully charged, so you can eliminate the first monster immediately.

The catch: If any monster reaches your city at the exact moment your weapon finishes charging, you lose the game before you can fire! You must eliminate monsters before they arrive.

Goal: Determine the maximum number of monsters you can eliminate before losing, or return n if you can eliminate all monsters.

Input: Two arrays of size n:

  • dist[i] - initial distance of monster i from the city (km)
  • speed[i] - speed of monster i (km/minute)

Output: Maximum number of monsters you can eliminate

Input & Output

example_1.py โ€” Basic Case
$ Input: dist = [1,3,4], speed = [1,1,1]
โ€บ Output: 3
๐Ÿ’ก Note: Monster arrival times: [1.0, 3.0, 4.0]. At time 0: eliminate monster at distance 1. At time 1: eliminate monster at distance 3. At time 2: eliminate monster at distance 4. All monsters eliminated successfully!
example_2.py โ€” Partial Success
$ Input: dist = [1,1,2,3], speed = [1,1,1,1]
โ€บ Output: 1
๐Ÿ’ก Note: Monster arrival times: [1.0, 1.0, 2.0, 3.0]. At time 0: eliminate first monster (arrives at 1.0). At time 1: second monster already arrived at time 1.0, so we lose. Maximum eliminated: 1.
example_3.py โ€” Different Speeds
$ Input: dist = [3,2,4], speed = [5,3,2]
โ€บ Output: 1
๐Ÿ’ก Note: Monster arrival times: [0.6, 0.67, 2.0]. At time 0: eliminate monster 0 (arrives at 0.6). At time 1: monster 1 already arrived at 0.67, game over. Maximum eliminated: 1.

Visualization

Tap to expand
Monster Defense StrategyCITYDefend!๐Ÿ”ซ๐Ÿ‘น2.0min๐Ÿ‘น3.0min๐Ÿ‘น4.0minTarget 1Strategy Timeline:Time 0: Eliminate Monster 1 (arrives at 2.0 min) โœ“Time 1: Eliminate Monster 0 (arrives at 3.0 min) โœ“Time 2: Eliminate Monster 2 (arrives at 4.0 min) โœ“Result: All 3 monsters eliminated!
Understanding the Visualization
1
Calculate Threat Level
Determine when each monster will arrive at your city
2
Prioritize Targets
Sort monsters by arrival time - most urgent first
3
Execute Strategy
Eliminate monsters in order, checking if each can be stopped in time
Key Takeaway
๐ŸŽฏ Key Insight: Always eliminate the monster that will reach your city first (considering when your weapon will be ready). This greedy strategy maximizes the number of monsters you can eliminate.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n log n)

Sorting the arrival times takes O(n log n), iteration takes O(n)

n
2n
โšก Linearithmic
Space Complexity
O(n)

Space for storing arrival times and sorting

n
2n
โšก Linearithmic Space

Constraints

  • n == dist.length == speed.length
  • 1 โ‰ค n โ‰ค 105
  • 1 โ‰ค dist[i], speed[i] โ‰ค 105
  • Time limit: 1000ms
Asked in
Amazon 15 Google 12 Microsoft 8 Meta 6
28.4K Views
Medium Frequency
~15 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