Find the Number of Ways to Place People II - Problem

Imagine you're Alice and you want to build a private rectangular fence around just you and your friend Bob on a 2D plane! ๐Ÿ 

You're given n points where people must be placed, with exactly one person per point. Alice wants to build a rectangular fence where:

  • Alice's position is the upper-left corner
  • Bob's position is the lower-right corner
  • No other people are inside or on the fence boundary

The fence can be a line (zero area) if Alice and Bob share the same x or y coordinate.

Goal: Count how many valid pairs of positions (Alice, Bob) exist where Alice won't be sad due to unwanted visitors in their private space.

Note: "Upper-left" means Alice's x โ‰ค Bob's x and Alice's y โ‰ฅ Bob's y (remember y-axis increases upward).

Input & Output

example_1.py โ€” Basic Rectangle
$ Input: points = [[1,1],[2,2],[3,3]]
โ€บ Output: 0
๐Ÿ’ก Note: No valid Alice-Bob pairs exist because any rectangle formed will contain the middle point (2,2) on its boundary or inside.
example_2.py โ€” 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), and Alice at (2,6) with Bob at (4,4). Both form rectangles with no other points inside.
example_3.py โ€” Edge Case
$ Input: points = [[3,1],[1,3],[1,1]]
โ€บ Output: 1
๐Ÿ’ก Note: Only one valid pair: Alice at (1,3) and Bob at (3,1). The rectangle formed doesn't contain point (1,1) since it's exactly on the boundary.

Visualization

Tap to expand
X-axis (East โ†’)Y-axis (North โ†‘)AliceBobPerson CPerson DPrivate FenceโŒ Invalid!Person C is inside the fence - Alice will be sad!
Understanding the Visualization
1
Survey the Land
Identify all possible positions where people must be placed on the 2D plane
2
Test Alice Position
Pick a potential upper-left corner position for Alice
3
Find Valid Bob Spot
Look for positions where Bob can be lower-right (x โ‰ฅ Alice's x, y โ‰ค Alice's y)
4
Check for Intruders
Verify no other people would be inside or on the fence boundary
5
Count Valid Configurations
Add to total count if the fence configuration is private
Key Takeaway
๐ŸŽฏ Key Insight: Alice needs her position as upper-left corner (x โ‰ค Bob's x, y โ‰ฅ Bob's y) and an empty rectangle interior to be happy with their private space.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(nยณ)

O(nยฒ) pairs to check ร— O(n) points to verify for each pair

n
2n
โš  Quadratic Growth
Space Complexity
O(1)

Only using constant extra space for variables

n
2n
โœ“ Linear Space

Constraints

  • 2 โ‰ค points.length โ‰ค 50
  • points[i].length == 2
  • -109 โ‰ค xi, yi โ‰ค 109
  • All points have unique coordinates
Asked in
Google 25 Meta 18 Amazon 15 Microsoft 12
26.1K Views
Medium Frequency
~25 min Avg. Time
892 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