#1921

Eliminate Maximum Number of Monsters

specialist · 865 · lc medium +31 · verified · 51% accepted · 1,578 likes · top 40%

Description

You are defending a city from n monsters. Array dist gives each monster's initial distance and speed gives each monster's speed (km/min). Your weapon eliminates one monster per minute. If a monster reaches the city (distance 0) exactly when your weapon is charged, the city is still lost.

Return the maximum number of monsters you can eliminate before one reaches the city, or n if all can be stopped.

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 third 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.

Code

1
2
3