Skip to content

Commit 96ce07f

Browse files
committed
Eliminate Maximum Number of Monsters
1 parent 3aa976c commit 96ce07f

File tree

1 file changed

+61
-0
lines changed

1 file changed

+61
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
## 07. Eliminate Maximum Number of Monsters
2+
3+
The problem can be found at the following link: [Question Link](https://leetcode.com/problems/eliminate-maximum-number-of-monsters/description/)
4+
5+
6+
### My Approach
7+
8+
9+
1. Initialize a vector of integers `time` to store the time it takes for each person to reach their destination.
10+
11+
2. Inside the loop, calculate the time it takes for the current person to reach their destination:
12+
- Calculate the floating-point value by dividing `dist[i]` by `speed[i]`.
13+
- Use `ceil` to round the floating-point result to the nearest integer.
14+
- Store the rounded result in the `time` vector.
15+
16+
4. 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.
17+
18+
5. Initialize an integer `t` to 0. This variable will be used to keep track of the current time.
19+
20+
6. 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.
21+
22+
8. 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.
23+
24+
25+
### Time and Auxiliary Space Complexity
26+
27+
- **Time Complexity**: `O(n)`
28+
- **Auxiliary Space Complexity**: `O(n)`
29+
30+
31+
32+
### Code (C++)
33+
34+
```cpp
35+
36+
class Solution {
37+
public:
38+
int eliminateMaximum(vector<int>& dist, vector<int>& speed) {
39+
vector<int> time;
40+
41+
for(int i=0;i<dist.size();i++){
42+
int x = ceil((double)(dist[i])/(double)(speed[i]));
43+
time.push_back(x);
44+
}
45+
sort(time.begin(),time.end());
46+
int t =0;
47+
for(auto i:time){
48+
if(t<i) ++t;
49+
else break;
50+
}
51+
return t;
52+
}
53+
};
54+
55+
```
56+
57+
### Contribution and Support
58+
59+
For discussions, questions, or doubts related to this solution, please visit our [discussion section](https://leetcode.com/discuss/general-discussion). We welcome your input and aim to foster a collaborative learning environment.
60+
61+
If you find this solution helpful, consider supporting us by giving a `⭐ star` to the [rishabhv12/Daily-Leetcode-Solution](https://github.com/rishabhv12/Daily-Leetcode-Solution) repository.

0 commit comments

Comments
 (0)