Find the Number of Ways to Place People II - 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].

We define the right direction as positive x-axis (increasing x-coordinate) and the left direction as negative x-axis (decreasing x-coordinate). Similarly, we define the up direction as positive y-axis (increasing y-coordinate) and the down direction as negative y-axis (decreasing y-coordinate).

You have to place n people, including Alice and Bob, at these points such that there is exactly one person at every point. Alice wants to be alone with Bob, so Alice will build a rectangular fence with Alice's position as the upper left corner and Bob's position as the lower right corner of the fence.

If any person other than Alice and Bob is either inside the fence or on the fence, Alice will be sad.

Return the number of pairs of points where you can place Alice and Bob, such that Alice does not become sad on building the fence.

Note that Alice can only build a fence with Alice's position as the upper left corner, and Bob's position as the lower right corner.

Input & Output

Example 1 — Basic Case
$ Input: points = [[1,1],[2,2],[3,3]]
Output: 0
💡 Note: No valid Alice-Bob pairs exist. For any pair where Alice could be upper-left and Bob lower-right, the third point would always fall inside or on their rectangle boundary.
Example 2 — Valid Pairs
$ Input: points = [[6,2],[4,4],[2,6]]
Output: 2
💡 Note: Two valid pairs: Alice at (2,6) with Bob at (6,2) creates empty rectangle, and Alice at (2,6) with Bob at (4,4) also creates empty rectangle.
Example 3 — Edge Case
$ Input: points = [[3,1],[1,3],[1,1],[2,2]]
Output: 1
💡 Note: Only Alice at (1,3) and Bob at (3,1) form a valid pair with no other people inside their rectangle.

Constraints

  • 2 ≤ points.length ≤ 50
  • -50 ≤ points[i][0], points[i][1] ≤ 50
  • All points[i] are distinct

Visualization

Tap to expand
Place People II - Fence Problem INPUT x: 1 x: 2 x: 3 1,1 2,2 3,3 Points Array: [[1,1],[2,2],[3,3]] n = 3 points Diagonal arrangement Alice: upper-left corner Bob: lower-right corner ALGORITHM STEPS 1 Sort Points Sort by Y desc, then X asc 2 Check All Pairs Alice at i, Bob at j (j > i) 3 Validate Fence Bob.x >= Alice.x, Bob.y <= Alice.y 4 Early Pruning Skip if any point inside fence Pair Check Example: A B X Point inside = INVALID All pairs on diagonal! FINAL RESULT (1,1) (2,2) (3,3) All points diagonal No valid Alice-Bob pair Output: 0 No valid pairs! Every fence would contain 3rd point on the boundary Key Insight: Points on a strict diagonal mean any Alice-Bob pair will have the middle point exactly on the fence boundary. Early pruning: Sort points, then for each pair, track minimum Y seen. If any point falls inside the fence rectangle (including boundary), the pair is invalid. Time: O(n^2) with sorting optimization. TutorialsPoint - Find the Number of Ways to Place People II | Early Pruning Approach
Asked in
Google 15 Microsoft 12
12.0K Views
Medium Frequency
~35 min Avg. Time
245 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