题目浅析

  • 想查看原题可以点击题目链接

  • 简单地说,就是给一个二维数组代表点集,求其中上下左右都有点的点的个数。

思路分享

代码解答(强烈建议自行解答后再看)

  • 参考题解
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class Solution:
def countCoveredBuildings(self, n: int, buildings: List[List[int]]) -> int:
vertical_rec_high = [-inf]*(n+1)
vertical_rec_low = [inf]*(n+1)
horizon_rec_left = [inf]*(n+1)
horizon_rec_right = [-inf]*(n+1)
for x, y in buildings:
vertical_rec_high[x] = max(vertical_rec_high[x], y)
vertical_rec_low[x] = min(vertical_rec_low[x], y)
horizon_rec_left[y] = min(horizon_rec_left[y], x)
horizon_rec_right[y] = max(horizon_rec_right[y], x)
ans = 0
for x, y in buildings:
if horizon_rec_left[y] < x < horizon_rec_right[y] and vertical_rec_low[x] < y < vertical_rec_high[x]:
ans += 1
return ans