The Skyline Problem Leetcode Java
来源:互联网 发布:什么是网络民生新闻 编辑:程序博客网 时间:2024/06/04 19:02
public class Solution { class KeyPoint { public int key; public int height; public KeyPoint next = null; public KeyPoint(int key, int height) { this.key = key; this.height = height; } } public static int[] getKeyPoint(int key, int height) { int[] kp = new int[2]; kp[0] = key; kp[1] = height; return kp; } public List<int[]> getSkyline(int[][] buildings) { KeyPoint head = new KeyPoint(-1,0); KeyPoint prevKP = head; for (int[] building:buildings) { int l = building[0], r = building[1], h= building[2]; // insert left point while (prevKP.next != null && prevKP.next.key <= l) prevKP = prevKP.next; int preHeight = prevKP.height; if (prevKP.key == l) prevKP.height = Math.max(prevKP.height, h); else if (prevKP.height < h) { KeyPoint next = prevKP.next; prevKP.next = new KeyPoint(l, h); prevKP = prevKP.next; prevKP.next = next; } // insert right point and update points in between KeyPoint prev = prevKP, cur = prevKP.next; while (cur != null && cur.key < r) { preHeight = cur.height; cur.height = Math.max(cur.height, h); if (cur.height == prev.height) prev.next = cur.next; else prev = cur; cur = cur.next; } if (prev.height != preHeight && prev.key != r && (cur == null || cur.key != r)) { KeyPoint next = prev.next; prev.next = new KeyPoint(r, preHeight); prev.next.next = next; } } // convert to List<int[]> List<int[]> list = new ArrayList<int[]>(); KeyPoint prev = head, cur = head.next; while (cur != null) { if (cur.height != prev.height) list.add(getKeyPoint(cur.key, cur.height)); prev = cur; cur = cur.next; } return list; }}
0 0
- The Skyline Problem Leetcode Java
- leetcode - The Skyline Problem
- LeetCode - The Skyline Problem
- leetcode:the skyline problem
- The Skyline Problem -- leetcode
- Leetcode -- The Skyline Problem
- leetcode 218. The Skyline Problem java代码
- leetcode 218: The Skyline Problem
- Leetcode 218 The Skyline Problem
- leetcode 218: The Skyline Problem
- Leetcode #218 The Skyline Problem
- [leetcode] 218.The Skyline Problem
- [LeetCode 218] The Skyline Problem
- LeetCode 218. The Skyline Problem
- LeetCode 218. The Skyline Problem
- [LeetCode]--218. The Skyline Problem
- [LeetCode]218. The Skyline Problem
- 218. The Skyline Problem [leetcode]
- Android1.5中的AVD 总结
- WPF模仿QQ登录按钮
- windows系统卸载oracle(简单步骤,完全卸载)
- iOS7下隐藏status bar的详细研究
- 织梦dedecms|软件下载页面标签
- The Skyline Problem Leetcode Java
- 【数据可视化】大规模多变量空间数据场可视化
- 算法导论—最大子数组问题
- 博客地址收藏
- 求最大子数组
- 织梦dedecms|A标签里面的属性
- Android 长按Listview显示CheckBox,实现批量删除。
- 黑马程序员---网络编程之文件上传
- linux学习一