力扣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;
    }
相关推荐
leobertlan2 小时前
痛苦系列 | DSP-03 跨越单位圆:Z变换与零极点分析
算法·程序员
蛋蛋的就会顺顺的2 小时前
hot100 p0——栈,贪心算法,动态规划
java·前端·javascript·算法·leetcode·力扣
开发小能手1233 小时前
答案daanC语言
数据结构·算法
Navigator_Z5 小时前
LeetCode //C - 1291. Sequential Digits
c语言·算法·leetcode
pride.li6 小时前
第03章 Python 教程-基础语法:注释与变量
服务器·前端·后端·python·算法·计算机视觉
旺仔仔仔7 小时前
机器人项目实战(5):从单次抓取,到接收指令连续做事
算法
旺仔仔仔7 小时前
机器人项目实战(2):识别物体、提取点云,再到抓取姿态
算法
姜鱼问生7 小时前
密码明明一样,为什么 GPG 说“错的“?
后端·算法·架构
启观川7 小时前
数据结构与算法-第 3 章 常用算法-贪心算法
数据结构·笔记·python·算法·贪心算法
对空六课7 小时前
指标口径生效时间的版本查询实现
大数据·服务器·前端·算法·数据分析