-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEliminate Maximum Number of Monsters.cpp
31 lines (26 loc) Β· 1.17 KB
/
Eliminate Maximum Number of Monsters.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
class Solution {
public:
int eliminateMaximum(std::vector<int>& dist, std::vector<int>& speed) {
int n = dist.size(); // Number of monsters
// Convert distances to arrival times (time = distance / speed)
for (int i = 0; i < n; ++i) {
dist[i] = std::ceil(static_cast<double>(dist[i]) / speed[i]);
speed[i] = 0; // Initialize the "speed" array to 0 for counting monsters arriving at the same time.
}
// Count the number of monsters arriving at each minute
for (int arrivalTime : dist) {
if (arrivalTime >= n) continue; // Ignore monsters arriving after the game ends
speed[arrivalTime] += 1;
}
// Calculate the cumulative count of monsters arriving at or before each minute
for (int i = 1; i < n; ++i) {
speed[i] += speed[i - 1];
// If the cumulative count exceeds the current minute, return the maximum number of eliminated monsters
if (speed[i] > i) {
return i;
}
}
// If no game loss occurs, return the total number of monsters, as all of them can be eliminated
return n;
}
};