用HashHeap 和扫描线算法破解谷歌面试题:大楼轮廓

用HashHeap 和扫描线算法破解谷歌面试题:大楼轮廓

水平面上有 N 座大楼,每座大楼都是矩阵的形状,可以用一个三元组表示 (start, end, height),分别代表其在x轴上的起点,终点和高度。大楼之间从远处看可能会重叠,求出 N 座大楼的外轮廓线。

外轮廓线的表示方法为若干三元组,每个三元组包含三个数字 (start, end, height),代表这段轮廓的起始位置,终止位置和高度。

在线评测地址

样例1
输入:
[
    [1, 3, 3],
    [2, 4, 4],
    [5, 6, 1]
]
输出:
[
    [1, 2, 3],
    [2, 4, 4],
    [5, 6, 1]
]
说明:
建筑物如下图所示,黄色部分表示建筑物

在这里插入图片描述

样例2

输入:
[
    [1, 4, 3],
    [6, 9, 5]
]
输出:
[
    [1, 4, 3],
    [6, 9, 5]
]
说明:
建筑物如下图所示,黄色部分表示建筑物

在这里插入图片描述

【题解】

使用九章算法强化班中讲过的 HashHeap 和扫描线算法。 Java 可以用 TreeSet / TreeMap, C++ 可以用 Map.

import java.util.*;

public class Solution {

  class HashHeap {
    ArrayList<Integer> heap;
    String mode;
    int size_t;
    HashMap<Integer, Node> hash;

    class Node {
      public Integer id;
      public Integer num;

      Node(Node now) {
        id = now.id;
        num = now.num;
      }

      Node(Integer first, Integer second) {

        this.id = first;
        this.num = second;
      }
    }

    public HashHeap(String mod) {
      // TODO Auto-generated constructor stub
      heap = new ArrayList<Integer>();
      mode = mod;
      hash = new HashMap<Integer, Node>();
      size_t = 0;
    }

    public int peek() {
      return heap.get(0);
    }

    public int size() {
      return size_t;
    }

    public Boolean isEmpty() {
      return (heap.size() == 0);
    }

    int parent(int id) {
      if (id == 0) {
        return -1;
      }
      return (id - 1) / 2;
    }

    int lson(int id) {
      return id * 2 + 1;
    }

    int rson(int id) {
      return id * 2 + 2;
    }

    boolean comparesmall(int a, int b) {
      if (a <= b) {
        if (mode == "min")
          return true;
        else
          return false;
      } else {
        if (mode == "min")
          return false;
        else
          return true;
      }

    }

    void swap(int idA, int idB) {
      int valA = heap.get(idA);
      int valB = heap.get(idB);

      int numA = hash.get(valA).num;
      int numB = hash.get(valB).num;
      hash.put(valB, new Node(idA, numB));
      hash.put(valA, new Node(idB, numA));
      heap.set(idA, valB);
      heap.set(idB, valA);
    }

    public Integer poll() {
      size_t--;
      Integer now = heap.get(0);
      Node hashnow = hash.get(now);
      if (hashnow.num == 1) {
        swap(0, heap.size() - 1);
        hash.remove(now);
        heap.remove(heap.size() - 1);
        if (heap.size() > 0) {
          siftdown(0);
        }
      } else {
        hash.put(now, new Node(0, hashnow.num - 1));
      }
      return now;
    }

    public void add(int now) {
      size_t++;
      if (hash.containsKey(now)) {
        Node hashnow = hash.get(now);
        hash.put(now, new Node(hashnow.id, hashnow.num + 1));

      } else {
        heap.add(now);
        hash.put(now, new Node(heap.size() - 1, 1));
      }

      siftup(heap.size() - 1);
    }

    public void delete(int now) {
      size_t--;
      Node hashnow = hash.get(now);
      int id = hashnow.id;
      int num = hashnow.num;
      if (hashnow.num == 1) {

        swap(id, heap.size() - 1);
        hash.remove(now);
        heap.remove(heap.size() - 1);
        if (heap.size() > id) {
          siftup(id);
          siftdown(id);
        }
      } else {
        hash.put(now, new Node(id, num - 1));
      }
    }

