Eliminate Maximum Number of Monsters - Problem

You are playing a video game where you are defending your city from a group of n monsters. You are given a 0-indexed integer array dist of size n, where dist[i] is the initial distance in kilometers of the ith monster from the city.

The monsters walk toward the city at a constant speed. The speed of each monster is given to you in an integer array speed of size n, where speed[i] is the speed of the ith monster in kilometers per minute.

You have a weapon that, once fully charged, can eliminate a single monster. However, the weapon takes one minute to charge. The weapon is fully charged at the very start.

You lose when any monster reaches your city. If a monster reaches the city at the exact moment the weapon is fully charged, it counts as a loss, and the game ends before you can use your weapon.

Return the maximum number of monsters that you can eliminate before you lose, or n if you can eliminate all the monsters before they reach the city.

Input & Output

Example 1 — Basic Case
$ Input: dist = [1,3,4], speed = [1,1,1]
Output: 2
💡 Note: Monster 0 arrives at time 1, Monster 1 at time 3, Monster 2 at time 4. We can eliminate Monster 0 at time 0→1, Monster 1 at time 1→2. Monster 2 would arrive at time 4, but we can't charge until time 3, so we can eliminate it at time 2→3. Actually, we eliminate 2 monsters before losing.
Example 2 — All Monsters Eliminated
$ Input: dist = [3,2,4], speed = [5,3,2]
Output: 3
💡 Note: Arrival times: Monster 0 at 0.6min, Monster 1 at 0.67min, Monster 2 at 2min. We eliminate Monster 0 at time 0→1, Monster 1 at time 1→2, Monster 2 at time 2→3. All monsters eliminated before they reach the city.
Example 3 — Early Arrival
$ Input: dist = [4,2,3], speed = [2,1,1]
Output: 2
💡 Note: Arrival times: Monster 0 at 2min, Monster 1 at 2min, Monster 2 at 3min. We can eliminate Monster 0 at time 0→1 and Monster 1 at time 1→2, but Monster 2 arrives at time 3 which equals our charge time, so we lose before eliminating it.

Constraints

  • n == dist.length == speed.length
  • 1 ≤ n ≤ 105
  • 1 ≤ dist[i], speed[i] ≤ 105

Visualization

Tap to expand
Eliminate Maximum Number of MonstersCITY🏰M0dist=1, speed=1arrives: 1minM1dist=3, speed=1arrives: 3minM2dist=4, speed=1arrives: 4minGreedy Strategy: Eliminate Most Urgent FirstTime 0→1:Eliminate M0Time 1→2:Eliminate M1Time 2→3:Can eliminate M2Actually all 3!Result: 3 monsters eliminatedSort by arrival time, then eliminate in that order
Understanding the Visualization
1
Input
Monsters at different distances with different speeds
2
Strategy
Calculate arrival times and prioritize most urgent
3
Output
Maximum monsters eliminated before losing
Key Takeaway
🎯 Key Insight: Always eliminate the monster that will arrive soonest to maximize survival time
Asked in
Google 15 Amazon 12 Microsoft 8 Apple 6
28.5K Views
Medium Frequency
~15 min Avg. Time
890 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