力扣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;
    }
相关推荐
Liu6288816 小时前
C++中的工厂模式高级应用
开发语言·c++·算法
AI科技星16 小时前
全尺度角速度统一:基于 v ≡ c 的纯推导与验证
c语言·开发语言·人工智能·opencv·算法·机器学习·数据挖掘
参.商.17 小时前
【Day41】143. 重排链表
leetcode·golang
条tiao条17 小时前
KMP 算法详解:告别暴力匹配,让字符串匹配 “永不回头”
开发语言·算法
干啥啥不行,秃头第一名17 小时前
C++20概念(Concepts)入门指南
开发语言·c++·算法
zzh9407717 小时前
Gemini 3.1 Pro 硬核推理优化剖析:思维织锦、动态计算与国内实测
算法
2301_8073671918 小时前
C++中的解释器模式变体
开发语言·c++·算法
愣头不青18 小时前
617.合并二叉树
java·算法
MIUMIUKK18 小时前
双指针三大例题
算法
灵感__idea18 小时前
Hello 算法:复杂问题的应对策略
前端·javascript·算法