Skip to content

Commit 1da0130

Browse files
committed
maxHeap
1 parent 67d30fa commit 1da0130

15 files changed

Lines changed: 1274 additions & 1199 deletions

Java/HashHeap.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
H
2+
1532965421
3+
tags: HashHeap
24

35
非题.是从九章找来的HashHeap implementation.
46

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,25 @@
44

55
又叫做skyline. 用Sweep Line做的O(nLogN), 但是貌似还有很多做法: segement tree, hashheap, treeSet?
66

7-
#### 方法1: Sweep Line, Time O(nLogN), Space O(n)
8-
original reference http://codechen.blogspot.com/2015/06/leetcode-skyline-problem.html?_sm_au_=isVmHvFmFs40TWRt
9-
10-
sweep line:
7+
#### Sweep Line, Time O(nLogN), Space O(n)
8+
- original reference http://codechen.blogspot.com/2015/06/leetcode-skyline-problem.html?_sm_au_=isVmHvFmFs40TWRt
9+
- 画图分析: 需要找到 non-overlaping height point at current index; also height needs to be different than prev height peek to be visible.
1110
- 把所有点分出来每个点有index x, 再加上一个height.
1211
- 在这个list上排序根据index和height. 注意用负数标记building start point height, 这样保证start在end 之前
1312
- 用负数的height标记start: 在priority queue里面同一个x-pos比较 startPoint.height, endPoint.height 的时候, 因为end height是整数, 所以compare时会自动把start point放在end point前面
1413
- 当然了, 如果两个 start point比较, 第二个point的负数超大的话(也就是height很高), 就会顺理compare return正数, 成章形成倒位
1514
- 在processs时候用max-heap (reversed priorityqueue),再iterate heightPoints 来存最大的height . 遇到peek,就是一个合理的解
1615
- heightQueue里面加一个0, 用来在结尾的时候做closure
1716

18-
#### 方法2: Segment Tree
19-
REVIEW
17+
#### Segment Tree
18+
- 看了一些做法, segment tree写法很复杂, 估计在面试中难以用segment tree来写: https://www.cnblogs.com/tiezhibieek/p/5021202.html
19+
20+
#### HashHeap
21+
- HashHeap template 可以考虑: https://www.jiuzhang.com/solution/building-outline/#tag-highlight-lang-java
2022

2123
Binary Indexed Tree?
2224

23-
HashHeap?
25+
2426

