Find the Number of Ways to Place People I - Problem

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 A is on the upper left side of B, and there are no other points in the rectangle (or line) they make (including the border), except for the points A and B.

Return the count.

Input & Output

Example 1 — Basic Rectangle
$ Input: points = [[1,1],[2,2],[3,3]]
Output: 0
💡 Note: No valid pairs exist. For each pair, either one point is not upper-left of the other, or there are points on the boundary of their rectangle.
Example 2 — Valid Pair
$ Input: points = [[6,2],[4,4],[2,6]]
Output: 2
💡 Note: Point (2,6) is upper-left of (4,4) and (6,2). Point (4,4) is upper-left of (6,2). All rectangles are empty, so we have 2 valid pairs.
Example 3 — Blocked Rectangle
$ Input: points = [[0,0],[1,1],[2,0]]
Output: 1
💡 Note: Only (1,1) is upper-left of (2,0) with an empty rectangle between them.

Constraints

  • 2 ≤ points.length ≤ 50
  • points[i].length == 2
  • 0 ≤ xi, yi ≤ 50
  • All the given points are unique

Visualization

Tap to expand
Find the Number of Ways to Place People I INPUT 1 2 3 1 2 3 (1,1) (2,2) (3,3) Input Array: points = [[1,1],[2,2],[3,3]] n = 3 points A must be upper-left of B No points inside rectangle (including borders) ALGORITHM STEPS 1 Sort Points By x (asc), then y (desc) 2 Check All Pairs For each pair (A, B) 3 Validate Position A.x <= B.x AND A.y >= B.y 4 Check Rectangle No other points inside Pair Analysis: Pair Upper-Left? Valid? (1,1)-(2,2) NO NO (1,1)-(3,3) NO NO (2,2)-(3,3) NO NO All on diagonal - none upper-left FINAL RESULT (1,1) (2,2) (3,3) Diagonal Output: 0 No valid pairs found! Points lie on a diagonal None is upper-left of another Key Insight: For point A to be "upper-left" of B: A.x <= B.x AND A.y >= B.y (A is left or same x, and higher or same y) When points lie on a diagonal (like y=x), each point going right also goes up, so no point satisfies the condition. Time Complexity: O(n^3) - checking all pairs and verifying no points inside. Space: O(1) extra space. TutorialsPoint - Find the Number of Ways to Place People I | Optimal Solution
Asked in
Google 15 Microsoft 8
8.5K Views
Medium Frequency
~25 min Avg. Time
234 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen