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 资源

通过相关的交互式教程、备忘单和代码比较来扩展您的知识。