LC 2013. 检测正方形
题目描述
这是 LeetCode 上的 2013. 检测正方形 ,难度为 中等。
给你一个在 X-Y
平面上的点构成的数据流。设计一个满足下述要求的算法:
- 添加一个在数据流中的新点到某个数据结构中。可以添加重复的点,并会视作不同的点进行处理。
- 给你一个查询点,请你从数据结构中选出三个点,使这三个点和查询点一同构成一个面积为正的轴对齐正方形 ,统计满足该要求的方案数目。
轴对齐正方形是一个正方形,除四条边长度相同外,还满足每条边都与 x-轴
或 y-轴
平行或垂直。
实现 DetectSquares
类:
DetectSquares()
使用空数据结构初始化对象void add(int[] point)
向数据结构添加一个新的点point = [x, y]
int count(int[] point)
统计按上述方式与点point = [x, y]
共同构造 轴对齐正方形 的方案数。
示例:
1 |
|
提示:
- $point.length = 2$
- $0 <= x, y <= 1000$
- 调用
add
和count
的总次数最多为 $5000$
哈希表
对于 add
操作,我们可以使用「哈希表 套 哈希表」的方式,以 {x, {y : 点 (x,y) 数量}}
的形式对传入点进行存储。
对于 count
查询而言,假定传入的点为 $(x, y)$,我们可以先查询 $x$ 行都有哪些列,枚举这些列( 即枚举点 $(x, ny)$ ),由 $y$ 和 $ny$ 可得正方形边长 $len$,此时再检查唯一确定的两点 $(x \pm len, y)$ 和 $(x \pm len, ny)$ 的出现次数,应用乘法原理,即可知道该正方形的方案数,统计所有合法方案数即是该询问的答案。
利用题目范围给定的 x
和 y
具有明确的范围 0 <= x, y <= 1000
,我们可以使用数组充当哈希表,但是为了拓展性和减少边界判断,即支持将平面拓展到任意大小,最好还是直接使用哈希表。
Java 代码:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28class DetectSquares {
Map<Integer, Map<Integer, Integer>> row2Col = new HashMap<>();
public void add(int[] point) {
int x = point[0], y = point[1];
Map<Integer, Integer> col2Cnt = row2Col.getOrDefault(x, new HashMap<>());
col2Cnt.put(y, col2Cnt.getOrDefault(y, 0) + 1);
row2Col.put(x, col2Cnt);
}
public int count(int[] point) {
int x = point[0], y = point[1];
int ans = 0;
Map<Integer, Integer> col2Cnt = row2Col.getOrDefault(x, new HashMap<>());
for (int ny : col2Cnt.keySet()) {
if (ny == y) continue;
int c1 = col2Cnt.get(ny);
int len = y - ny;
int[] nums = new int[]{x + len, x - len};
for (int nx : nums) {
Map<Integer, Integer> temp = row2Col.getOrDefault(nx, new HashMap<>());
int c2 = temp.getOrDefault(y, 0), c3 = temp.getOrDefault(ny, 0);
ans += c1 * c2 * c3;
}
}
return ans;
}
}
C++ 代码:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33class DetectSquares {
public:
unordered_map<int, unordered_map<int, int>> row2Col;
DetectSquares() {}
void add(vector<int> point) {
int x = point[0], y = point[1];
row2Col[x][y]++;
}
int count(vector<int> point) {
int x = point[0], y = point[1];
int ans = 0;
if (row2Col.find(x) == row2Col.end()) return 0;
auto& col2Cnt = row2Col[x];
for (auto& entry : col2Cnt) {
int ny = entry.first;
if (ny == y) continue;
int c1 = entry.second;
int len = y - ny;
vector<int> nxOptions = {x + len, x - len};
for (int nx : nxOptions) {
if (row2Col.find(nx) == row2Col.end()) continue;
auto& temp = row2Col[nx];
int c2 = (temp.find(y) != temp.end()) ? temp[y] : 0;
int c3 = (temp.find(ny) != temp.end()) ? temp[ny] : 0;
ans += c1 * c2 * c3;
}
}
return ans;
}
};
Python 代码:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24class DetectSquares:
def __init__(self):
self.row2Col = defaultdict(lambda: defaultdict(int))
def add(self, point: List[int]) -> None:
x, y = point
self.row2Col[x][y] += 1
def count(self, point: List[int]) -> int:
x, y = point
ans = 0
col2Cnt = self.row2Col.get(x, {})
for ny in col2Cnt:
if ny == y:
continue
c1 = col2Cnt[ny]
length = y - ny
for nx in [x + length, x - length]:
temp = self.row2Col.get(nx, {})
c2 = temp.get(y, 0)
c3 = temp.get(ny, 0)
ans += c1 * c2 * c3
return ans
Java 代码(数组充当哈希表):1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27class DetectSquares {
int N = 1010;
int[][] cnts = new int[N][N];
public void add(int[] point) {
int x = point[0], y = point[1];
cnts[x][y]++;
}
public int count(int[] point) {
int x = point[0], y = point[1];
int ans = 0;
for (int ny = 0; ny < N; ny++) {
if (y == ny) continue;
int c1 = cnts[x][ny];
if (c1 == 0) continue;
int len = y - ny;
int[] nums = new int[]{x + len, x - len};
for (int nx : nums) {
if (nx < 0 || nx >= N) continue;
int c2 = cnts[nx][y], c3 = cnts[nx][ny];
ans += c1 * c2 * c3;
}
}
return ans;
}
}
- 时间复杂度:
add
操作的复杂度为 $O(1)$,count
最坏情况会扫描完所有此前加入的点,复杂度为 $O(n)$ - 空间复杂度:$O(n)$
最后
这是我们「刷穿 LeetCode」系列文章的第 No.2013
篇,系列开始于 2021/01/01,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先把所有不带锁的题目刷完。
在这个系列文章里面,除了讲解解题思路以外,还会尽可能给出最为简洁的代码。如果涉及通解还会相应的代码模板。
为了方便各位同学能够电脑上进行调试和提交代码,我建立了相关的仓库:https://github.com/SharingSource/LogicStack-LeetCode 。
在仓库地址里,你可以看到系列文章的题解链接、系列文章的相应代码、LeetCode 原题链接和其他优选题解。
本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!