1921. Eliminate Maximum Number of Monsters Medium
1/**
2 * [1921] Eliminate Maximum Number of Monsters
3 *
4 * 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 i^th monster from the city.
5 * 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 i^th monster in kilometers per minute.
6 * 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.
7 * 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.
8 * Return the maximum number of monsters that you can eliminate before you lose, or n if you can eliminate all the monsters before they reach the city.
9 *
10 * Example 1:
11 *
12 * Input: dist = [1,3,4], speed = [1,1,1]
13 * Output: 3
14 * Explanation:
15 * In the beginning, the distances of the monsters are [1,3,4]. You eliminate the first monster.
16 * After a minute, the distances of the monsters are [X,2,3]. You eliminate the second monster.
17 * After a minute, the distances of the monsters are [X,X,2]. You eliminate the thrid monster.
18 * All 3 monsters can be eliminated.
19 * Example 2:
20 *
21 * Input: dist = [1,1,2,3], speed = [1,1,1,1]
22 * Output: 1
23 * Explanation:
24 * In the beginning, the distances of the monsters are [1,1,2,3]. You eliminate the first monster.
25 * After a minute, the distances of the monsters are [X,0,1,2], so you lose.
26 * You can only eliminate 1 monster.
27 *
28 * Example 3:
29 *
30 * Input: dist = [3,2,4], speed = [5,3,2]
31 * Output: 1
32 * Explanation:
33 * In the beginning, the distances of the monsters are [3,2,4]. You eliminate the first monster.
34 * After a minute, the distances of the monsters are [X,0,2], so you lose.
35 * You can only eliminate 1 monster.
36 *
37 *
38 * Constraints:
39 *
40 * n == dist.length == speed.length
41 * 1 <= n <= 10^5
42 * 1 <= dist[i], speed[i] <= 10^5
43 *
44 */
45pub struct Solution {}
46
47// problem: https://leetcode.com/problems/eliminate-maximum-number-of-monsters/
48// discuss: https://leetcode.com/problems/eliminate-maximum-number-of-monsters/discuss/?currentPage=1&orderBy=most_votes&query=
49
50// submission codes start here
51
52impl Solution {
53 pub fn eliminate_maximum(dist: Vec<i32>, speed: Vec<i32>) -> i32 {
54 0
55 }
56}
57
58// submission codes end
59
60#[cfg(test)]
61mod tests {
62 use super::*;
63
64 #[test]
65 fn test_1921() {
66 }
67}
68
Back
© 2025 bowen.ge All Rights Reserved.