-
Notifications
You must be signed in to change notification settings - Fork 8
/
1942 - The Number of the Smallest Unoccupied Chair.cpp
56 lines (51 loc) · 1.33 KB
/
1942 - The Number of the Smallest Unoccupied Chair.cpp
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
#include <bits/stdc++.h>
using namespace std;
// Priority Queue Similar to 218.Skyline Problem
// Leetcode Discuss Answer: https://leetcode.com/problems/the-number-of-the-smallest-unoccupied-chair/discuss/1359912/Priority-Queue-or-C%2B%2B-or-Concise-Code-or-Skyline-Problem
class Solution
{
public:
int smallestChair(vector<vector<int>> ×, int targetFriend)
{
int idx = 0;
vector<pair<int, int>> timeline;
for (vector<int> &time : times)
{
timeline.push_back({time[0], (idx + 1)});
timeline.push_back({time[1], -(idx + 1)});
idx++;
}
sort(timeline.begin(), timeline.end());
int prevLow = 0;
unordered_map<int, int> cache;
priority_queue<int, vector<int>, greater<int>> pq;
for (pair<int, int> time : timeline)
{
idx = abs(time.second) - 1; // getting the correct idx position
if (time.second > 0)
{
int a = pq.empty() ? prevLow : pq.top(); // if pq iks empty then a = prevLow
int b = prevLow;
int seat = 0;
if (a < b)
{
seat = a;
pq.pop();
}
else
{
seat = b;
prevLow++;
}
cache.insert({idx, seat});
if (targetFriend == idx)
return seat;
}
else
{
pq.push(cache[idx]);
}
}
return -1;
}
};