150強訪談簡單

檢測方塊

“檢測方塊”問題的詳細指南和 Python 實作。

問題陳述

簡單

您將獲得 X-Y 平面上的一系列點。設計一個資料結構:

- 從流中新增點。允許重複的點,並且應將其視為不同的點。

- 給定一個查詢點,計算從資料結構中選擇三個點以使這三個點和查詢點形成面積為正的軸對齊正方形的方法的數量。

軸對齊正方形是指邊的長度都相同且平行或垂直於 x 軸和 y 軸的正方形。

實作一個函數 detectSquares(operations: list, arguments: list) -> list ,其中操作是“DetectSquares”、“add”或“count”,參數是對應的參數。返回結果列表(對於建構函數和添加,無)。

約束條件
  • point.length == 2
  • 0 <= x, y <= 1000
  • At most 3000 calls in total will be made to add and count

範例

Example 1
Input
["DetectSquares","add","add","add","count","count","add","count"], [[],[3,10],[11,2],[3,2],[11,10],[14,8],[11,2],[11,10]]
Output
[None,None,None,None,1,0,None,2]
Explanation

After adding (3,10), (11,2), (3,2): count(11,10) finds 1 square with corners (3,10),(11,10),(11,2),(3,2). count(14,8) finds 0. After adding another (11,2): count(11,10) finds 2 squares (using each copy of (11,2)).

Need a Hint?
考慮使用數學和幾何特定的資料結構,例如集合或堆疊。
Edge Cases to Watch
  • 空輸入結構
  • 單元素輸入
  • 大數值範圍

準備好解決了嗎?

Open the problem in PyRun's browser-based Python editor. Your code runs fully offline — no server required.

在編輯器中開啟
Found this breakdown helpful?

PyRun is built and maintained by an independent solo developer. If this helped your interview prep, consider buying a coffee!

Buy me a coffee

推薦的 Python 資源

透過相關的互動式教學、備忘單和程式碼比較來擴展您的知識。