1404. Unbeaten Points

Medium3.5 points estimatedGeometrySortingArray

A marketplace lists depots in points, where each depot is [x, y] with x its speed score and y its capacity score. A depot is beaten if some other depot with different coordinates has an x at least as large and a y at least as large. Return the distinct depots that are not beaten, each as [x, y], sorted by x ascending.

Depots with identical coordinates never beat each other, so a location listed several times appears once in the answer. Because the survivors form a staircase, x strictly increases while y strictly decreases along the result. A good plan sorts by x descending (ties by y descending), keeps every depot whose y is strictly greater than every y kept so far, and reverses the kept list. The intended solution runs in O(n log n) time and O(n) space.

Example 1

Input:
points = [[1,5],[3,3],[2,2],[4,1],[3,3]]
Output:
[[1,5],[3,3],[4,1]]
Explanation:

Depot [2, 2] is beaten by [3, 3], and the repeated [3, 3] is reported once, leaving a staircase of three depots.

Example 2

Input:
points = [[5,5]]
Output:
[[5,5]]
Explanation:

A lone depot is never beaten.

Example 3

Input:
points = [[2,7],[2,9],[6,9],[6,1]]
Output:
[[6,9]]
Explanation:

Depot [6, 9] beats every other one, so it is the only survivor.

Constraints

1 ≤ n ≤ 1000

0 ≤ x, y ≤ 109

How this problem is judged

Answers
Your answer must match exactly. Numbers compare by value, so 2 and 2.0 are equal.

Expected complexity

Time
O(n log n)
Space
O(n)

What the author was aiming for. Your own solution is not measured against it.

Asked in an interview

Were you asked this in an interview? Say where, anonymously.

Code
Loading the editor…