Find the Number of Ways to Place People I
MEDIUMDescription
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).
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 so 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.
Approaches
Checkout 2 different approaches to solve Find the Number of Ways to Place People I. Click on different approaches to view the approach and algorithm in detail.
Brute-Force Approach
This approach directly translates the problem statement into code. It iterates through all possible pairs of points (A, B) and for each pair, it first checks if A is to the upper-left of B. If this condition is met, it then performs another iteration through all other points (C) to check if any of them lie inside the rectangle formed by A and B. If no such point C is found, the pair (A, B) is counted.
Algorithm
- Initialize a counter
countto 0. - Iterate through each point
Ain thepointsarray using an indexi. - In a nested loop, iterate through each point
Bin thepointsarray using an indexj. - If
iandjare the same, skip to the next iteration to avoid comparing a point with itself. - Check if point
Ais on the upper-left side of pointB. This condition ispoints[i][0] <= points[j][0]andpoints[i][1] >= points[j][1]. - If the upper-left condition is met, we must check if the rectangle they form is empty. Initialize a boolean flag
is_emptytotrue. - Start a third nested loop, iterating through every point
Cwith indexk. - If
kis the same asiorj, skip. - Check if point
Clies inside or on the boundary of the rectangle formed byAandB. The condition ispoints[k][0] >= points[i][0],points[k][0] <= points[j][0],points[k][1] >= points[j][1], andpoints[k][1] <= points[i][1]. - If such a point
Cis found, setis_emptytofalseand break the innermost loop. - After checking all points
C, ifis_emptyis stilltrue, it means we found a valid pair. Incrementcount. - After all loops complete, return the final
count.
The brute-force algorithm systematically checks every possible ordered pair of points (A, B). For each pair, it first validates the geometric condition: A.x <= B.x and A.y >= B.y. If this condition holds, the algorithm proceeds to check for the 'empty rectangle' constraint. It does this by iterating through every other point C in the list and checking if C lies inside or on the boundary of the rectangle defined by A and B. A point C is inside if A.x <= C.x <= B.x and B.y <= C.y <= A.y. If any such point C is found, the rectangle is not empty, and the pair (A, B) is invalid. If the loop over all points C completes without finding any interfering point, the pair (A, B) is valid, and a counter is incremented. This process is repeated for all pairs to find the total count.
class Solution {
public int numberOfPairs(int[][] points) {
int n = points.length;
int count = 0;
for (int i = 0; i < n; i++) { // Point A
for (int j = 0; j < n; j++) { // Point B
if (i == j) continue;
int x1 = points[i][0];
int y1 = points[i][1];
int x2 = points[j][0];
int y2 = points[j][1];
// Check if A is upper-left of B
if (x1 <= x2 && y1 >= y2) {
boolean isRectangleEmpty = true;
// Check for any other point C inside the rectangle
for (int k = 0; k < n; k++) {
if (k == i || k == j) continue;
int x3 = points[k][0];
int y3 = points[k][1];
if (x3 >= x1 && x3 <= x2 && y3 >= y2 && y3 <= y1) {
isRectangleEmpty = false;
break;
}
}
if (isRectangleEmpty) {
count++;
}
}
}
}
return count;
}
}
Complexity Analysis
Pros and Cons
- It is straightforward to understand and implement as it directly follows the problem's definition.
- It is guaranteed to be correct and works well for the given small constraints (n <= 50).
- The O(n^3) time complexity makes it inefficient and potentially too slow if the constraints on
nwere larger.
Code Solutions
Checking out 4 solutions in different languages for Find the Number of Ways to Place People I. Click on different languages to view the code.
Video Solution
Watch the video walkthrough for Find the Number of Ways to Place People I
Similar Questions
5 related questions you might find useful
Algorithms:
Patterns:
Data Structures:
Subscribe to Scale Engineer newsletter
Learn about System Design, Software Engineering, and interview experiences every week.
No spam, unsubscribe at any time.