    void siftup(int id) {
      while (parent(id) > -1) {
        int parentId = parent(id);
        if (comparesmall(heap.get(parentId), heap.get(id)) == true) {
          break;
        } else {
          swap(id, parentId);
        }
        id = parentId;
      }
    }

    void siftdown(int id) {
      while (lson(id) < heap.size()) {
        int leftId = lson(id);
        int rightId = rson(id);
        int son;
        if (rightId >= heap.size()
            || (comparesmall(heap.get(leftId), heap.get(rightId)) == true)) {
          son = leftId;
        } else {
          son = rightId;
        }
        if (comparesmall(heap.get(id), heap.get(son)) == true) {
          break;
        } else {
          swap(id, son);
        }
        id = son;
      }
    }
  }

  class Edge {
    int pos;
    int height;
    boolean isStart;

    public Edge(int pos, int height, boolean isStart) {
      this.pos = pos;
      this.height = height;
      this.isStart = isStart;
    }

  }

  class EdgeComparator implements Comparator<Edge> {
    @Override
    public int compare(Edge arg1, Edge arg2) {
      Edge l1 = (Edge) arg1;
      Edge l2 = (Edge) arg2;
      if (l1.pos != l2.pos)
        return compareInteger(l1.pos, l2.pos);
      if (l1.isStart && l2.isStart) {
        return compareInteger(l2.height, l1.height);
      }
      if (!l1.isStart && !l2.isStart) {
        return compareInteger(l1.height, l2.height);
      }
      return l1.isStart ? -1 : 1;
    }

    int compareInteger(int a, int b) {
      return a <= b ? -1 : 1;
    }
  }

  List<List<Integer>> output(List<List<Integer>> res) {
    List<List<Integer>> ans = new ArrayList<List<Integer>>();
    if (res.size() > 0) {
      int pre = res.get(0).get(0);
      int height = res.get(0).get(1);
      for (int i = 1; i < res.size(); i++) {
        List<Integer> now = new ArrayList<Integer>();
        int id = res.get(i).get(0);
        if (height > 0) {
          now.add(pre);
          now.add(id);
          now.add(height);
          ans.add(now);
        }
        pre = id;
        height = res.get(i).get(1);
      }
    }
    return ans;
  }

  public List<List<Integer>> buildingOutline(int[][] buildings) {
    // write your code here
    List<List<Integer>> res = new ArrayList<List<Integer>>();

    if (buildings == null || buildings.length == 0
        || buildings[0].length == 0) {
      return res;
    }
    ArrayList<Edge> edges = new ArrayList<Edge>();
    for (int[] building : buildings) {
      Edge startEdge = new Edge(building[0], building[2], true);
      edges.add(startEdge);
      Edge endEdge = new Edge(building[1], building[2], false);
      edges.add(endEdge);
    }
    Collections.sort(edges, new EdgeComparator());

    HashHeap heap = new HashHeap("max");

    List<Integer> now = null;
    for (Edge edge : edges) {
      if (edge.isStart) {
        if (heap.isEmpty() || edge.height > heap.peek()) {
          now = new ArrayList<Integer>(Arrays.asList(edge.pos,
              edge.height));
          res.add(now);
        }
        heap.add(edge.height);
      } else {
        heap.delete(edge.height);
        if (heap.isEmpty() || edge.height > heap.peek()) {
          if (heap.isEmpty()) {
            now = new ArrayList<Integer>(Arrays.asList(edge.pos, 0));
          } else {
            now = new ArrayList<Integer>(Arrays.asList(edge.pos,
                heap.peek()));
          }
          res.add(now);
        }
      }
    }
    return output(res);
  }

}

点击获取更多题解

相关推荐

巨硬家面试题:字符串查找(带解题思路)

对于一个给定的 source 字符串和一个 target 字符串,你应该在 source 字符串中找出 target 字符串出现的第一个位置(从0开始)。如果不存在,则返回 -1。在线做题地址说明在面试中我是否需要实现KMP算法?不需要,当这种问题出现在面试中时,面试官很可能只是想要测试一下你的基础应用能力。当然你需要先跟面试官确认清楚要怎么实现这个题。《九章算法班》中讲过比KMP更简单的算法:Rabin-karp 算法,可免费试听。样例 1:输入: source = "source" ,

