Eliminate Maximum Number of Monsters LeetCode solution in Java

1
class Solution {
2
public int eliminateMaximum(int[] dist, int[] speed) {
3

4
int n = dist.length;
5

6
int[] time = new int[n];
7

8
for (int i = 0; i < n; i++) {
9
time[i] = (int) Math.ceil(dist[i] * 1.0 / speed[i]);
10
}
11

12
Arrays.sort(time);
13

14
int eliminated = 0;
15

16
// At i = 0, minute = 0 ( therefore, we can use i in place of minute )
17

18
for (int i = 0; i < n; i++) {
19

20
if (time[i] > i) { // At ith minute, eliminate the first monster arriving after ith minute
21
eliminated++;
22
} else {
23
break; // Monster reached the city
24
}
25
}
26

27
return eliminated;
28
}
29
}

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0