- 浏览: 137710 次
文章分类
- 全部博客 (189)
- Tree (14)
- Dynamic Programming (34)
- Array (20)
- Search (1)
- Hash (12)
- Backtracking (22)
- Divide and Conque (8)
- Greedy (6)
- Stack (12)
- software (0)
- List (7)
- Math (22)
- Two pointers (16)
- String (20)
- Linux (1)
- Sliding Window (4)
- Finite State Machine (1)
- Breadth-first Search (7)
- Graph (4)
- DFS (6)
- BFS (3)
- Sort (9)
- 基础概念 (2)
- 沟通表达 (0)
- Heap (2)
- Binary Search (15)
- 小结 (1)
- Bit Manipulation (8)
- Union Find (4)
- Topological Sort (1)
- PriorityQueue (1)
- Design Pattern (1)
- Design (1)
- Iterator (1)
- Queue (1)
最新评论
-
likesky3:
看了数据结构书得知并不是迭代和递归的区别,yb君的写法的效果是 ...
Leetcode - Graph Valid Tree -
likesky3:
迭代和递归的区别吧~
Leetcode - Graph Valid Tree -
qb_2008:
还有一种find写法:int find(int p) { i ...
Leetcode - Graph Valid Tree -
qb_2008:
要看懂这些技巧的代码确实比较困难。我是这么看懂的:1. 明白这 ...
Leetcode - Single Num II -
qb_2008:
public int singleNumber2(int[] ...
Leetcode - Single Num II
[分析]
思路1参考https://leetcode.com/discuss/44537/my-java-ac-code-with-priorityqueues
记录下目前的理解:目标是求出所有拐点。所谓拐点是指让当前x坐标高度发生变化的点,当前x坐标的高度是x处所有楼房的最大高度。所有楼房的左右两顶点加入到考察集合,排序该集合,排序后使用最大堆来保存当前的楼高状态,遍历到左顶点时将高度加入最大堆,右顶点时将高度从最大堆中删除。最大堆堆顶是当前已遍历x坐标的最大高度,一旦堆顶元素变化说明拐点出现了。
各顶点排序规则是若两个点x坐标不同,按x坐标排序,否则需要分三种情况讨论:1)一个是左顶点一个是右顶点,则左顶点排前,考虑case{[0, 1, 1],[1,2,1]},{[0,1,1],[1,2,2]},{[0,1,2],[1,2,1]},也即两栋楼房紧贴在一起的情况,都让左顶点先;2)两个都是左顶点,则高度大的优先;3)两个都是右顶点,则高度小的优先。2)和3)都可考虑case{[1,2,1],[1,2,2],[1,2,3]}来帮助理解,可以想象成高楼套小楼。
思路2参考http://www.meetqun.com/thread-9858-1-1.html
思路3:http://www.geeksforgeeks.org/divide-and-conquer-set-7-the-skyline-problem/
Method 3是第二次做时自己写出来的,一开始边界case仍然没想到,但看到测试样例后修正过来了,最终Accept,很有成就感。
出题者送的话
Be warned, my friend, thou art about to do some serious coding should thou dare this path. Dauntlessly define thy classes and meticulously do thy math. Listen to the prophecy if thou must, but do not let thy own sword idle with rust. By the journey's end, may thy toil and sweat be duly repaid, and no more interviews shalt thou again be afraid.
共勉
思路1参考https://leetcode.com/discuss/44537/my-java-ac-code-with-priorityqueues
记录下目前的理解:目标是求出所有拐点。所谓拐点是指让当前x坐标高度发生变化的点,当前x坐标的高度是x处所有楼房的最大高度。所有楼房的左右两顶点加入到考察集合,排序该集合,排序后使用最大堆来保存当前的楼高状态,遍历到左顶点时将高度加入最大堆,右顶点时将高度从最大堆中删除。最大堆堆顶是当前已遍历x坐标的最大高度,一旦堆顶元素变化说明拐点出现了。
各顶点排序规则是若两个点x坐标不同,按x坐标排序,否则需要分三种情况讨论:1)一个是左顶点一个是右顶点,则左顶点排前,考虑case{[0, 1, 1],[1,2,1]},{[0,1,1],[1,2,2]},{[0,1,2],[1,2,1]},也即两栋楼房紧贴在一起的情况,都让左顶点先;2)两个都是左顶点,则高度大的优先;3)两个都是右顶点,则高度小的优先。2)和3)都可考虑case{[1,2,1],[1,2,2],[1,2,3]}来帮助理解,可以想象成高楼套小楼。
思路2参考http://www.meetqun.com/thread-9858-1-1.html
思路3:http://www.geeksforgeeks.org/divide-and-conquer-set-7-the-skyline-problem/
Method 3是第二次做时自己写出来的,一开始边界case仍然没想到,但看到测试样例后修正过来了,最终Accept,很有成就感。
出题者送的话
Be warned, my friend, thou art about to do some serious coding should thou dare this path. Dauntlessly define thy classes and meticulously do thy math. Listen to the prophecy if thou must, but do not let thy own sword idle with rust. By the journey's end, may thy toil and sweat be duly repaid, and no more interviews shalt thou again be afraid.
共勉
// Method 3: mine public class Solution { public List<int[]> getSkyline(int[][] buildings) { List<int[]> ret = new ArrayList<int[]>(); if (buildings == null || buildings.length == 0) return ret; Edge[] edges = getEdges(buildings); Arrays.sort(edges, compEdge); PriorityQueue<Integer> maxHeap = new PriorityQueue<Integer>(comp); // Style 2: more clear int oldHeight = 0; for (int i = 0; i < edges.length; i++) { Edge curr = edges[i]; if (curr.isStart) { maxHeap.offer(curr.y); } else { maxHeap.remove(curr.y); } int newHeight = maxHeap.isEmpty() ? 0 : maxHeap.peek(); if (oldHeight != newHeight) { oldHeight = newHeight; int[] point = {curr.x, newHeight}; ret.add(point); } } //Style 1: mine // for (int i = 0; i < edges.length; i++) { // Edge curr = edges[i]; // if (curr.isStart) { // if (maxHeap.isEmpty() || curr.y > maxHeap.peek()) { // int[] point = {curr.x, curr.y}; // ret.add(point); // } // maxHeap.offer(curr.y); // } else { // if (curr.y == maxHeap.peek()) { // maxHeap.poll(); // if (maxHeap.isEmpty() || maxHeap.peek() != curr.y) { // hard point // int[] point = {curr.x, maxHeap.isEmpty() ? 0 : maxHeap.peek()}; // ret.add(point); // } // } else { // maxHeap.remove(curr.y); // } // } // } return ret; } private static class Edge { int x, y; boolean isStart; public Edge(int x, int y, boolean isStart) { this.x = x; this.y = y; this.isStart = isStart; } } private Comparator<Integer> comp = new Comparator<Integer>() { public int compare(Integer a, Integer b) { return b - a; } }; private Comparator<Edge> compEdge = new Comparator<Edge>() { public int compare(Edge a, Edge b) { if (a.x == b.x) { // hard point if (a.isStart && b.isStart) return b.y - a.y; else if (!a.isStart && !b.isStart) return a.y - b.y; else return a.isStart ? -1 : 1; } else { return a.x - b.x; } } }; private Edge[] getEdges(int[][] buildings) { Edge[] edges = new Edge[buildings.length * 2]; int j = 0; for (int i = 0; i < buildings.length; i++) { edges[j++] = new Edge(buildings[i][0], buildings[i][2], true); edges[j++] = new Edge(buildings[i][1], buildings[i][2], false); } return edges; } }
// Method 1 public class Solution { public List<int[]> getSkyline(int[][] buildings) { List<int[]> result = new ArrayList<int[]>(); if (buildings == null || buildings.length == 0) return result; int N = buildings.length; Pair[] pairs = new Pair[2 * N]; for (int i = 0; i < N; i++) { pairs[2 * i] = new Pair(buildings[i][0], -buildings[i][2]); pairs[2 * i + 1] = new Pair(buildings[i][1], buildings[i][2]); } Arrays.sort(pairs); PriorityQueue<Integer> maxHeap = new PriorityQueue<Integer>(N, Collections.reverseOrder()); int height = 0; for (int i = 0; i < pairs.length; i++) { if (pairs[i].height < 0) //left start maxHeap.add(-pairs[i].height); else //right end maxHeap.remove(pairs[i].height); int newHeight = !maxHeap.isEmpty() ? maxHeap.peek() : 0; if (newHeight != height) { height = newHeight; int[] point = new int[2]; point[0] = pairs[i].x; point[1] = height; result.add(point); } } return result; } } class Pair implements Comparable<Pair>{ int x; int height; public Pair(int x, int height) { this.x = x; this.height = height; } public int compareTo(Pair p) { if (this.x != p.x) return this.x - p.x; // if the right end and left start overlap, the left start comes first if (this.height * p.height < 0) return this.height - p.height; // if both left start overlap, the larger height comes first if (this.height < 0 && p.height < 0) return Math.abs(p.height) - Math.abs(this.height); // if both right end overlap, the smaller height comes first return this.height - p.height; } }
// Method 2: 更快些 public class Solution { class Node { boolean isStart; int x; int h; } class NodeComparator implements Comparator<Node> { public int compare(Node n1, Node n2) { if (n1.x != n2.x) { return n1.x - n2.x; } if (n1.isStart != n2.isStart) { return n1.isStart ? -1 : 1; } if (n1.isStart) { return n2.h - n1.h; } return n1.h - n2.h; } } public List<int[]> getSkyline(int[][] buildings) { int n = buildings.length; ArrayList<Node> s = new ArrayList<Node>(); for (int i = 0; i < n; ++i) { Node node = new Node(); node.isStart = true; node.x = buildings[i][0]; node.h = buildings[i][2]; s.add(node); node = new Node(); node.isStart = false; node.x = buildings[i][1]; node.h = buildings[i][2]; s.add(node); } Collections.sort(s, new NodeComparator()); TreeMap<Integer, Integer> height_map = new TreeMap<Integer, Integer>(); ArrayList<int[]> result = new ArrayList<int[]>(); int beginX = 0; int curH = 0; for (int i = 0; i < s.size(); ++i) { Node node = s.get(i); if (node.isStart) { Integer old = height_map.get(node.h); int newValue = 1; if (old != null) { newValue += old; } height_map.put(node.h, newValue); if (node.h > curH) { beginX = node.x; curH = node.h; result.add(new int[]{beginX, curH}); } } else { Integer old = height_map.get(node.h); if (old == 1) { height_map.remove(node.h); } else { height_map.put(node.h, old - 1); } if (height_map.size() == 0) { beginX = node.x; curH = 0; result.add(new int[]{beginX, curH}); } else { Integer h = (int)height_map.lastKey(); if (h != curH) { beginX = node.x; curH = h; result.add(new int[]{beginX, curH}); } } } } return result; } }
发表评论
-
Leetcode - Sort List
2015-08-15 17:41 491Sort a linked list in O(n log n ... -
Leetcode - Different Ways to Add Parentheses
2015-07-29 20:21 1201Given a string of numbers and o ... -
Leetcode - Sliding Window Maximum
2015-07-23 09:18 563Given an array nums, there is a ... -
Leetcode - Median of Two Sorted Arrays
2015-07-12 11:02 458There are two sorted arrays num ... -
Leetcode - Merge k Sorted Lists
2015-07-08 09:57 469Merge k sorted linked lists and ... -
Leetcode - Kth Largest Element in an Array
2015-05-23 21:25 547Find the kth largest element ... -
Leetcode - Best Time to Buy and Sell Stock IV
2015-04-23 09:59 0public class Solution ... -
Leetcode - Best Time to Buy and Sell Stock III
2015-04-23 09:04 484Say you have an array for whi ... -
Leetcode - Unique Binary Search Trees II
2015-04-20 09:30 518Given n, generate all structu ...
相关推荐
《在IDE中高效进行LeetCode练习:leetcode-editor的深度解析》 在编程学习与技能提升的过程中,LeetCode作为一款广受欢迎的在线编程挑战平台,帮助众多开发者锻炼算法思维,提高编程能力。而为了进一步提升练习体验...
leetcode-习题集资源leetcode-习题集资源leetcode-习题集资源leetcode-习题集资源leetcode-习题集资源leetcode-习题集资源
leetcode-cli-plugins leetcode-cli 的第 3 方插件。 什么是 如何使用 如何使用 插件 名称 描述 增强的命令 按公司或标签过滤问题 list 不要在同一台计算机上使 Chrome 的会话过期 login 不要在同一台计算机上使 ...
在IDE中解决LeetCode问题,支持leetcode.com与leetcode-cn.com,满足基本的做题需求。 理论上支持: IntelliJ IDEA PhpStorm WebStorm PyCharm RubyMine AppCode CLion GoLand DataGrip Rider MPS Android Studio。
LeetCode Editor 7.4 版本的下载是一个名为 "leetcode-editor" 的压缩包文件。这个压缩包的导入过程非常简单,只需要将它直接拖入 IDEA 界面,IDEA 会自动识别并安装插件。这种方式使得安装过程无需额外的步骤,对于...
leetcode-习题集资源源代码leetcode-习题集资源源代码leetcode-习题集资源源代码leetcode-习题集资源源代码leetcode-习题集资源源代码
~/.vscode/extensions/leetcode.vscode-leetcode-0.17.0/node_modules/vsc-leetcode-cli/bin/leetcode /usr/local/bin/leetcode 修改模板 open ~/.vscode/extensions/leetcode.vscode-leetcode-0.17.0/node_modules/...
解题思路思路和LeetCode-python 503.下一个更大元素 II一致,只是这里求的是下标的距离,而不是数值倒序搜索,用到栈,栈里存储索引情况1:若栈为
leetcode-cheat 的发布 它是什么 ? 这是一个chrome 扩展,可以帮助您更高效地使用 leetcode。您可以从 重要: leetcode-cheat 现在只支持中文版。 也就是说不完全支持leetcode.com,但是你可以用leetcode-cn.com代替...
超过经理收入的员工Employee 表包含所有员工,他们的经理也属于员工。结果如下:select e.Name as Employee from Employe
`swift-Swif-LeetCode-Utils` 是一个实用工具库,它为Swift程序员提供了方便快捷的方法来处理这些问题。这个项目可以帮助你更高效地进行LeetCode上的编程练习,提升你的解决方案的可读性和简洁性。 首先,让我们...
java java_leetcode-115-distinct-subsquences
java java_leetcode-112-path-sum
java java_leetcode-101-symmetric-tree
java java_leetcode-100-same-tree
970. 强整数对数运算function powerfulIntegers(x: number, y: number, bound: number): numb
java java_leetcode-110-balanced-binary-tree
java java_leetcode-73-set-matrix-zeroes
java java_leetcode-113-path-sumII