You are given an array of airplane time intervals airplanes, where airplanes[i] = [start_i, end_i] is the time window during which airplane i is visible in the sky.
You command a single anti-aircraft gun that can shoot down one airplane at a time, and shooting down an airplane takes the entire time that airplane is in the sky, from its start to its end. While shooting one airplane, you cannot start shooting another until you finish the current one.
Return the maximum number of airplanes that can be shot down. Two airplanes whose intervals only touch at an endpoint (one ends exactly when the other starts) do not overlap, and both can be shot down.
Function Signature
def max_planes_shot(airplanes: list[list[int]]) -> int:
Rules
-
To shoot down airplane
i
, the gun must be on it for the whole window
[start_i, end_i]
. A plane cannot be shot for part of its window.
-
Two chosen airplanes are compatible when one has
end <= start
of the other. Sharing an endpoint is allowed.
-
Return only the count.
Constraints
-
1 <= len(airplanes) <= 10^5
-
len(airplanes[i]) == 2
-
0 <= start_i < end_i <= 10^9
Examples
Input: airplanes = [[1, 10], [2, 3], [4, 5], [6, 7]]
Output: 3
Shoot [2, 3], then [4, 5], then [6, 7]. The plane [1, 10] overlaps all three, so taking it would leave room for nothing else.
Input: airplanes = [[1, 5], [2, 6], [3, 7]]
Output: 1
Every pair of windows overlaps (all three contain time 4), so only one airplane can be shot down.
Input: airplanes = [[1, 3], [3, 5], [5, 6], [2, 4]]
Output: 3
[1, 3], [3, 5] and [5, 6] only touch at endpoints, so all three can be shot. [2, 4] overlaps both [1, 3] and [3, 5], and no choice of four works.