Skip to content

Commit ad15031

Browse files
committed
Implement the-skyline-problem
name: the-skyline-problem url: https://leetcode.com/problems/the-skyline-problem difficulty: 3 time: -1 ms time-rank: 0.0 % time-complexity: O(inf) space: -1.0 MB space-rank: 0.0 % space-complexity: O(inf) Fixes #324 This is a brute-force solution. The obvious limitation is memory. Time is O(MN) or O(N) where M is the average width of each building. Really horrible solution. I think that the proper one involves interval trees, which I really know little about. I could certainly draw the interval graph itself, and finding the starting point and ending point is obviously easy. The first two examples pass, but obviously [[0,2147483647,2147483647]] throws an allocation error. Signed-off-by: Christopher Friedt <[email protected]>
1 parent 0385511 commit ad15031

File tree

2 files changed

+117
-0
lines changed

2 files changed

+117
-0
lines changed

the-skyline-problem-test.cpp

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* Copyright (c) 2021, Christopher Friedt
3+
*
4+
* SPDX-License-Identifier: MIT
5+
*/
6+
7+
#include <gtest/gtest.h>
8+
9+
#include "the-skyline-problem.cpp"
10+
11+
using namespace std;
12+
13+
TEST(TheSkylineProblem, 2_9_10__3_7_15__5_12_12__15_20_10__19_24_8) {
14+
vector<vector<int>> buildings = {
15+
{2, 9, 10}, {3, 7, 15}, {5, 12, 12}, {15, 20, 10}, {19, 24, 8}};
16+
vector<vector<int>> expected = {{2, 10}, {3, 15}, {7, 12}, {12, 0},
17+
{15, 10}, {20, 8}, {24, 0}};
18+
EXPECT_EQ(expected, Solution().getSkyline(buildings));
19+
}
20+
21+
TEST(TheSkylineProblem, 0_2_3__2_5_3) {
22+
vector<vector<int>> buildings = {{0, 2, 3}, {2, 5, 3}};
23+
vector<vector<int>> expected = {{0, 3}, {5, 0}};
24+
EXPECT_EQ(expected, Solution().getSkyline(buildings));
25+
}
26+
27+
#if 0
28+
TEST(TheSkylineProblem, 0_2147483647_2147483647) {
29+
vector<vector<int>> buildings = {{0, 2147483647, 2147483647}};
30+
vector<vector<int>> expected = {{2147483647, 0}};
31+
EXPECT_EQ(expected, Solution().getSkyline(buildings));
32+
}
33+
#endif

the-skyline-problem.cpp

+84
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/*
2+
* Copyright (c) 2021, Christopher Friedt
3+
*
4+
* SPDX-License-Identifier: MIT
5+
*/
6+
7+
// clang-format off
8+
// name: the-skyline-problem
9+
// url: https://leetcode.com/problems/the-skyline-problem
10+
// difficulty: 3
11+
// clang-format on
12+
13+
#include <vector>
14+
15+
using namespace std;
16+
17+
enum {
18+
LEFT,
19+
RIGHT,
20+
HEIGHT,
21+
};
22+
23+
enum {
24+
X,
25+
Y,
26+
};
27+
28+
template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) {
29+
os << "[";
30+
for (int i = 0, N = v.size(); i < N; ++i) {
31+
os << v[i];
32+
if (i < N - 1) {
33+
os << ",";
34+
}
35+
}
36+
os << "]";
37+
return os;
38+
}
39+
40+
class Solution {
41+
public:
42+
vector<vector<int>> getSkyline(vector<vector<int>> &B) {
43+
vector<vector<int>> S;
44+
int xmin = B[0][LEFT];
45+
int xmax = xmin;
46+
47+
for (auto &b : B) {
48+
xmax = max(xmax, b[RIGHT]);
49+
}
50+
51+
vector<int> h(xmax - xmin + 1, 0);
52+
for (auto &b : B) {
53+
for (auto x = b[LEFT]; x <= b[RIGHT]; ++x) {
54+
h[x - xmin] = max(h[x - xmin], b[HEIGHT]);
55+
}
56+
}
57+
58+
// cout << "h: " << h << endl;
59+
60+
S.push_back(vector<int>{xmin, h[0]});
61+
for (int x = xmin + 1; x <= xmax; ++x) {
62+
auto &h_curr = h[x - xmin];
63+
auto &h_prev = h[x - xmin - 1];
64+
65+
if (h_curr == h_prev) {
66+
continue;
67+
}
68+
69+
if (h_curr > h_prev) {
70+
S.push_back(vector<int>{x, h_curr});
71+
} else {
72+
S.push_back(vector<int>{x - 1, h_curr});
73+
}
74+
75+
// cout << "S: " << S << endl;
76+
}
77+
78+
S.push_back(vector<int>{xmax, 0});
79+
80+
// cout << "S: " << S << endl;
81+
82+
return S;
83+
}
84+
};

0 commit comments

Comments
 (0)