Eliminate Maximum Number of Monsters LeetCode solution in Java
1class Solution {2public int eliminateMaximum(int[] dist, int[] speed) {34int n = dist.length;56int[] time = new int[n];78for (int i = 0; i < n; i++) {9time[i] = (int) Math.ceil(dist[i] * 1.0 / speed[i]);10}1112Arrays.sort(time);1314int eliminated = 0;1516// At i = 0, minute = 0 ( therefore, we can use i in place of minute )1718for (int i = 0; i < n; i++) {1920if (time[i] > i) { // At ith minute, eliminate the first monster arriving after ith minute21eliminated++;22} else {23break; // Monster reached the city24}25}2627return eliminated;28}29}