LintCode的博客 1230

阿里2020最新动态规划题:背包问题

在n个物品中挑选若干物品装入背包,最多能装多满?假设背包的大小为m,每个物品的大小为A[i]。你不可以将物品进行切割。点此在线做题样例 1:输入: [3,4,8,5], backpack size=10输出: 9样例 2:输入: [2,3,5,7], backpack size=12输出: 12算法:DP从已知的题目中,可以总结出以下两点:每件物品只有一种每件物品最多选择一次那么考虑对于前i件的物品在容量为w的背包下,最大的装载量是多少,由此可以总结出对应的子结构,进

LintCode的博客 681

百度最新面试题:祖玛游戏思路详解

这是一个祖玛游戏。一行球放在桌面上,球被涂上了红(R)、黄(Y)、蓝(B)、绿(G)和白(W)这么几种颜色,同时你也拥有几个球。每一次,从你拥有的球当中拿出一个,插入到当前行当中(包括最左边和最右边)。然后,如果有三个或更多的同色球挨在一起,然后就消除这几个球。一直这么做直到没有更多的球可以消除。找到最少的需要插入的球的数量,使得所有的球都可以被删除。如果不能删除所有的球,那么就返回-1。初始的行不会拥有三个及以上的连续的同色球。行中的球的数量不会超过20,用名为"borad"的输入字符串表示。

LintCode的博客 1079

LeetCode/LintCode 题解丨一周爆刷分治法:合并两棵二叉树

LeetCode/LintCode 题解丨一周爆刷分治法:合并两棵二叉树

LintCode的博客 328

LeetCode/LintCode 题解丨一周爆刷字符串:乱序字符串

LeetCode/LintCode 题解丨一周爆刷字符串:乱序字符串

LintCode的博客 330

LeetCode/LintCode 题解丨一周爆刷字符串:文字并排

LeetCode/LintCode 题解丨一周爆刷字符串:文字并排

LintCode的博客 326

LeetCode/LintCode 题解丨一周爆刷字符串:简化路径

LeetCode/LintCode 题解丨一周爆刷字符串:简化路径

LintCode的博客 1478

LeetCode/LintCode 题解丨一周爆刷字符串:神奇字符串

LeetCode/LintCode 题解丨一周爆刷字符串:神奇字符串

LintCode的博客 398

LeetCode/LintCode 题解丨一周爆刷字符串:查找和替换模式

LeetCode/LintCode 题解丨一周爆刷字符串:查找和替换模式

LintCode的博客 363

LeetCode/LintCode 题解丨一周爆刷字符串:旋转字符数组

LeetCode/LintCode 题解丨一周爆刷字符串:旋转字符数组

LintCode的博客 1676

LeetCode/LintCode 题解丨一周爆刷字符串:URL 编码

LeetCode/LintCode 题解丨一周爆刷字符串:URL 编码

LintCode的博客 760

LeetCode/LintCode 题解丨一周爆刷字符串:空格替换

LeetCode/LintCode 题解丨一周爆刷字符串:空格替换

LintCode的博客 294

LeetCode/LintCode 题解丨一周爆刷字符串:独特的摩尔斯编码

LeetCode/LintCode 题解丨一周爆刷字符串:独特的摩尔斯编码

LintCode的博客 293

LeetCode/LintCode 题解丨一周爆刷双指针:寻找重复的数

LeetCode/LintCode 题解丨一周爆刷双指针:寻找重复的数

LintCode的博客 375

LeetCode/LintCode 题解丨一周爆刷双指针:神奇字符串

LeetCode/LintCode 题解丨一周爆刷双指针:神奇字符串

LintCode的博客 333

LeetCode/LintCode 题解丨一周爆刷双指针:最小范围

LeetCode/LintCode 题解丨一周爆刷双指针:最小范围

LintCode的博客 1909

LeetCode/LintCode 题解丨一周爆刷双指针: 两数之和

LeetCode/LintCode 题解丨一周爆刷双指针: 两数之和

LintCode的博客 274

