Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit f57d616

Browse files
committed
Add solution #3027
1 parent b290eac commit f57d616

File tree

2 files changed

+69
-0
lines changed

2 files changed

+69
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2136,6 +2136,7 @@
21362136
3021|[Alice and Bob Playing Flower Game](./solutions/3021-alice-and-bob-playing-flower-game.js)|Medium|
21372137
3024|[Type of Triangle](./solutions/3024-type-of-triangle.js)|Easy|
21382138
3025|[Find the Number of Ways to Place People I](./solutions/3025-find-the-number-of-ways-to-place-people-i.js)|Medium|
2139+
3027|[Find the Number of Ways to Place People II](./solutions/3027-find-the-number-of-ways-to-place-people-ii.js)|Hard|
21392140
3042|[Count Prefix and Suffix Pairs I](./solutions/3042-count-prefix-and-suffix-pairs-i.js)|Easy|
21402141
3066|[Minimum Operations to Exceed Threshold Value II](./solutions/3066-minimum-operations-to-exceed-threshold-value-ii.js)|Medium|
21412142
3068|[Find the Maximum Sum of Node Values](./solutions/3068-find-the-maximum-sum-of-node-values.js)|Hard|
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/**
2+
* 3027. Find the Number of Ways to Place People II
3+
* https://leetcode.com/problems/find-the-number-of-ways-to-place-people-ii/
4+
* Difficulty: Hard
5+
*
6+
* You are given a 2D array points of size n x 2 representing integer coordinates of some points
7+
* on a 2D-plane, where points[i] = [xi, yi].
8+
*
9+
* We define the right direction as positive x-axis (increasing x-coordinate) and the left
10+
* direction as negative x-axis (decreasing x-coordinate). Similarly, we define the up direction
11+
* as positive y-axis (increasing y-coordinate) and the down direction as negative y-axis
12+
* (decreasing y-coordinate)
13+
*
14+
* You have to place n people, including Alice and Bob, at these points such that there is exactly
15+
* one person at every point. Alice wants to be alone with Bob, so Alice will build a rectangular
16+
* fence with Alice's position as the upper left corner and Bob's position as the lower right
17+
* corner of the fence (Note that the fence might not enclose any area, i.e. it can be a line).
18+
* If any person other than Alice and Bob is either inside the fence or on the fence, Alice will
19+
* be sad.
20+
*
21+
* Return the number of pairs of points where you can place Alice and Bob, such that Alice does not
22+
* become sad on building the fence.
23+
*
24+
* Note that Alice can only build a fence with Alice's position as the upper left corner, and Bob's
25+
* position as the lower right corner. For example, Alice cannot build either of the fences in the
26+
* picture below with four corners (1, 1), (1, 3), (3, 1), and (3, 3), because:
27+
* - With Alice at (3, 3) and Bob at (1, 1), Alice's position is not the upper left corner and Bob's
28+
* position is not the lower right corner of the fence.
29+
* - With Alice at (1, 3) and Bob at (1, 1), Bob's position is not the lower right corner of the
30+
* fence.
31+
*/
32+
33+
/**
34+
* @param {number[][]} points
35+
* @return {number}
36+
*/
37+
var numberOfPairs = function(points) {
38+
points.sort((a, b) => a[0] === b[0] ? b[1] - a[1] : a[0] - b[0]);
39+
let result = 0;
40+
41+
for (let i = 0; i < points.length; i++) {
42+
const [, maxY] = points[i];
43+
let minY = -Infinity;
44+
45+
for (let j = i + 1; j < points.length; j++) {
46+
const [, y] = points[j];
47+
if (y <= maxY && y > minY) {
48+
let isValid = true;
49+
for (let k = 0; k < points.length; k++) {
50+
if (k !== i && k !== j) {
51+
const [x, ky] = points[k];
52+
if (x >= points[i][0] && x <= points[j][0]
53+
&& ky <= points[i][1] && ky >= points[j][1]) {
54+
isValid = false;
55+
break;
56+
}
57+
}
58+
}
59+
if (isValid) {
60+
result++;
61+
minY = y;
62+
}
63+
}
64+
}
65+
}
66+
67+
return result;
68+
};

0 commit comments

Comments
 (0)