Eliminate Maximum Number of Monsters - Complete Solution Guide
Eliminate Maximum Number of Monsters is LeetCode problem 1921, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.
Problem Statement
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 i th 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 i th monster in kilometers per minute. You have a weapon that, once fully charged, can eliminat
Detailed Explanation
The problem describes a scenario where you are defending a city from monsters. Each monster has an initial distance from the city and a speed. You have a weapon that can eliminate one monster per minute, starting from the very beginning. The goal is to find the maximum number of monsters you can eliminate before any monster reaches the city. If a monster reaches the city at the exact moment you're charging your weapon, you lose. The inputs are two arrays: `dist` representing the initial distance of each monster, and `speed` representing the speed of each monster. The output is the maximum number of monsters that can be eliminated.
Solution Approach
The solution calculates the arrival time for each monster by dividing its distance by its speed. These arrival times are stored in an array and then sorted in ascending order. The code then iterates through the sorted arrival times. In each iteration, it checks if the arrival time of the current monster is less than or equal to the current time (which is also the number of monsters eliminated so far). If it is, it means the monster has reached the city (or is about to reach) and the game is lost. Therefore, we return the number of monsters eliminated up to that point. If all monsters can be eliminated before reaching the city, the function returns the total number of monsters.
Step-by-Step Algorithm
- Step 1: Calculate the arrival time for each monster by dividing its distance by its speed. Store these times in an array.
- Step 2: Sort the array of arrival times in ascending order.
- Step 3: Iterate through the sorted arrival times, with the index `i` representing the current minute (and also the number of monsters eliminated so far).
- Step 4: In each iteration, check if the arrival time of the current monster is less than or equal to `i`. If it is, return `i` (the number of monsters eliminated so far).
- Step 5: If the loop completes without returning, it means all monsters can be eliminated. Return the total number of monsters `n`.
Key Insights
- Insight 1: The key is to calculate the arrival time for each monster to the city. The arrival time is calculated as distance / speed.
- Insight 2: Sorting the arrival times is crucial. This allows us to prioritize eliminating monsters that will reach the city sooner.
- Insight 3: The time spent eliminating monsters increases by 1 minute with each eliminated monster. We need to make sure that at each time step `i`, the arrival time of the `i`-th monster is greater than `i` to avoid losing.
Complexity Analysis
Time Complexity: O(n log n)
Space Complexity: O(n)
Topics
This problem involves: Array, Greedy, Sorting.
Companies
Asked at: Agoda.