2.2 KiB
2.2 KiB
[3025] Find The Number Of Ways To Place People I
題目資訊
- 難度: Medium
- 標籤: Array, Math, Geometry, Sorting, Enumeration
- 題目連結: LeetCode
- 練習日期: 2025-09-02
題目描述
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
A
is on the upper left side ofB
, and- there are no other points in the rectangle (or line) they make (including the border).
Return the count.
解題思路
初步分析
- 核心概念 幾何關係判斷 + 區域內點的檢測
- 關鍵限制條件
- A必須在B的左上方, A.x <= B.x, A.y >= B.y
- 矩形區域內(包含邊界)不能有其他點
- 預期時間/空間複雜度?
解法概述
-
暴力解法:
- 思路:
- 遍歷所有點對 (A, B)
- 檢查每個pair形成的矩形, 裡面跟邊界會不會包含其他點
- 時間複雜度:O(N^3)
- 空間複雜度:O(?)
- 思路:
-
優化解法:
- 思路:
- 時間複雜度:O(?)
- 空間複雜度:O(?)
測試案例
範例輸入輸出
Input: points = [[6,2],[4,4],[2,6]]
Output: 2
Explanation:
- The left one is the pair (points[1], points[0]), where points[1] is on the upper left side of points[0] and the rectangle is empty.
- The left one is the pair (points[1], points[0]), where points[1] is on the upper left side of points[0] and the rectangle is empty.
- The right one is the pair (points[2], points[0]), where points[2] is on the upper left side of points[0], but points[1] is inside the rectangle so it's not a valid pair.
邊界情況
2 <= n <= 50
points[i].length == 2
0 <= points[i][0], points[i][1] <= 50
- All
points[i]
are distinct.
學習筆記
今天學到什麼?
- 二維空間判斷點大小
遇到的困難
- 無
改善方向
- 無
相關題目
- #223 Rectangle Area
總結: 這題的核心概念是...,適合練習...技巧。