当前位置: 首页 > news >正文

Golang | Leetcode Golang题解之第218题天际线问题

题目:

题解:

type pair struct{ right, height int }
type hp []pairfunc (h hp) Len() int            { return len(h) }
func (h hp) Less(i, j int) bool  { return h[i].height > h[j].height }
func (h hp) Swap(i, j int)       { h[i], h[j] = h[j], h[i] }
func (h *hp) Push(v interface{}) { *h = append(*h, v.(pair)) }
func (h *hp) Pop() interface{}   { a := *h; v := a[len(a)-1]; *h = a[:len(a)-1]; return v }func getSkyline(buildings [][]int) (ans [][]int) {n := len(buildings)boundaries := make([]int, 0, n*2)for _, building := range buildings {boundaries = append(boundaries, building[0], building[1])}sort.Ints(boundaries)idx := 0h := hp{}for _, boundary := range boundaries {for idx < n && buildings[idx][0] <= boundary {heap.Push(&h, pair{buildings[idx][1], buildings[idx][2]})idx++}for len(h) > 0 && h[0].right <= boundary {heap.Pop(&h)}maxn := 0if len(h) > 0 {maxn = h[0].height}if len(ans) == 0 || maxn != ans[len(ans)-1][1] {ans = append(ans, []int{boundary, maxn})}}return
}
http://www.lryc.cn/news/395999.html

相关文章:

  • 【Mars3d】osgb倾斜摄影模型加载慢卡顿的优化方案参考
  • 认识同源策略
  • ADOQuery 查询MSSQL存储过程一个莫名其妙的错误;
  • 变阻器的分类
  • 微服务节流阀:Eureka中服务限流策略的精妙实现
  • Keras实战之图像分类识别
  • Celery,一个实时处理的 Python 分布式系统
  • 源码编译安装 LAMP
  • PostgreSQL的pg_filedump工具
  • Java语言+后端+前端Vue,ElementUI 数字化产科管理平台 产科电子病历系统源码
  • Linux 服务器环境搭建
  • RabbitMQ 更改服务端口号
  • 16:9横屏短视频素材库有哪些?横屏短视频素材网站分享
  • 在Java中,创建一个实现了Callable接口的类可以提供强大的灵活性,特别是当你需要在多线程环境中执行任务并获取返回结果时。
  • Vuforia AR篇(八)— AR塔防上篇
  • Spring AOP源码篇四之 数据库事务
  • 小波与傅里叶变换的对比(Python)
  • Linux-sqlplus安装
  • LeetCode 算法:课程表 c++
  • 前端面试题30(闭包和作用域链的关系)
  • A股本周在3000点以下继续筑底,本周依然继续探底?
  • Javadoc介绍
  • C# Application.DoEvents()的作用
  • IDEA如何创建原生maven子模块
  • LCD EMC 辐射 测试随想
  • Docker安装遇到问题:curl: (7) Failed to connect to download.docker.com port 443: 拒绝连接
  • 阿里云安装rabbitMQ
  • 中文大模型基准测评2024上半年报告
  • 新火种AI|OpenAI的CEO又有新动作?这次他成立了AI健康公司
  • 中介子方程五十