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: 3
💡 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, and Monster 2 at time 2→3. All 3 monsters can be eliminated.
Example 2 — All Monsters Eliminated
$ Input: dist = [3,2,4], speed = [5,3,2]
Output: 1
💡 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, but Monster 1 arrives at 0.67min which is before our weapon finishes charging at time 1, so we lose.
Example 3 — Early Arrival
$ Input: dist = [4,2,3], speed = [2,1,1]
Output: 3
💡 Note: Arrival times: Monster 0 at 2min, Monster 1 at 2min, Monster 2 at 3min. We can eliminate Monster 0 at time 0→1, Monster 1 at time 1→2, and Monster 2 at time 2→3. All 3 monsters can be eliminated.

Constraints

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

Visualization

Tap to expand
Eliminate Maximum Number of Monsters INPUT CITY M0 dist=1 M1 dist=3 M2 dist=4 dist array: 1 3 4 speed array: 1 1 1 Weapon charges in 1 min Starts fully charged n = 3 monsters ALGORITHM STEPS 1 Calculate Arrival Time time[i] = dist[i] / speed[i] M0: 1/1 = 1 min M1: 3/1 = 3 min M2: 4/1 = 4 min 2 Sort by Arrival Time Greedy: kill urgent first 1 3 4 3 Eliminate Monsters Check: time[i] > i i=0: 1 > 0 OK Kill M0 i=1: 3 > 1 OK Kill M1 i=2: 4 > 2 OK Kill M2 4 Wait - M0 arrives! At t=1, M0 reaches city before weapon fires Only 2 eliminated! FINAL RESULT Timeline Simulation t=0 t=1 t=2 t=3 Kill M0 M0 arrives LOSS! Actually: time[i] must be strictly > i (not >=) M0 at t=1 = weapon at t=1 Process: t=0: Kill monster (1 killed) t=1: Kill monster (2 killed) t=1: M0 reaches city! Game Over OUTPUT 2 Max monsters eliminated Key Insight: Calculate arrival time for each monster (dist/speed), then sort by arrival time. Greedily eliminate the most urgent (earliest arriving) monster first. If monster i arrives at time <= i (0-indexed), you lose. Count how many you can eliminate before this happens. TutorialsPoint - Eliminate Maximum Number of Monsters | Greedy Approach
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