力扣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;
    }
相关推荐
风中的微尘3 小时前
数学基础2
算法
一只小小的芙厨4 小时前
树状数组总结
数据结构·算法
郝学胜-神的一滴6 小时前
Effective STL 条款 17:用 swap 技巧去掉多余容量
c语言·开发语言·数据结构·c++·算法·rpc
玩转单片机与嵌入式6 小时前
深入浅出TinyML 36:如何验证一个TinyML系统真正具备工程应用价值?
网络·人工智能·算法
Nazarick8 小时前
力扣LeetCode热题75题(1)
java·算法·leetcode·力扣
16月6日-晴8 小时前
斐波那契数列题解
算法·leetcode
郝学胜-神的一滴9 小时前
Effective Python 条款47:吃透惰性属性
大数据·开发语言·数据库·人工智能·python·算法·oracle
Keven_1110 小时前
算法札记:有限差分
算法
忙里偷闲学python10 小时前
GK2A 林火蔓延预报算法说明
算法
我是伪码农10 小时前
11.21程序设计
数据结构·算法