LeetCode/LintCode 题解丨一周爆刷双指针:K个空的位置

LeetCode/LintCode 题解丨一周爆刷双指针:K个空的位置

LintCode的博客 308

LeetCode/LintCode 题解丨一周爆刷双指针:数组中的最长山脉

LeetCode/LintCode 题解丨一周爆刷双指针:数组中的最长山脉

LintCode的博客 299

2022年Apple春招真题:二叉树的层次遍历

2022年Apple春招真题:二叉树的层次遍历

LintCode的博客 321

2022年美团春招真题:字符串排序

2022年美团春招真题:字符串排序

LintCode的博客 289

2022年百度春招真题:最长上升子序列

2022年百度春招真题:最长上升子序列

LintCode的博客 221

2022年微软面试真题:最大子数组

描述:给定一个整数数组,找到一个具有最大和的子数组,返回其最大和。每个子数组的数字在数组中的位置应该是连续的。样例 1:输入:nums = [−2,2,−3,4,−1,2,1,−5,3]输出:6解释:符合要求的子数组为[4,−1,2,1],其最大和为 6。样例 2:输入:nums = [1,2,3,4]输出:10解释:符合要求的子数组为[1,2,3,4],其最大和为 10。挑战要求时间复杂度为O(n)...

LintCode的博客 381

亚马逊面试真题:二叉树的层次遍历

描述:给出一棵二叉树,返回其节点值的层次遍历(逐层从左往右访问)首个数据为根节点,后面接着是其左儿子和右儿子节点值,"#"表示不存在该子节点。节点数量不超过20。在线测评地址样例 1:输入:tree = {1,2,3}输出:[[1],[2,3]]解释:1/ \2 3它将被序列化为{1,2,3}样例 2:输入:tree = {1,#,2,3} 输出:[[1],[2],[3]] 解释:1 \ 2 / 3 它将被序列化为{1,#,2,3}挑

LintCode的博客 408

亚马逊经典真题:三数之和

描述:给出一个有n个整数的数组S,在S中找到三个整数a, b, c,找到所有使得a + b + c = 0的三元组。在线测评地址样例 1:输入:numbers = [2,7,11,15]输出:[]解释:找不到三元组使得三个数和为0。样例 2:输入:numbers = [-1,0,1,2,-1,-4]输出:[[-1, 0, 1],[-1, -1, 2]]解释:[-1, 0, 1]和[-1, -1, 2]是符合条件的2个三元组。源代码:class Solution:

LintCode的博客 520

谷歌真题回顾:下一个排列

不能错过的谷歌面试真题

LintCode的博客 289

微软面试真题:全排列

解决面试中的全排列问题

LintCode的博客 220

亚马逊面试宝典:两数之和

描述:给一个整数数组,找到两个数使得他们的和等于一个给定的数 target。你需要实现的函数twoSum需要返回这两个数的下标, 并且第一个下标小于第二个下标。注意这里下标的范围是 0 到 n-1。在线测评地址样例 1:输入:numbers = [2,7,11,15]target = 9输出:[0,1]解释:numbers[0] + numbers[1] = 9样例 2:输入:numbers = [15,2,7,11]target = 9输出:[1,2]解释:num

LintCode的博客 159

谷歌面试真题分享

谷歌面试题:插入区间描述:给出一个无重叠的按照区间起始端点排序的区间列表。在列表中插入一个新的区间,你要确保列表中的区间仍然有序且不重叠(如果有必要的话,可以合并区间)。在线测评地址样例 1:输入:区间列表 = [(1,2), (5,9)]新的区间 = (2, 5)输出:[(1,9)]解释:插入后区间有重叠,需要合并区间。样例 2:输入:区间列表 = [(1,2), (5,9)]新的区间 = (3, 4)输出:[(1,2), (3,4), (5,9)]解释:区间按

LintCode的博客 481

微软面试真题分享

想进入大厂工作,你不能错过的经验分享

LintCode的博客 259
上一篇: 经典算法题:约瑟夫问题暴力解法
下一篇: 分享一道字节跳动面试题
LintCode
LintCode 企业官方账号 企业官方账号
博客等级 码龄6年 15粉丝 113原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值