07. Eliminate Maximum Number of Monsters
The problem can be found at the following link: Question Link
My Approach
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]
byspeed[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 timei
for the current person. If it is, incrementt
by 1 to simulate the elimination of the person who arrives earliest.If the current time
t
is greater than or equal to the timei
for the current person, break out of the loop since you have eliminated the maximum number of people possible without causing a collision.
Time and Auxiliary Space Complexity
Time Complexity:
O(n)
Auxiliary Space Complexity:
O(n)
Code (C++)
Contribution and Support
For discussions, questions, or doubts related to this solution, please visit our discussion section. 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 rishabhv12/Daily-Leetcode-Solution repository.
Last updated