leetcode
leetcode 2701 ~ 2750
人员站位的方案数 I

人员站位的方案数 I

难度:

标签:

题目描述

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

 

Example 1:

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:

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:

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 <= 50
  • points[i].length == 2
  • 0 <= points[i][0], points[i][1] <= 50
  • All points[i] are distinct.

代码结果

运行时间: 43 ms, 内存: 16.0 MB


/*
 * 思路:
 * 1. 使用流来处理点对的遍历和过滤。
 * 2. 使用任意匹配来检查是否有其他点在矩形内或边缘。
 * 3. 统计所有满足条件的点对数。
 */
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class Solution {
    public int countValidPairs(int[][] points) {
        List<int[]> pointList = Arrays.asList(points);
        return (int) pointList.stream()
                .flatMap(a -> pointList.stream().map(b -> new int[][]{a, b}))
                .filter(pair -> pair[0][0] < pair[1][0] && pair[0][1] < pair[1][1])
                .filter(pair -> pointList.stream()
                        .filter(p -> p != pair[0] && p != pair[1])
                        .noneMatch(p -> p[0] >= pair[0][0] && p[0] <= pair[1][0] &&
                                        p[1] >= pair[0][1] && p[1] <= pair[1][1]))
                .count();
    }
}

解释

方法:

此题解首先对点进行排序,排序策略是按x坐标升序排序,若x坐标相同则按y坐标降序排序。排序后,对每个点作为Alice的位置,遍历其后面的点作为Bob的位置,尝试构建一个符合条件的矩形。使用一个变量'y'来记录当前可以作为右下角的点Bob的最大y坐标,以确保不会有其他点在围栏内。通过两层循环的遍历,检查每一个可能的Alice和Bob的组合,符合条件即Alice在左上角,Bob在右下角,且围栏内没有其他点的话,则增加答案计数。

时间复杂度:

O(n^2)

空间复杂度:

O(1)

代码细节讲解

🦆
在排序策略中,为什么选择先按x坐标升序,再按y坐标降序进行排序?这种排序对算法的优化有什么帮助?
这种排序策略有两个主要目的。首先,按x坐标升序排序是为了确保当我们选择一个点作为Alice的位置时,其后的所有点都有可能作为Bob的位置,因为Bob的x坐标必须大于或等于Alice的x坐标。其次,y坐标降序排序的目的是在x坐标相同的情况下,优先考虑较高的y坐标作为Alice的位置,这样可以降低之后查找合适的Bob位置时的复杂度,因为我们只需要查找y坐标小于当前Alice y坐标的点,而不需要考虑更高的y坐标。这种排序方式有助于减少内层循环的次数,从而优化整体算法的性能。
🦆
你在算法描述中提到使用变量'y'来记录可能的Bob的最大y坐标,这种方法如何确保围栏内没有其他点?
在算法中,变量'y'用于记录在当前Alice的位置下,可以作为Bob的位置的最大y坐标。由于已经按x升序和y降序排序,当我们遍历后面的点寻找Bob的位置时,任何一个有效的Bob位置都保证了所有x坐标相同且y坐标在Alice和该Bob之间的点都已经被考虑过,因此不会成为围栏内的点。此外,对于任何x坐标大于当前Alice的点,他们的y坐标必须小于当前记录的最大y坐标'y',以确保这些点不会位于Alice和Bob形成的矩形内部。这种策略确保了在Alice和Bob之间形成的矩形区域内不会有其他点。
🦆
对于围栏可能是一条线段的情况,即Alice和Bob的坐标可能相同的情况,这种情况在你的算法中是如何处理的?
在当前的算法实现中,如果Alice和Bob的坐标完全相同,即在相同的x和y坐标上,这种情况实际上是不会被计算为有效的Alice-Bob对的。这是因为算法中在内层循环时只考虑了那些y坐标严格小于Alice的y坐标的点作为Bob的候选点。因此,如果Alice和Bob坐标相同,他们不会形成一个有效的矩形(实际上也不符合题目要求的矩形定义),所以这种情况被自动排除了。如果需要处理这种情况,算法需要进行适当调整以包含这种特殊情况。

相关问题