-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
105 lines (99 loc) · 4.67 KB
/
main.cpp
File metadata and controls
105 lines (99 loc) · 4.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
// Source: https://leetcode.com/problems/find-the-number-of-ways-to-place-people-ii
// Title: Find the Number of Ways to Place People II
// Difficulty: Medium
// Author: Mu Yang <http://muyang.pro>
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// 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] = [x_i, y_i]`.
//
// 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 (**Note** that the fence **might not** enclose any area, i.e. it can be a line). 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. For example, Alice cannot build either of the fences in the picture below with four corners `(1, 1)`, `(1, 3)`, `(3, 1)`, and `(3, 3)`, because:
//
// - With Alice at `(3, 3)` and Bob at `(1, 1)`, Alice's position is not the upper left corner and Bob's position is not the lower right corner of the fence.
// - With Alice at `(1, 3)` and Bob at `(1, 1)`, Bob's position is not the lower right corner of the fence.
//
// https://assets.leetcode.com/uploads/2024/01/04/example0alicebob-1.png
//
// **Example 1:**
// https://assets.leetcode.com/uploads/2024/01/04/example1alicebob.png
//
// ```
// Input: points = [[1,1],[2,2],[3,3]]
// Output: 0
// Explanation: There is no way to place Alice and Bob such that Alice can build a fence with Alice's position as the upper left corner and Bob's position as the lower right corner. Hence we return 0.
// ```
//
// **Example 2:**
// https://assets.leetcode.com/uploads/2024/02/04/example2alicebob.png
//
// ```
// Input: points = [[6,2],[4,4],[2,6]]
// Output: 2
// Explanation: There are two ways to place Alice and Bob such that Alice will not be sad:
// - Place Alice at (4, 4) and Bob at (6, 2).
// - Place Alice at (2, 6) and Bob at (4, 4).
// You cannot place Alice at (2, 6) and Bob at (6, 2) because the person at (4, 4) will be inside the fence.
// ```
//
// **Example 3:**
// https://assets.leetcode.com/uploads/2024/02/04/example4alicebob.png
//
// ```
// Input: points = [[3,1],[1,3],[1,1]]
// Output: 2
// Explanation: There are two ways to place Alice and Bob such that Alice will not be sad:
// - Place Alice at (1, 1) and Bob at (3, 1).
// - Place Alice at (1, 3) and Bob at (1, 1).
// You cannot place Alice at (1, 3) and Bob at (3, 1) because the person at (1, 1) will be on the fence.
// Note that it does not matter if the fence encloses any area, the first and second fences in the image are valid.
// ```
//
// **Constraints:**
//
// - `2 <= n <= 1000`
// - `points[i].length == 2`
// - `-10^9 <= points[i][0], points[i][1] <= 10^9`
// - All `points[i]` are distinct.
//
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#include <algorithm>
#include <climits>
#include <vector>
using namespace std;
// Use Sort, O(n^2)
//
// First sort by x coordinate.
//
// For each point A, check all point at its right.
// If the right point is lower, it is an candidate.
//
// To check if there is any point in the rectangle,
// we track the largest y coordinate below A.
// If the right point is lower that number, it is invalid.
class Solution {
public:
int numberOfPairs(vector<vector<int>>& points) {
int n = points.size();
// Sort
sort(points.begin(), points.end(), [](const auto& a, const auto& b) -> bool {
return a[0] != b[0] ? a[0] < b[0] : a[1] > b[1];
}); // sort by x asc and y desc
// Loop
auto ans = 0;
for (auto i = 0; i < n; ++i) {
auto yi = points[i][1];
auto ymax = INT_MIN;
for (auto j = i + 1; j < n; ++j) {
auto yj = points[j][1];
if (yj > yi || yj <= ymax) continue;
++ans;
ymax = yj;
}
}
return ans;
}
};