力扣3531——统计被覆盖的建筑

题目

给你一个正整数 n,表示一个 n x n 的城市,同时给定一个二维数组 buildings,其中 buildings[i] = [x, y] 表示位于坐标 [x, y] 的一个 唯一 建筑。

如果一个建筑在四个方向(左、右、上、下)中每个方向上都至少存在一个建筑,则称该建筑 被覆盖 。

返回 被覆盖 的建筑数量。

示例

示例 1:

输入: n = 3, buildings = [[1,2],[2,2],[3,2],[2,1],[2,3]]

输出: 1

解释: 只有建筑[2,2] 被覆盖,因为它在每个方向上都至少存在一个建筑:

上方 ([1,2])

下方 ([3,2])

左方 ([2,1])

右方([2,3])

因此,被覆盖的建筑数量是 1。

示例 2:

输入: n = 3, buildings = [[1,1],[1,2],[2,1],[2,2]]

输出: 0

解释:

没有任何一个建筑在每个方向上都有至少一个建筑。

示例 3:

输入: n = 5, buildings = [[1,3],[3,2],[3,3],[3,5],[5,3]]

输出: 1

解释: 只有建筑[3,3] 被覆盖,因为它在每个方向上至少存在一个建筑:

上方 ([1,3])

下方 ([5,3])

左方 ([3,2])

右方([3,5])

因此,被覆盖的建筑数量是 1。

提示:

2 <= n <= 105

1 <= buildings.length <= 105

buildings[i] = [x, y]

1 <= x, y <= n

buildings 中所有坐标均 唯一 。

思路:

定义两个横纵方向的哈希数组,分别计算两个方向每个坐标的最大值和最小值,如果building[i]在该范围内,则该建筑被覆盖

例如对于坐标

{1,3},{3,2},{3,3},{3,5},{5,3}

初始化mapRow为

{0,[Integer.MAX_VALUE,Integer.MIN_VALUE]} //表示第0行的最小值和最大值

{1,[Integer.MAX_VALUE,Integer.MIN_VALUE]}

{2,[Integer.MAX_VALUE,Integer.MIN_VALUE]}

{3,[Integer.MAX_VALUE,Integer.MIN_VALUE]}

{4,[Integer.MAX_VALUE,Integer.MIN_VALUE]}

{5,[Integer.MAX_VALUE,Integer.MIN_VALUE]}

初始化mapCol为

{0,[Integer.MAX_VALUE,Integer.MIN_VALUE]} //表示第0列的最小值和最大值

{1,[Integer.MAX_VALUE,Integer.MIN_VALUE]}

{2,[Integer.MAX_VALUE,Integer.MIN_VALUE]}

{3,[Integer.MAX_VALUE,Integer.MIN_VALUE]}

{4,[Integer.MAX_VALUE,Integer.MIN_VALUE]}

{5,[Integer.MAX_VALUE,Integer.MIN_VALUE]}

对于数字{1,3}修改mapRow为

{0,[Integer.MAX_VALUE,Integer.MIN_VALUE]} //表示第0行的最小值和最大值

{1,[Integer.MAX_VALUE,Integer.MIN_VALUE]}

{2,[Integer.MAX_VALUE,Integer.MIN_VALUE]}

{3,[1,1]}. //表示第3行的最小值和最大值分别是(1,1)

{4,[Integer.MAX_VALUE,Integer.MIN_VALUE]}

{5,[Integer.MAX_VALUE,Integer.MIN_VALUE]}

修改mapCol为

{0,[Integer.MAX_VALUE,Integer.MIN_VALUE]} //表示第0行的最小值和最大值

{1,[3,3]} //表示第1列的最小值和最大值分别是(3,3)

{2,[Integer.MAX_VALUE,Integer.MIN_VALUE]}

{3,[Integer.MAX_VALUE,Integer.MIN_VALUE]}.

{4,[Integer.MAX_VALUE,Integer.MIN_VALUE]}

{5,[Integer.MAX_VALUE,Integer.MIN_VALUE]}


最后的mapRow为

最后的mapCol为

代码

java 复制代码
    public static int countCoveredBuildings(int n, int[][] buildings) {
        HashMap<Integer, int[]> mapRow = new HashMap<>();//第i行的最大值和最小值
        HashMap<Integer, int[]> mapCol = new HashMap<>();//第i列的最大值和最小值
        for(int i = 0; i < n + 1;i++){
            mapRow.put(i, new int[]{Integer.MAX_VALUE,Integer.MIN_VALUE});
            mapCol.put(i, new int[]{Integer.MAX_VALUE,Integer.MIN_VALUE});
        }
        int result = 0;
        for(int[] building : buildings){
            if(building[0]>mapRow.get(building[1])[1])
                mapRow.put(building[1],new int[]{mapRow.get(building[1])[0],building[0]});
            if(building[0]<mapRow.get(building[1])[0])
                mapRow.put(building[1],new int[]{building[0],mapRow.get(building[1])[1]});
            if(building[1]>mapCol.get(building[0])[1])
                mapCol.put(building[0],new int[]{mapCol.get(building[0])[0],building[1]});
            if(building[1]<mapCol.get(building[0])[0])
                mapCol.put(building[0],new int[]{building[1],mapCol.get(building[0])[1]});
        }
        for(int[] building : buildings){
            if(building[0] < mapRow.get(building[1])[1] && building[0] > mapRow.get(building[1])[0] && building[1] < mapCol.get(building[0])[1] && building[1] > mapCol.get(building[0])[0]){
                result += 1;
            }
        }
        return result;
    }
相关推荐
core5122 小时前
深度解析DeepSeek-R1中GRPO强化学习算法
人工智能·算法·机器学习·deepseek·grpo
mit6.8242 小时前
计数if|
算法
a伊雪3 小时前
c++ 引用参数
c++·算法
圣保罗的大教堂3 小时前
leetcode 3531. 统计被覆盖的建筑 中等
leetcode
Data_agent3 小时前
1688获得1688店铺列表API,python请求示例
开发语言·python·算法
2301_764441334 小时前
使用python构建的应急物资代储博弈模型
开发语言·python·算法
hetao17338374 小时前
2025-12-11 hetao1733837的刷题笔记
c++·笔记·算法
Xの哲學4 小时前
Linux电源管理深度剖析
linux·服务器·算法·架构·边缘计算