Skip to content

Commit f5ffd19

Browse files
just4oncegitbook-bot
authored andcommitted
GitBook: [master] 3 pages modified
1 parent 4e7d35c commit f5ffd19

File tree

3 files changed

+264
-0
lines changed

3 files changed

+264
-0
lines changed

SUMMARY.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@
5757
* [divide-and-conquer](leetcode/divide-and-conquer/README.md)
5858
* [169-majority-element](leetcode/divide-and-conquer/169-majority-element.md)
5959
* [215-kth-largest-element-in-an-array](leetcode/divide-and-conquer/215-kth-largest-element-in-an-array.md)
60+
* [218-the-skyline-problem](leetcode/divide-and-conquer/218-the-skyline-problem.md)
61+
* [241-different-ways-to-add-parentheses](leetcode/divide-and-conquer/241-different-ways-to-add-parentheses.md)
6062
* [hash-table](leetcode/hash-table/README.md)
6163
* [003-longest-substring-without-repeating-characters](leetcode/hash-table/003-longest-substring-without-repeating-characters.md)
6264
* [030-substring-with-concatenation-of-all-words](leetcode/hash-table/030-substring-with-concatenation-of-all-words.md)
Lines changed: 217 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,217 @@
1+
# 218-the-skyline-problem
2+
3+
## Question {#question}
4+
5+
[https://leetcode.com/problems/the-skyline-problem/description/](https://leetcode.com/problems/the-skyline-problem/description/)
6+
7+
A city's skyline is the outer contour of the silhouette formed by all the buildings in that city when viewed from a distance. Now suppose you are **given the locations and height of all the buildings** as shown on a cityscape photo \(Figure A\), write a program to **output the skyline** formed by these buildings collectively \(Figure B\).[![Buildings](https://leetcode.com/static/images/problemset/skyline1.jpg) ](https://leetcode.com/static/images/problemset/skyline1.jpg)[![Skyline Contour](https://leetcode.com/static/images/problemset/skyline2.jpg)](https://leetcode.com/static/images/problemset/skyline2.jpg)
8+
9+
The geometric information of each building is represented by a triplet of integers `[Li, Ri, Hi]`, where `Li` and `Ri` are the x coordinates of the left and right edge of the ith building, respectively, and `Hi` is its height. It is guaranteed that `0 ≤ Li, Ri ≤ INT_MAX`, `0 < Hi ≤ INT_MAX`, and `Ri - Li > 0`. You may assume all buildings are perfect rectangles grounded on an absolutely flat surface at height 0.
10+
11+
For instance, the dimensions of all buildings in Figure A are recorded as: `[ [2 9 10], [3 7 15], [5 12 12], [15 20 10], [19 24 8] ]` .
12+
13+
The output is a list of "**key points**" \(red dots in Figure B\) in the format of `[ [x1,y1], [x2, y2], [x3, y3], ... ]` that uniquely defines a skyline. **A key point is the left endpoint of a horizontal line segment**. Note that the last key point, where the rightmost building ends, is merely used to mark the termination of the skyline, and always has zero height. Also, the ground in between any two adjacent buildings should be considered part of the skyline contour.
14+
15+
For instance, the skyline in Figure B should be represented as:`[ [2 10], [3 15], [7 12], [12 0], [15 10], [20 8], [24, 0] ]`.
16+
17+
**Notes:**
18+
19+
* The number of buildings in any input list is guaranteed to be in the range `[0, 10000]`.
20+
* The input list is already sorted in ascending order by the left x position `Li`.
21+
* The output list must be sorted by the x position.
22+
* There must be no consecutive horizontal lines of equal height in the output skyline. For instance, `[...[2 3], [4 5], [7 5], [11 5], [12 7]...]` is not acceptable; the three lines of height 5 should be merged into one in the final output as such: `[...[2 3], [4 5], [12 7], ...]`
23+
24+
## Thought Process {#thought-process}
25+
26+
1. PriorityQueue
27+
1. To get the outline of the buildings, we need to go through all the critical points in the chart and check whether we add them base on followings
28+
2. One particular useful data structure to get max or min in log\(n\) times is heap, so we are going to use heap to retrieve max height efficiently
29+
3. We use a list save all the critical points and sort them
30+
1. First based on the left coordinate
31+
2. Second reverse in height, because if the left coordinate is the same, we should use the taller height in outline
32+
4. If the current critical point is a starting point, and we find the currentMaxH != prevH, we can add it to our outlines
33+
5. If the current critical point is a ending point, we need to remove its starting point from our heap, and also compare currentMaxH to prevH, if they are different, we can add it to out outline, otherwise it's shadowed
34+
6. Time complexity O\(n^2\), n from all the points and n from remove it from hepa
35+
7. Space complexity O\(n\)
36+
2. TreeMap
37+
1. To avoid O\(n\) time in removing object, we can use treemap instead to mark the entry where the entry's key is height and value is count
38+
2. We continues decrease the count when we hit the right point and remove when the count = 1
39+
3. Time complexity O\(nlogn\)
40+
4. Space complexity O\(n\)
41+
3. Divide and Conquer
42+
1. We can separate the list into two equal parts, where we find the skyline for left and right and then we merge
43+
2. Merging part is little bit tricky where we have to keep the height of each side
44+
3. When comparing left and right skyline, we can poll the minX point out and then check its height with current height
45+
46+
## Solution
47+
48+
PriorityQueue
49+
50+
```java
51+
class Solution {
52+
private class Point implements Comparable<Point> {
53+
int x, y;
54+
boolean isLeft;
55+
56+
public Point(int x, int y) {
57+
this(x, y, false);
58+
}
59+
60+
public Point(int x, int y, boolean isLeft) {
61+
this.x = x;
62+
this.y = y;
63+
this.isLeft = isLeft;
64+
}
65+
66+
public int compareTo(Point that) {
67+
return this.x == that.x ? that.y - this.y : this.x - that.x;
68+
}
69+
}
70+
71+
public List<int[]> getSkyline(int[][] buildings) {
72+
// identify all the critical points
73+
List<Point> points = new ArrayList<>();
74+
for (int[] building : buildings) {
75+
points.add(new Point(building[0], building[2], true));
76+
points.add(new Point(building[1], building[2]));
77+
}
78+
Collections.sort(points);
79+
PriorityQueue<Integer> maxHeap = new PriorityQueue<>((a, b) -> b - a);
80+
maxHeap.offer(0);
81+
int preH = 0;
82+
List<int[]> res = new ArrayList<>();
83+
for (Point point : points) {
84+
if (point.isLeft) maxHeap.offer(point.y);
85+
else maxHeap.remove(point.y);
86+
int curH = maxHeap.peek();
87+
if (curH != preH) {
88+
res.add(new int[]{point.x, curH});
89+
preH = curH;
90+
}
91+
}
92+
return res;
93+
}
94+
}
95+
```
96+
97+
```java
98+
class Solution {
99+
public List<int[]> getSkyline(int[][] buildings) {
100+
// identify all the critical points
101+
List<int[]> points = new ArrayList<>();
102+
for (int[] building : buildings) {
103+
points.add(new int[]{building[0], -building[2]});
104+
points.add(new int[]{building[1], building[2]});
105+
}
106+
Collections.sort(points, (a, b) -> a[0] == b[0] ? a[1] - b[1] : a[0] - b[0]);
107+
PriorityQueue<Integer> maxHeap = new PriorityQueue<>((a, b) -> b - a);
108+
maxHeap.offer(0);
109+
int preH = 0;
110+
List<int[]> res = new ArrayList<>();
111+
for (int[] point : points) {
112+
if (point[1] < 0) maxHeap.offer(-point[1]);
113+
else maxHeap.remove(point[1]);
114+
int curH = maxHeap.peek();
115+
if (curH != preH) {
116+
res.add(new int[]{point[0], curH});
117+
preH = curH;
118+
}
119+
}
120+
return res;
121+
}
122+
}
123+
```
124+
125+
2. PriorityQueue with TreeMap
126+
127+
```java
128+
class Solution {
129+
public List<int[]> getSkyline(int[][] buildings) {
130+
// identify all the critical points
131+
List<int[]> points = new ArrayList<>();
132+
for (int[] building : buildings) {
133+
points.add(new int[]{building[0], -building[2]});
134+
points.add(new int[]{building[1], building[2]});
135+
}
136+
// sort it base on the left point else by the reverse height
137+
Collections.sort(points, (a, b) -> a[0] == b[0] ? a[1] - b[1] : a[0] - b[0]);
138+
// use treemap can avoid the O(n) time cost on heap, we can remove
139+
// the item when height count = 1
140+
TreeMap<Integer, Integer> treeMap = new TreeMap<>(Collections.reverseOrder());
141+
treeMap.put(0, 1);
142+
int preH = 0;
143+
List<int[]> res = new ArrayList<>();
144+
// For every point, we need to get the maxCurH to compare to preH
145+
// If there are different, this means we can add the point to the
146+
// outline
147+
for (int[] point : points) {
148+
if (point[1] < 0) treeMap.put(-point[1], treeMap.getOrDefault(-point[1], 0) + 1);
149+
else if (treeMap.get(point[1]) == 1) treeMap.remove(point[1]);
150+
else treeMap.put(point[1], treeMap.get(point[1]) - 1);
151+
int curH = treeMap.firstKey();
152+
if (curH != preH) {
153+
res.add(new int[]{point[0], curH});
154+
preH = curH;
155+
}
156+
}
157+
return res;
158+
}
159+
}
160+
```
161+
162+
3. Divide and Conquer
163+
164+
```java
165+
class Solution {
166+
public List<int[]> getSkyline(int[][] buildings) {
167+
// we can use divide and conquer approach as well
168+
if (buildings.length == 0) return new LinkedList<int[]>();
169+
return search(buildings, 0, buildings.length - 1);
170+
}
171+
172+
private LinkedList<int[]> search(int[][] buildings, int lo, int hi) {
173+
if (lo < hi) {
174+
int mi = lo + (hi - lo) / 2;
175+
return merge(search(buildings, lo, mi), search(buildings, mi + 1, hi));
176+
} else {
177+
LinkedList<int[]> res = new LinkedList<>();
178+
res.add(new int[]{buildings[lo][0], buildings[lo][2]});
179+
res.add(new int[]{buildings[lo][1], 0});
180+
return res;
181+
}
182+
}
183+
184+
private LinkedList<int[]> merge(LinkedList<int[]> left, LinkedList<int[]> right) {
185+
LinkedList<int[]> res = new LinkedList<>();
186+
int h1 = 0, h2 = 0;
187+
// Merging is little bit tricky, we need two separate variables to
188+
// Check the height add appropiately two shadowed buildings
189+
190+
// h1 and h2 each track the building height on left and right respectively
191+
// Now when a shadowed building comes in, our h will guard and prevent the
192+
// building being added.
193+
while (!left.isEmpty() && !right.isEmpty()) {
194+
int x = 0, h = 0;
195+
if (left.peek()[0] < right.peek()[0]) {
196+
x = left.peek()[0];
197+
h1 = left.poll()[1];
198+
} else if (right.peek()[0] < left.peek()[0]) {
199+
x = right.peek()[0];
200+
h2 = right.poll()[1];
201+
} else {
202+
x = left.peek()[0];
203+
h1 = left.poll()[1];
204+
h2 = right.poll()[1];
205+
}
206+
h = Math.max(h1, h2);
207+
if (res.isEmpty() || h != res.getLast()[1]) res.add(new int[]{x, h});
208+
}
209+
res.addAll(left);
210+
res.addAll(right);
211+
return res;
212+
}
213+
}
214+
```
215+
216+
## Additional {#additional}
217+
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# 241-different-ways-to-add-parentheses
2+
3+
## Question {#question}
4+
5+
Given a string of numbers and operators, return all possible results from computing all the different possible ways to group numbers and operators. The valid operators are `+`, `-` and `*`.
6+
7+
**Example 1:**
8+
9+
```text
10+
Input: "2-1-1"
11+
Output: [0, 2]
12+
Explanation:
13+
((2-1)-1) = 0
14+
(2-(1-1)) = 2
15+
```
16+
17+
**Example 2:**
18+
19+
```text
20+
Input: "2*3-4*5"
21+
Output: [-34, -14, -10, -10, 10]
22+
Explanation:
23+
(2*(3-(4*5))) = -34
24+
((2*3)-(4*5)) = -14
25+
((2*(3-4))*5) = -10
26+
(2*((3-4)*5)) = -10
27+
(((2*3)-4)*5) = 10
28+
```
29+
30+
## Thought Process {#thought-process}
31+
32+
1. Divide and Conquer
33+
1. Divide the input string into two different parts every time we encounter a operator
34+
2. From the return list, we can add the corresponding value based on operators to our list
35+
3. Time complexity O\(3^n\)
36+
4. Space complexity O\(3^n\)
37+
2.
38+
## Solution
39+
40+
```java
41+
42+
```
43+
44+
## Additional {#additional}
45+

0 commit comments

Comments
 (0)