How many airplanes can a single gun shoot down (maximum non-overlapping time windows)

Quick Overview

Given the time windows when airplanes are in the sky and one gun that must stay on a plane for its whole window, find the most planes that can be shot down. It tests interval scheduling, picking the right sort key, and treating windows that touch at an endpoint as compatible.

How many airplanes can a single gun shoot down (maximum non-overlapping time windows)

Company: Agoda

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

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 ```python 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 ```text 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. ```text 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. ```text 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.

Overview: Given the time windows when airplanes are in the sky and one gun that must stay on a plane for its whole window, find the most planes that can be shot down. It tests interval scheduling, picking the right sort key, and treating windows that touch at an endpoint as compatible.

Agoda logo
Agoda
Nov 25, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

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.