Skip to content

Commit 84a9448

Browse files
committed
218 codes and comments updated.
1 parent 4b2a413 commit 84a9448

File tree

1 file changed

+21
-24
lines changed
  • 0218-The-Skyline-Problem/cpp-0218

1 file changed

+21
-24
lines changed

0218-The-Skyline-Problem/cpp-0218/main.cpp

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/// Source : https://leetcode.com/problems/the-skyline-problem/description/
22
/// Author : liuyubobobo
33
/// Time : 2017-10-28
4+
/// updated: 2019-03-14
45

56
#include <iostream>
67
#include <vector>
@@ -10,24 +11,18 @@
1011

1112
using namespace std;
1213

14+
15+
/// Using Segment Tree
16+
/// Time Complexity: O(nlogn)
17+
/// Space Complexity: O(n)
1318
class SegmentTree{
1419

1520
private:
1621
int n;
17-
vector<int> tree;
18-
vector<int> lazy;
22+
vector<int> tree, lazy;
1923

2024
public:
21-
SegmentTree(int n){
22-
23-
this->n = n;
24-
25-
int size = 4*n;
26-
for(int i = 0 ; i < size ; i ++){
27-
tree.push_back(0);
28-
lazy.push_back(0);
29-
}
30-
}
25+
SegmentTree(int n): n(n), tree(4 * n, 0), lazy(4 * n, 0){}
3126

3227
void add(int l, int r, int h){
3328
update(0, 0, n-1, l, r, h);
@@ -41,19 +36,19 @@ class SegmentTree{
4136
void update(int treeID, int treeL, int treeR, int l, int r, int h){
4237

4338
if(lazy[treeID] != 0){
44-
tree[treeID] = max(tree[treeID], lazy[treeID]);
39+
tree[treeID] = max(tree[treeID], lazy[treeID]); // max
4540
if(treeL != treeR){
46-
lazy[2 * treeID + 1] = max(lazy[treeID], lazy[2 * treeID + 1]);
47-
lazy[2 * treeID + 2] = max(lazy[treeID], lazy[2 * treeID + 2]);
41+
lazy[2 * treeID + 1] = max(lazy[treeID], lazy[2 * treeID + 1]); // max
42+
lazy[2 * treeID + 2] = max(lazy[treeID], lazy[2 * treeID + 2]); // max
4843
}
4944
lazy[treeID] = 0;
5045
}
5146

5247
if(treeL == l && treeR == r){
53-
tree[treeID] = max(tree[treeID], h);
48+
tree[treeID] = max(tree[treeID], h); // max
5449
if(treeL != treeR){
55-
lazy[2 * treeID + 1] = max(h, lazy[2 * treeID + 1]);
56-
lazy[2 * treeID + 2] = max(h, lazy[2 * treeID + 2]);
50+
lazy[2 * treeID + 1] = max(h, lazy[2 * treeID + 1]); // max
51+
lazy[2 * treeID + 2] = max(h, lazy[2 * treeID + 2]); // max
5752
}
5853
return;
5954
}
@@ -75,10 +70,10 @@ class SegmentTree{
7570
int query(int treeID, int treeL, int treeR, int index){
7671

7772
if(lazy[treeID] != 0){
78-
tree[treeID] = max(tree[treeID], lazy[treeID]);
73+
tree[treeID] = max(tree[treeID], lazy[treeID]); // max
7974
if(treeL != treeR){
80-
lazy[2 * treeID + 1] = max(lazy[treeID], lazy[2 * treeID + 1]);
81-
lazy[2 * treeID + 2] = max(lazy[treeID], lazy[2 * treeID + 2]);
75+
lazy[2 * treeID + 1] = max(lazy[treeID], lazy[2 * treeID + 1]); // max
76+
lazy[2 * treeID + 2] = max(lazy[treeID], lazy[2 * treeID + 2]); // max
8277
}
8378
lazy[treeID] = 0;
8479
}
@@ -97,13 +92,14 @@ class SegmentTree{
9792
}
9893
};
9994

95+
10096
class Solution {
10197
public:
10298
vector<pair<int, int>> getSkyline(vector<vector<int>>& buildings) {
10399

104100
// Coordinate compression
105101
set<int> unique_points;
106-
for(vector<int> info: buildings){
102+
for(const vector<int>& info: buildings){
107103
unique_points.insert(info[0]);
108104
unique_points.insert(info[1]-1);
109105
unique_points.insert(info[1]);
@@ -118,13 +114,13 @@ class Solution {
118114

119115
// segment tree
120116
SegmentTree stree(pos.size());
121-
for(vector<int> info: buildings)
117+
for(const vector<int>& info: buildings)
122118
stree.add(indexes[info[0]], indexes[info[1]-1], info[2]);
123119

124120
// get results
125121
vector<pair<int, int>> res;
126122
unique_points.clear();
127-
for(vector<int> info: buildings){
123+
for(const vector<int>& info: buildings){
128124
unique_points.insert(info[0]);
129125
unique_points.insert(info[1]);
130126
}
@@ -141,6 +137,7 @@ class Solution {
141137
}
142138
};
143139

140+
144141
int main() {
145142

146143
int n = 5;

0 commit comments

Comments
 (0)