2527
```
2628
/*
@@ -97,11 +99,12 @@ public int compare(Point a, Point b) {
9799
queue.offer(new Point(buildings[i][1], buildings[i][2]));
98100
}
99101

100-
// Mark height and calcualte the outline point.
102+
// Mark and store height in maxHeap: start and end with ground point height=0
101103
PriorityQueue<Integer> maxHeightQueue = new PriorityQueue<>(Collections.reverseOrder());
102104
maxHeightQueue.offer(0);
103105
int prevPeak = maxHeightQueue.peek();
104106

107+
// Find non-overlaping height. Positive height marks end of building outline, so remove from maxHeap.
105108
while (!queue.isEmpty()) {
106109
Point point = queue.poll();
107110
if (point.height < 0) {
@@ -110,6 +113,7 @@ public int compare(Point a, Point b) {
110113
maxHeightQueue.remove(point.height);
111114
}
112115

116+
// Goal: find non-overlapping height
113117
int currPeak = maxHeightQueue.peek();
114118
if (currPeak != prevPeak) {
115119
rst.add(new int[]{point.pos, currPeak});

LevelReviewPage.md

Lines changed: 92 additions & 89 deletions
Large diffs are not rendered by default.

README.md

Lines changed: 457 additions & 457 deletions
Large diffs are not rendered by default.

ReviewPage.md

Lines changed: 474 additions & 471 deletions
Large diffs are not rendered by default.

TagREADME.md

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ Table of Contents
5858
* [Matrix DFS (2)](#matrix-dfs-2)
5959
* [Queue (2)](#queue-2)
6060
* [Geometry (2)](#geometry-2)
61+
* [HashHeap (1)](#hashheap-1)
6162
* [KMP (1)](#kmp-1)
6263
* [Brainteaser (1)](#brainteaser-1)
6364
* [Interval (1)](#interval-1)
@@ -663,7 +664,7 @@ Table of Contents
663664
|9|[Unique Binary Search Tree II.java](https://github.com/awangdev/LintCode/blob/master/Java/Unique%20Binary%20Search%20Tree%20II.java)|Medium|Java|[BST, DP, Divide and Conquer, Tree]||
664665
|10|[Count of Range Sum.java](https://github.com/awangdev/LintCode/blob/master/Java/Count%20of%20Range%20Sum.java)|Hard|Java|[BST, Divide and Conquer, Merge Sort, PreSum]||
665666
|11|[Expression Add Operators.java](https://github.com/awangdev/LintCode/blob/master/Java/Expression%20Add%20Operators.java)|Hard|Java|[Backtracking, DFS, Divide and Conquer, String]||
666-
|12|[Building Outline.java](https://github.com/awangdev/LintCode/blob/master/Java/Building%20Outline.java)|Review|Java|[Binary Indexed Tree, Divide and Conquer, Heap, Segment Tree, Sweep Line]||
667+
|12|[The Skyline Problem.java](https://github.com/awangdev/LintCode/blob/master/Java/The%20Skyline%20Problem.java)|Review|Java|[Binary Indexed Tree, Divide and Conquer, Heap, Segment Tree, Sweep Line]||
667668
|13|[Find Peak Element II.java](https://github.com/awangdev/LintCode/blob/master/Java/Find%20Peak%20Element%20II.java)|Hard|Java|[Binary Search, DFS, Divide and Conquer]||
668669
|14|[Search a 2D Matrix II.java](https://github.com/awangdev/LintCode/blob/master/Java/Search%20a%202D%20Matrix%20II.java)|Medium|Java|[Binary Search, Divide and Conquer]||
669670
|15|[Interval Minimum Number.java](https://github.com/awangdev/LintCode/blob/master/Java/Interval%20Minimum%20Number.java)|Medium|Java|[Binary Search, Divide and Conquer, Lint, Segment Tree]||
@@ -1085,7 +1086,7 @@ Table of Contents
10851086
| Squence | Problem | Level | Language | Tags | Video Tutorial|
10861087
|:-------:|:--------------|:------:|:---------:|:----:|:-------------:|
10871088
|0|[Trapping Rain Water II.java](https://github.com/awangdev/LintCode/blob/master/Java/Trapping%20Rain%20Water%20II.java)|Hard|Java|[BFS, Heap]||
1088-
|1|[Building Outline.java](https://github.com/awangdev/LintCode/blob/master/Java/Building%20Outline.java)|Review|Java|[Binary Indexed Tree, Divide and Conquer, Heap, Segment Tree, Sweep Line]||
1089+
|1|[The Skyline Problem.java](https://github.com/awangdev/LintCode/blob/master/Java/The%20Skyline%20Problem.java)|Review|Java|[Binary Indexed Tree, Divide and Conquer, Heap, Segment Tree, Sweep Line]||
10891090
|2|[Kth Smallest Number in Sorted Matrix.java](https://github.com/awangdev/LintCode/blob/master/Java/Kth%20Smallest%20Number%20in%20Sorted%20Matrix.java)|Medium|Java|[Binary Search, Heap]||
10901091
|3|[Ugly Number II.java](https://github.com/awangdev/LintCode/blob/master/Java/Ugly%20Number%20II.java)|Medium|Java|[DP, Heap, Math]||
10911092
|4|[Data Stream Median.java](https://github.com/awangdev/LintCode/blob/master/Java/Data%20Stream%20Median.java)|Hard|Java|[Design, Heap]||
@@ -1173,7 +1174,7 @@ Table of Contents
11731174
| Squence | Problem | Level | Language | Tags | Video Tutorial|
11741175
|:-------:|:--------------|:------:|:---------:|:----:|:-------------:|
11751176
|0|[Count of Smaller Numbers After Self.java](https://github.com/awangdev/LintCode/blob/master/Java/Count%20of%20Smaller%20Numbers%20After%20Self.java)|Hard|Java|[BST, Binary Indexed Tree, Binary Search, Divide and Conquer, Segment Tree]||
1176-
|1|[Building Outline.java](https://github.com/awangdev/LintCode/blob/master/Java/Building%20Outline.java)|Review|Java|[Binary Indexed Tree, Divide and Conquer, Heap, Segment Tree, Sweep Line]||
1177+
|1|[The Skyline Problem.java](https://github.com/awangdev/LintCode/blob/master/Java/The%20Skyline%20Problem.java)|Review|Java|[Binary Indexed Tree, Divide and Conquer, Heap, Segment Tree, Sweep Line]||
11771178
|2|[Interval Minimum Number.java](https://github.com/awangdev/LintCode/blob/master/Java/Interval%20Minimum%20Number.java)|Medium|Java|[Binary Search, Divide and Conquer, Lint, Segment Tree]||
11781179
|3|[Interval Sum.java](https://github.com/awangdev/LintCode/blob/master/Java/Interval%20Sum.java)|Medium|Java|[Binary Search, Lint, Segment Tree]||
11791180
|4|[Interval Sum II.java](https://github.com/awangdev/LintCode/blob/master/Java/Interval%20Sum%20II.java)|Hard|Java|[Binary Search, Lint, Segment Tree]||
@@ -1382,7 +1383,7 @@ Table of Contents
13821383
|:-------:|:--------------|:------:|:---------:|:----:|:-------------:|
13831384
|0|[Number of Airplane in the sky.java](https://github.com/awangdev/LintCode/blob/master/Java/Number%20of%20Airplane%20in%20the%20sky.java)|Medium|Java|[Array, Interval, PriorityQueue, Sort, Sweep Line]||
13841385
|1|[Merge Intervals.java](https://github.com/awangdev/LintCode/blob/master/Java/Merge%20Intervals.java)|Medium|Java|[Array, PriorityQueue, Sort, Sweep Line]||
1385-
|2|[Building Outline.java](https://github.com/awangdev/LintCode/blob/master/Java/Building%20Outline.java)|Review|Java|[Binary Indexed Tree, Divide and Conquer, Heap, Segment Tree, Sweep Line]||
1386+
|2|[The Skyline Problem.java](https://github.com/awangdev/LintCode/blob/master/Java/The%20Skyline%20Problem.java)|Review|Java|[Binary Indexed Tree, Divide and Conquer, Heap, Segment Tree, Sweep Line]||
13861387
|3|[Meeting Rooms II.java](https://github.com/awangdev/LintCode/blob/master/Java/Meeting%20Rooms%20II.java)|Medium|Java|[Greedy, Heap, Sort, Sweep Line]||
13871388
|4|[Meeting Rooms.java](https://github.com/awangdev/LintCode/blob/master/Java/Meeting%20Rooms.java)|Easy|Java|[PriorityQueue, Sort, Sweep Line]||
13881389

@@ -1568,7 +1569,7 @@ Table of Contents
15681569
| Squence | Problem | Level | Language | Tags | Video Tutorial|
15691570
|:-------:|:--------------|:------:|:---------:|:----:|:-------------:|
15701571
|0|[Count of Smaller Numbers After Self.java](https://github.com/awangdev/LintCode/blob/master/Java/Count%20of%20Smaller%20Numbers%20After%20Self.java)|Hard|Java|[BST, Binary Indexed Tree, Binary Search, Divide and Conquer, Segment Tree]||
1571-
|1|[Building Outline.java](https://github.com/awangdev/LintCode/blob/master/Java/Building%20Outline.java)|Review|Java|[Binary Indexed Tree, Divide and Conquer, Heap, Segment Tree, Sweep Line]||
1572+
|1|[The Skyline Problem.java](https://github.com/awangdev/LintCode/blob/master/Java/The%20Skyline%20Problem.java)|Review|Java|[Binary Indexed Tree, Divide and Conquer, Heap, Segment Tree, Sweep Line]||
15721573

15731574

15741575

@@ -1608,6 +1609,16 @@ Table of Contents
16081609

16091610

16101611

1612+
## HashHeap (1)
1613+
| Squence | Problem | Level | Language | Tags | Video Tutorial|
1614+
|:-------:|:--------------|:------:|:---------:|:----:|:-------------:|
1615+
|0|[HashHeap.java](https://github.com/awangdev/LintCode/blob/master/Java/HashHeap.java)|Hard|Java|[HashHeap]||
1616+
1617+
1618+
1619+
1620+
1621+
16111622
## KMP (1)
16121623
| Squence | Problem | Level | Language | Tags | Video Tutorial|
16131624
|:-------:|:--------------|:------:|:---------:|:----:|:-------------:|

0 commit comments

Comments
 (0)