Find the Number of Ways to Place People I - Problem
Find the Number of Ways to Place People I
You're given a 2D array
Your task is to count the number of valid pairs of points (A, B) where:
• Point A is in the upper-left region relative to point B (meaning A.x ≤ B.x and A.y ≥ B.y)
• The rectangle formed by these two points (including its border) contains no other points except A and B themselves
This is essentially finding pairs of points that can form "clear rectangles" with no obstacles inside.
Goal: Return the total count of such valid pairs.
You're given a 2D array
points of size n x 2 representing integer coordinates of points on a 2D plane, where points[i] = [xi, yi].Your task is to count the number of valid pairs of points (A, B) where:
• Point A is in the upper-left region relative to point B (meaning A.x ≤ B.x and A.y ≥ B.y)
• The rectangle formed by these two points (including its border) contains no other points except A and B themselves
This is essentially finding pairs of points that can form "clear rectangles" with no obstacles inside.
Goal: Return the total count of such valid pairs.
Input & Output
example_1.py — Basic Case
$
Input:
points = [[1,1],[2,2],[3,3]]
›
Output:
0
💡 Note:
No valid pairs exist. For (1,1) and (2,2): (1,1) is upper-left of (2,2), but (3,3) lies on the border of their rectangle. For (1,1) and (3,3): (2,2) lies inside their rectangle.
example_2.py — Valid Pairs
$
Input:
points = [[6,2],[4,4],[2,6]]
›
Output:
2
💡 Note:
Two valid pairs: (4,4) and (6,2) form a clear rectangle, and (2,6) and (6,2) form a clear rectangle. The third pair (2,6) and (4,4) has (6,2) inside their rectangle.
example_3.py — Edge Case
$
Input:
points = [[0,0],[1,0],[0,1]]
›
Output:
3
💡 Note:
All three pairs are valid: (0,1)→(0,0), (0,1)→(1,0), and (1,0)→(0,0). Each forms a clear rectangle or line with no other points inside.
Visualization
Tap to expand
Understanding the Visualization
1
Identify Upper-Left Condition
Point A must have x ≤ B.x and y ≥ B.y
2
Form Rectangle
Rectangle is defined by corners (A.x, A.y) and (B.x, B.y)
3
Check for Obstacles
No other points should lie within or on the rectangle border
4
Count Valid Pairs
Increment counter for each clear rectangle found
Key Takeaway
🎯 Key Insight: We're looking for pairs of points that can form 'empty' rectangles with one point in the upper-left position relative to the other. The challenge is efficiently checking that no other points interfere with each potential rectangle.
Time & Space Complexity
Time Complexity
O(n³)
O(n log n) for sorting + O(n³) for checking pairs and rectangles, but with better constants
⚠ Quadratic Growth
Space Complexity
O(1)
Constant extra space if we sort in-place
✓ Linear Space
Constraints
- 1 ≤ points.length ≤ 50
- points[i].length == 2
- 0 ≤ xi, yi ≤ 50
- All points are unique
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code