philosyang.com

3025. Find the Number of Ways to Place People I | 1707

What a weird question - and it is exactly these questions that feels like a FAANG problem.

 1class Solution:
 2    def numberOfPairs(self, points: List[List[int]]) -> int:
 3        # intuition:
 4        # we have to start from the left-top-most point
 5        # needs a secondary sort for points
 6        # for each top-left, keep bottom-right's `y` in a range
 7
 8        result = 0
 9
10        points.sort(key=lambda x: (x[0], -x[1]))
11        n = len(points)
12
13        for i in range(n - 1):
14            y_hi = points[i][1]
15            y_lo = -1
16
17            for j in range(i + 1, n):
18                y = points[j][1]
19                if y_lo < y <= y_hi:
20                    result += 1
21                    y_lo = y
22
23        return result

#Array #Math #Geometry #Sorting #Enumeration #Python