07. Eliminate Maximum Number of Monsters
Last updated
Was this helpful?
Last updated
Was this helpful?
The problem can be found at the following link:
Initialize a vector of integers time
to store the time it takes for each person to reach their destination.
Inside the loop, calculate the time it takes for the current person to reach their destination:
Calculate the floating-point value by dividing dist[i]
by speed[i]
.
Use ceil
to round the floating-point result to the nearest integer.
Store the rounded result in the time
vector.
After the loop, sort the time
vector in ascending order. This will give you a list of times in ascending order, representing when each person will reach their destination.
Initialize an integer t
to 0. This variable will be used to keep track of the current time.
Inside the loop, check if the current time t
is less than the time i
for the current person. If it is, increment t
by 1 to simulate the elimination of the person who arrives earliest.
If the current time t
is greater than or equal to the time i
for the current person, break out of the loop since you have eliminated the maximum number of people possible without causing a collision.
Time Complexity: O(n)
Auxiliary Space Complexity: O(n)
For discussions, questions, or doubts related to this solution, please visit our . We welcome your input and aim to foster a collaborative learning environment.
If you find this solution helpful, consider supporting us by giving a ⭐ star
to the repository.