Last Updated on 2023-11-07 by Clay
題目
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
題目的情境為你正在玩一款遊戲,遊戲中你需要從怪物手中保衛城鎮,並且怪獸的初始位置都儲存在 dist
中,他們朝城鎮移動的速度為 speed
。
你的手上有武器可以擊殺怪物,但是一次你只能擊殺一支怪物(移動距離不限)—— 請返回你能擊殺的怪獸最大數量(INT)。
解題思路
最直覺的做法就是計算出『每個怪獸抵達城市所需要的時間』,排序後再每擊敗一隻怪獸增加一次時間 —— 直到我們還來不及擊敗怪獸、怪獸就已經抵達了城市。過程中自然是要統計好我們擊敗的怪獸數,畢竟這才是題目要的答案。
我最早開始是想要使用優先權佇列(priority queue)去自動排序,但後來意識到每次加入元素就重新排序一次反而變成浪費時間;於是就很單純地使用陣列儲存怪獸抵達城市的時間,然後直接內建的 sort 排序了。
C++ 範例程式碼
class Solution {
public:
int eliminateMaximum(vector<int>& dist, vector<int>& speed) {
// Init
int eliminate = 0;
int turn = 1;
std::vector<int> rounds;
for (int i=0; i<dist.size(); ++i) {
int round = (dist[i] + speed[i] - 1) / speed[i];
rounds.emplace_back(round);
}
// Sort
sort(rounds.begin(), rounds.end());
// Eliminate monster
for (int& round: rounds) {
if (round < turn) {
break;
}
++eliminate;
++turn;
}
return eliminate;
}
};
Python 範例程式碼
class Solution:
def eliminateMaximum(self, dist: List[int], speed: List[int]) -> int:
# Init
eliminate = 0
turns = 1
rounds = [(dist[i] + speed[i] - 1) // speed[i] for i in range(len(dist))]
# Sort
rounds = sorted(rounds)
# Eliminate monster
for round in rounds:
if round < turns:
break
eliminate += 1
turns += 1
return eliminate