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 ith 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 ith monster in kilometers per minute.
You have a weapon that, once fully charged, can eliminate a single monster. However, the weapon takes one minute to charge.The weapon is fully charged at the very start.
You lose when any monster reaches your city. If a monster reaches the city at the exact moment the weapon is fully charged, it counts as a loss, and the game ends before you can use your weapon.
Return the maximum number of monsters that you can eliminate before you lose, ornif you can eliminate all the monsters before they reach the city.
Input: dist =[1,3,4], speed =[1,1,1]Output: 3Explanation:
In the beginning, the distances of the monsters are [1,3,4]. You eliminate the first monster.After a minute, the distances of the monsters are [X,2,3]. You eliminate the second monster.After a minute, the distances of the monsters are [X,X,2]. You eliminate the thrid monster.All 3 monsters can be eliminated.
Example 2:
1
2
3
4
5
6
Input: dist =[1,1,2,3], speed =[1,1,1,1]Output: 1Explanation:
In the beginning, the distances of the monsters are [1,1,2,3]. You eliminate the first monster.After a minute, the distances of the monsters are [X,0,1,2], so you lose.You can only eliminate 1 monster.
Example 3:
1
2
3
4
5
6
Input: dist =[3,2,4], speed =[5,3,2]Output: 1Explanation:
In the beginning, the distances of the monsters are [3,2,4]. You eliminate the first monster.After a minute, the distances of the monsters are [X,0,2], so you lose.You can only eliminate 1 monster.
We first compute the time each monster takes to reach the city and store these as their arrival times, then sort this array.
At each minute, we eliminate the monster with the earliest arrival; if a monster arrives before it can be eliminated, we stop. For example, with arrival = [1,3,4], the monster at index 0 must be eliminated first since it arrives soonest.
classSolution {
publicinteliminateMaximum(int[] dist, int[] speed) {
int n = dist.length;
int[] arrival =newint[n];
for(int i = 0; i < n; i++){
arrival[i]= (int)Math.ceil(dist[i]* 1.0/ speed[i]);
}
Arrays.sort(arrival);
int eliminated = 0;
// At i = 0, minute = 0 ( therefore, we can use i in place of minute )for(int i = 0; i < n; i++){
// if current arrival time is more than current minute, // eliminate the monstor if(arrival[i]> i){
eliminated++;
}else{
break; // Monster reached the city }
}
return eliminated;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
classSolution {
funeliminateMaximum(dist: IntArray, speed: IntArray): Int {
val n = dist.size
val arrival = IntArray(n) { i -> (dist[i] + speed[i] - 1) / speed[i] }
arrival.sort()
var eliminated = 0for (i in0 until n) {
if (arrival[i] > i) {
eliminated++ } else {
break }
}
return eliminated
}
}
1
2
3
4
5
6
7
8
9
10
11
12
classSolution:
defeliminateMaximum(self, dist: List[int], speed: List[int]) -> int:
n = len(dist)
arrival = [(dist[i] + speed[i] -1) // speed[i] for i in range(n)] # ceil division arrival.sort()
eliminated =0for i in range(n):
if arrival[i] > i:
eliminated +=1else:
breakreturn eliminated
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
impl Solution {
pubfneliminate_maximum(dist: Vec<i32>, speed: Vec<i32>) -> i32 {
let n = dist.len();
letmut arrival: Vec<i32>= dist.iter()
.zip(speed.iter())
.map(|(&d, &s)| (d + s -1) / s)
.collect();
arrival.sort();
for i in0..n {
if arrival[i] asusize<= i {
return i asi32;
}
}
n asi32 }
}
Compute when each monster will arrive at the city, then use a min-heap to always eliminate the monster with the earliest arrival each minute, stopping if a monster reaches the city before it can be eliminated.