3025. Find the Number of Ways to Place People I
SourceBiweekly Contest 123 Q2DifficultyMediumRating1707
Description
You are given a 2D array points of size n x 2 representing integer coordinates of some points on a 2D plane, where points[i] = [xi, yi].
Count the number of pairs of points (A, B), where
Ais on the upper left side ofB, and- there are no other points in the rectangle (or line) they make (including the border), except for the points
AandB.
Return the count.
Example 1:
Input: points = [[1,1],[2,2],[3,3]]
Output: 0
Explanation:
There is no way to choose A and B such that A is on the upper left side of B.
Example 2:
Input: points = [[6,2],[4,4],[2,6]]
Output: 2
Explanation:
- The left one is the pair
(points[1], points[0]), wherepoints[1]is on the upper left side ofpoints[0]and the rectangle is empty. - The middle one is the pair
(points[2], points[1]), same as the left one it is a valid pair. - The right one is the pair
(points[2], points[0]), wherepoints[2]is on the upper left side ofpoints[0], butpoints[1]is inside the rectangle so it's not a valid pair.
Example 3:
Input: points = [[3,1],[1,3],[1,1]]
Output: 2
Explanation:
- The left one is the pair
(points[2], points[0]), wherepoints[2]is on the upper left side ofpoints[0]and there are no other points on the line they form. Note that it is a valid state when the two points form a line. - The middle one is the pair
(points[1], points[2]), it is a valid pair same as the left one. - The right one is the pair
(points[1], points[0]), it is not a valid pair aspoints[2]is on the border of the rectangle.
Constraints:
2 <= n <= 50points[i].length == 20 <= points[i][0], points[i][1] <= 50- All
points[i]are distinct.
Solutions
Solution 1: Sorting and Enumeration
Thinking
\(n \le 50\). Enumerating an upper-left and a lower-right corner and scanning the box is \(O(n^3)\) and would pass, but repeats a lot of work.
After sorting by increasing \(x\) and, on ties, decreasing \(y\), a valid lower-right \(y\) must strictly increase as \(x\) grows, or the new point would sit inside an earlier rectangle.
For each upper-left point we keep the largest chosen \(y_2\) and count a pair only when \(\textit{maxY} < y_2 \le y_1\).
Enumerate the upper-left point \((x_1, y_1)\). A later point \((x_2, y_2)\) is a valid lower-right corner only when its \(y\) is strictly larger than the \(y\) of every lower-right point already accepted; otherwise it lies inside a rectangle that has already been formed.
Sort the points by increasing \(x\). When two points share \(x\), sort them by decreasing \(y\).
For each upper-left point \((x_1, y_1)\), maintain \(\textit{maxY}\), the largest \(y\) among accepted lower-right points. Scan the following points \((x_2, y_2)\). If \(\textit{maxY} < y_2 \le y_1\), the pair is valid: add one to the answer and set \(\textit{maxY} = y_2\).
After every upper-left point has been scanned, the answer is complete.
The time complexity is \(O(n^2)\), and the space complexity is \(O(\log n)\). Here, \(n\) is the number of points.
1 2 3 4 5 6 7 8 9 10 11 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | |


