โ† All problems

3025. Find the Number of Ways to Place People I

MediumOpen on LeetCodeProblem statement

Problem Statement

3025. Find the Number of Ways to Place People I

Medium


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

Count the number of pairs of points (A, B), where

Return the count.

 

Example 1:

Input: points = [[1,1],[2,2],[3,3]]

Output: 0

Explanation:

There is no way to choose A and B so A is on the upper left side of B.

Example 2:

Input: points = [[6,2],[4,4],[2,6]]

Output: 2

Explanation:

Example 3:

Input: points = [[3,1],[1,3],[1,1]]

Output: 2

Explanation:

 

Constraints:

Java โ€” simulation

Source file
class Solution {
    public int numberOfPairs(int[][] points) {
        int n = points.length, count = 0;
        for (int i = 0; i < n; i++) {
            int[] B = points[i];
            for (int j = 0; j < n; j++) {
                if (i == j)
                    continue;
                int[] A = points[j];
                if (A[0] <= B[0] && A[1] >= B[1]) { // A is on upper left of B
                    boolean hasPtsOnRect = false;
                    // System.out.println(Arrays.toString(A) + Arrays.toString(B));
                    for (int k = 0; k < n; k++) { // check for points on rectangle formed
                        if (i == k || j == k)
                            continue;
                        int[] C = points[k];
                        if (C[0] >= A[0] && C[0] <= B[0] && C[1] <= A[1] && C[1] >= B[1]) { // inside rect
                            hasPtsOnRect = true;
                            // System.out.println("point found inside rect:\t" + Arrays.toString(C));
                            break;
                        }
                    }
                    if (!hasPtsOnRect)
                        count++;
                }
            }
        }
        return count;
    }
}