|
| 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