原文链接: https://leetcode-cn.com/problems/eliminate-maximum-number-of-monsters
英文原文
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, or n
if you can eliminate all the monsters before they reach the city.
Example 1:
Input: dist = [1,3,4], speed = [1,1,1] Output: 3 Explanation: 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:
Input: dist = [1,1,2,3], speed = [1,1,1,1] Output: 1 Explanation: 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:
Input: dist = [3,2,4], speed = [5,3,2] Output: 1 Explanation: 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.
Constraints:
n == dist.length == speed.length
1 <= n <= 105
1 <= dist[i], speed[i] <= 105
中文题目
你正在玩一款电子游戏,在游戏中你需要保护城市免受怪物侵袭。给你一个 下标从 0 开始 且长度为 n
的整数数组 dist
,其中 dist[i]
是第 i
个怪物与城市的 初始距离(单位:米)。
怪物以 恒定 的速度走向城市。给你一个长度为 n
的整数数组 speed
表示每个怪物的速度,其中 speed[i]
是第 i
个怪物的速度(单位:米/分)。
怪物从 第 0 分钟 时开始移动。你有一把武器,并可以 选择 在每一分钟的开始时使用,包括第 0 分钟。但是你无法在一分钟的中间使用武器。这种武器威力惊人,一次可以消灭任一还活着的怪物。
一旦任一怪物到达城市,你就输掉了这场游戏。如果某个怪物 恰 在某一分钟开始时到达城市,这会被视为 输掉 游戏,在你可以使用武器之前,游戏就会结束。
返回在你输掉游戏前可以消灭的怪物的 最大 数量。如果你可以在所有怪物到达城市前将它们全部消灭,返回 n
。
示例 1:
输入:dist = [1,3,4], speed = [1,1,1] 输出:3 解释: 第 0 分钟开始时,怪物的距离是 [1,3,4],你消灭了第一个怪物。 第 1 分钟开始时,怪物的距离是 [X,2,3],你没有消灭任何怪物。 第 2 分钟开始时,怪物的距离是 [X,1,2],你消灭了第二个怪物。 第 3 分钟开始时,怪物的距离是 [X,X,1],你消灭了第三个怪物。 所有 3 个怪物都可以被消灭。
示例 2:
输入:dist = [1,1,2,3], speed = [1,1,1,1] 输出:1 解释: 第 0 分钟开始时,怪物的距离是 [1,1,2,3],你消灭了第一个怪物。 第 1 分钟开始时,怪物的距离是 [X,0,1,2],你输掉了游戏。 你只能消灭 1 个怪物。
示例 3:
输入:dist = [3,2,4], speed = [5,3,2] 输出:1 解释: 第 0 分钟开始时,怪物的距离是 [3,2,4],你消灭了第一个怪物。 第 1 分钟开始时,怪物的距离是 [X,0,2],你输掉了游戏。 你只能消灭 1 个怪物。
提示:
n == dist.length == speed.length
1 <= n <= 105
1 <= dist[i], speed[i] <= 105
通过代码
高赞题解
func eliminateMaximum(dis, speed []int) int {
n := len(dis)
type pair struct{ d, spd int }
a := make([]pair, n)
for i, d := range dis {
a[i] = pair{d, speed[i]}
}
// 按照怪物到达城市的时间从小到大排序
sort.Slice(a, func(i, j int) bool { a, b := a[i], a[j]; return a.d*b.spd < b.d*a.spd })
for t, p := range a {
if t*p.spd >= p.d {
return t
}
}
return n
}
统计信息
通过次数 | 提交次数 | AC比率 |
---|---|---|
5767 | 16595 | 34.8% |
提交历史
提交时间 | 提交结果 | 执行时间 | 内存消耗 | 语言 |
---|