-
Notifications
You must be signed in to change notification settings - Fork 119
/
Copy path0218-TheSkylineProblem.cs
165 lines (137 loc) · 4.64 KB
/
0218-TheSkylineProblem.cs
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
//-----------------------------------------------------------------------------
// Runtime: 288ms
// Memory Usage: 35.6 MB
// Link: https://leetcode.com/submissions/detail/263059422/
//-----------------------------------------------------------------------------
using System;
using System.Collections.Generic;
namespace LeetCode
{
public class _0218_TheSkylineProblem
{
public IList<IList<int>> GetSkyline(int[][] buildings)
{
var sortedList = new SortedList<int, IList<int>>();
foreach (var building in buildings)
{
if (!sortedList.ContainsKey(building[0]))
sortedList.Add(building[0], new List<int>());
sortedList[building[0]].Add(-building[2]);
if (!sortedList.ContainsKey(building[1]))
sortedList.Add(building[1], new List<int>());
sortedList[building[1]].Add(building[2]);
}
var result = new List<IList<int>>();
var heights = new MaxPriorityQueue();
foreach (var x in sortedList.Keys)
{
foreach (var height in sortedList[x])
{
if (height < 0) heights.Insert(-height);
else heights.Delete(height);
}
if (heights.Size() == 0) result.Add(new List<int>() { x, 0 });
else
{
var height = heights.Max();
if (result.Count == 0 || result[result.Count - 1][1] != height)
result.Add(new List<int>() { x, height });
}
}
return result;
}
public class MaxPriorityQueue : PriorityQueue
{
public MaxPriorityQueue() : base() { }
public MaxPriorityQueue(int initCapacity) : base(initCapacity) { }
protected override void Sink(int k)
{
while (2 * k <= N)
{
int j = 2 * k;
if (j < N && pq[j] < pq[j + 1]) j++;
if (pq[k] >= pq[j]) break;
Swap(k, j);
k = j;
}
}
protected override void Swim(int k)
{
while (k > 1 && pq[k / 2] < pq[k])
{
Swap(k / 2, k);
k = k / 2;
}
}
public int Max() => First();
public int DeleteMax() => Delete();
}
public abstract class PriorityQueue
{
protected int N;
protected int[] pq;
public PriorityQueue() : this(1) { }
public PriorityQueue(int initCapacity)
{
this.N = 0;
pq = new int[initCapacity + 1];
}
public bool IsEmpty() => N == 0;
public int Size() => N;
public int First()
{
if (IsEmpty()) { throw new ArgumentOutOfRangeException(); }
return pq[1];
}
public void Insert(int x)
{
if (N >= pq.Length - 1)
Resize(pq.Length * 2);
pq[++N] = x;
Swim(N);
}
protected abstract void Swim(int k);
public int Delete()
{
var result = pq[1];
Swap(1, N--);
Sink(1);
pq[N + 1] = 0;
if (N > 0 && N == (pq.Length - 1) / 4)
Resize(pq.Length / 2);
return result;
}
public void Delete(int x)
{
var index = -1;
for (int i = 1; i <= N; i++)
{
if (pq[i] == x)
{
index = i;
break;
}
}
Swap(index, N--);
Sink(index);
pq[N + 1] = 0;
if (N > 0 && N == (pq.Length - 1) / 4)
Resize(pq.Length / 2);
}
protected abstract void Sink(int k);
private void Resize(int newCapacity)
{
var temp = new int[newCapacity + 1];
for (int i = 1; i <= N; i++)
temp[i] = pq[i];
pq = temp;
}
protected void Swap(int index1, int index2)
{
var temp = pq[index1];
pq[index1] = pq[index2];
pq[index2] = temp;
}
}
}
}