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 monsterifrom the city (km)speed[i]- speed of monsteri(km/minute)
Output: Maximum number of monsters you can eliminate
Input & Output
Visualization
Time & Space Complexity
Sorting the arrival times takes O(n log n), iteration takes O(n)
Space for storing arrival times and sorting
Constraints
- n == dist.length == speed.length
- 1 โค n โค 105
- 1 โค dist[i], speed[i] โค 105
- Time limit: 1000ms