-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay52.cc
More file actions
95 lines (73 loc) · 2.97 KB
/
Day52.cc
File metadata and controls
95 lines (73 loc) · 2.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/*
The skyline of a city is composed of several buildings of various widths and heights, possibly overlapping one another when viewed from a distance. We can represent the buildings using an array of `(left, right, height)` tuples, which tell us where on an imaginary x-axis a building begins and ends, and how tall it is. The skyline itself can be described by a list of `(x, height)` tuples, giving the locations at which the height visible to a distant observer changes, and each new height.
Given an array of buildings as described above, create a function that returns the skyline.
For example, suppose the input consists of the buildings `[(0, 15, 3), (4, 11, 5), (19, 23, 4)]`. In aggregate, these buildings would create a skyline that looks like the one below.
```
______
| | ___
___| |___ | |
| | B | | | C |
| A | | A | | |
| | | | | |
------------------------
```
As a result, your function should return `[(0, 3), (4, 5), (11, 3), (15, 0), (19, 4), (23, 0)]`.
*/
#include <bits/stdc++.h>
using namespace std;
// Event struct: x-coordinate, height, start/end marker
struct Event {
int x, height;
bool isStart;
Event(int x, int height, bool isStart)
: x(x), height(height), isStart(isStart) {}
};
// Custom sorting for events
bool cmp(const Event& a, const Event& b) {
if (a.x != b.x) return a.x < b.x;
// If same x:
// Start events before end events
if (a.isStart != b.isStart) return a.isStart;
// Among starts: higher height first
if (a.isStart) return a.height > b.height;
// Among ends: lower height first
return a.height < b.height;
}
vector<pair<int, int>> getSkyline(vector<tuple<int, int, int>>& buildings) {
vector<Event> events;
// Convert each building into two events
for (auto& b : buildings) {
int left = get<0>(b), right = get<1>(b), height = get<2>(b);
events.emplace_back(left, height, true); // Start of building
events.emplace_back(right, height, false); // End of building
}
// Sort events
sort(events.begin(), events.end(), cmp);
// Max-heap using multiset to store active building heights
multiset<int> heights = {0}; // Initialize with ground level
int prevMax = 0;
vector<pair<int, int>> result;
for (auto& e : events) {
if (e.isStart) {
heights.insert(e.height);
} else {
heights.erase(heights.find(e.height)); // Erase one instance
}
int currMax = *heights.rbegin(); // Current max height
if (currMax != prevMax) {
result.emplace_back(e.x, currMax);
prevMax = currMax;
}
}
return result;
}
int main() {
vector<tuple<int, int, int>> buildings = {
{0, 15, 3}, {4, 11, 5}, {19, 23, 4}};
vector<pair<int, int>> skyline = getSkyline(buildings);
for (auto& p : skyline) {
cout << "(" << p.first << ", " << p.second << ") ";
}
cout << endl;
return 0;
}