The weapon needs 1 minute to recharge after each shot. To maximize eliminations, we should prioritize monsters that will reach the city soonest. For each monster, we calculate the time it takes to arrive (distance / speed, rounded up). Sorting these arrival times lets us greedily eliminate monsters in order of urgency. If at any minute the earliest uneliminated monster has already arrived, the game ends.
minReach[i] = ceil(dist[i] / speed[i]) for each monster.minReach in ascending order.minute:minute >= minReach[minute], the monster arrives before we can shoot it. Return the current count.class Solution:
def eliminateMaximum(self, dist: List[int], speed: List[int]) -> int:
minReach = [math.ceil(d / s) for d, s in zip(dist, speed)]
minReach.sort()
res = 0
for minute in range(len(minReach)):
if minute >= minReach[minute]:
return res
res += 1
return resThis is the same approach as above, but we save space by reusing the input dist array to store arrival times instead of creating a new array. The logic remains identical: compute arrival times, sort, and check if we can eliminate each monster before it arrives.
dist[i] with ceil(dist[i] / speed[i]).dist in ascending order.minute from 0 to n - 1:minute >= dist[minute], return minute.n if all monsters are eliminated.class Solution:
def eliminateMaximum(self, dist: List[int], speed: List[int]) -> int:
for i in range(len(dist)):
dist[i] = math.ceil(dist[i] / speed[i])
dist.sort()
for minute in range(len(dist)):
if minute >= dist[minute]:
return minute
return len(dist)Instead of sorting all arrival times upfront, we can use a min-heap to extract the minimum arrival time on demand. This still achieves the same goal of processing monsters in order of how soon they reach the city. We pop from the heap one at a time and check if the monster arrives before the current minute.
dist[i] / speed[i] into a min-heap for each monster.res = 0 to count eliminations.res >= arrival_time, the monster has reached the city. Return res.res.res after all monsters are eliminated.class Solution:
def eliminateMaximum(self, dist: List[int], speed: List[int]) -> int:
minHeap = []
for i in range(len(dist)):
heapq.heappush(minHeap, dist[i] / speed[i])
res = 0
while minHeap:
if res >= heapq.heappop(minHeap):
return res
res += 1
return resWhen calculating arrival time, you need ceil(dist[i] / speed[i]) because a monster at distance 3 with speed 2 arrives at minute 2 (not 1.5). Using floor division or integer division without rounding up causes incorrect arrival time calculations, leading to wrong elimination counts.
The greedy approach only works when monsters are processed in order of their arrival times. Without sorting, you might try to eliminate a monster that arrives later while an earlier one reaches the city, giving incorrect results.
The condition should be minute >= minReach[minute] (monster arrives at or before the current minute), not minute > minReach[minute]. If a monster arrives exactly at minute 2 and we're at minute 2, we cannot eliminate it in time since we shoot at the start of each minute.