@@ -65,13 +65,88 @@ Since friend 0 sat on chair 2, we return 2.
65
65
### ** Python3**
66
66
67
67
``` python
68
-
68
+ class Solution :
69
+ def smallestChair (self , times : List[List[int ]], targetFriend : int ) -> int :
70
+ n = len (times)
71
+ h = list (range (n))
72
+ heapify(h)
73
+ for i in range (n):
74
+ times[i].append(i)
75
+ times.sort()
76
+ busy = []
77
+ for a, b, i in times:
78
+ while busy and busy[0 ][0 ] <= a:
79
+ heappush(h, heappop(busy)[1 ])
80
+ c = heappop(h)
81
+ if i == targetFriend:
82
+ return c
83
+ heappush(busy, (b, c))
84
+ return - 1
69
85
```
70
86
71
87
### ** Java**
72
88
73
89
``` java
90
+ class Solution {
91
+ public int smallestChair (int [][] times , int targetFriend ) {
92
+ int n = times. length;
93
+ int [][] ts = new int [n][3 ];
94
+ PriorityQueue<Integer > q = new PriorityQueue<> ();
95
+ PriorityQueue<int[]> busy = new PriorityQueue<> ((a, b) - > a[0 ] - b[0 ]);
96
+ for (int i = 0 ; i < n; ++ i) {
97
+ ts[i] = new int []{times[i][0 ], times[i][1 ], i};
98
+ q. offer(i);
99
+ }
100
+ Arrays . sort(ts, (a, b) - > a[0 ] - b[0 ]);
101
+ for (int [] t : ts) {
102
+ int a = t[0 ], b = t[1 ], i = t[2 ];
103
+ while (! busy. isEmpty() && busy. peek()[0 ] <= a) {
104
+ q. offer(busy. poll()[1 ]);
105
+ }
106
+ int c = q. poll();
107
+ if (i == targetFriend) {
108
+ return c;
109
+ }
110
+ busy. offer(new int []{b, c});
111
+ }
112
+ return - 1 ;
113
+ }
114
+ }
115
+ ```
74
116
117
+ ### ** C++**
118
+
119
+ ``` cpp
120
+ using pii = pair<int , int >;
121
+
122
+ class Solution {
123
+ public:
124
+ int smallestChair(vector<vector<int >>& times, int targetFriend) {
125
+ priority_queue<int, vector<int >, greater<int >> q;
126
+ priority_queue<pii, vector<pii >, greater<pii >> busy;
127
+ int n = times.size();
128
+ for (int i = 0; i < n; ++i)
129
+ {
130
+ times[ i] .push_back(i);
131
+ q.push(i);
132
+ }
133
+ sort(times.begin(), times.end());
134
+ for (auto& t : times)
135
+ {
136
+ int a = t[ 0] , b = t[ 1] , i = t[ 2] ;
137
+ while (!busy.empty() && busy.top().first <= a)
138
+ {
139
+ q.push(busy.top().second);
140
+ busy.pop();
141
+ }
142
+ int c = q.top();
143
+ q.pop();
144
+ if (i == targetFriend) return c;
145
+ busy.push({b, c});
146
+ }
147
+ return -1;
148
+ }
149
+ };
75
150
```
76
151
77
152
### **...**
0 commit comments