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

题目

给你一个正整数 n,表示一个 n x n 的城市,同时给定一个二维数组 buildings,其中 buildingsi = 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

buildingsi = x, y

1 <= x, y <= n

buildings 中所有坐标均 唯一 。

思路:

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

例如对于坐标

{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;
    }
相关推荐
leobertlan3 分钟前
好玩系列:训练一个神经网络模型指导小孩玩游戏2-大局观教练
算法
Tisfy1 小时前
LeetCode 3876.构造奇偶一致的数组 II:三种情况分类讨论(其实还是脑筋急转弯)
算法·leetcode·题解·脑筋急转弯
乐迪信息1 小时前
智慧港口船舶AI算法实现在线状态监测
大数据·人工智能·深度学习·算法·计算机视觉
木井巳3 小时前
【BFS/DFS 解决 FloodFill 算法】太平洋大西洋水流问题
java·算法·leetcode·深度优先·广度优先·宽度优先·推荐算法
心抵鹊3 小时前
归并排序之翻转对(hard)
数据结构·算法
白山编程大哥3 小时前
Java 集合算法:从排序、查找到底层原理的实战指南
java·python·算法
shehuiyuelaiyuehao4 小时前
算法34,位运算符操作,总结
算法
Navigator_Z4 小时前
LeetCode //C - 1224. Maximum Equal Frequency
c语言·算法·leetcode
Navigator_Z5 小时前
LeetCode //C++ - 1226. The Dining Philosophers
c语言·算法·leetcode
FOORIR5 小时前
3D视觉+AI客流系统怎么做:从Sensor Pipeline到数据事件引擎
算法