Skip to content

Commit 4fa3e4d

Browse files
committed
feat: add solutions to lc problem: No.1942
No.1942.The Number of the Smallest Unoccupied Chair
1 parent b4f169b commit 4fa3e4d

File tree

6 files changed

+231
-3
lines changed

6 files changed

+231
-3
lines changed

Diff for: solution/0500-0599/0522.Longest Uncommon Subsequence II/README.md

+5-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,11 @@
4646

4747
<!-- 这里可写通用的实现逻辑 -->
4848

49-
获取**非其他字符串子序列**的字符串的最大长度。若不存在,返回 -1。
49+
**方法一:判断子序列**
50+
51+
判断是否独有,只需要取字符串 $s$ 本身,与其他字符串比较即可。题目可以转化为:获取**非其他字符串子序列**的字符串的最大长度。若不存在,返回 -1。
52+
53+
其中,$check(a,b)$ 用于判断字符串 $b$ 是否为字符串 $a$ 的子序列。
5054

5155
<!-- tabs:start -->
5256

Diff for: solution/1900-1999/1942.The Number of the Smallest Unoccupied Chair/README.md

+78-1
Original file line numberDiff line numberDiff line change
@@ -64,22 +64,99 @@
6464

6565
<!-- 这里可写通用的实现逻辑 -->
6666

67+
**方法一:优先队列(最小堆)**
68+
6769
<!-- tabs:start -->
6870

6971
### **Python3**
7072

7173
<!-- 这里可写当前语言的特殊实现逻辑 -->
7274

7375
```python
74-
76+
class Solution:
77+
def smallestChair(self, times: List[List[int]], targetFriend: int) -> int:
78+
n = len(times)
79+
h = list(range(n))
80+
heapify(h)
81+
for i in range(n):
82+
times[i].append(i)
83+
times.sort()
84+
busy = []
85+
for a, b, i in times:
86+
while busy and busy[0][0] <= a:
87+
heappush(h, heappop(busy)[1])
88+
c = heappop(h)
89+
if i == targetFriend:
90+
return c
91+
heappush(busy, (b, c))
92+
return -1
7593
```
7694

7795
### **Java**
7896

7997
<!-- 这里可写当前语言的特殊实现逻辑 -->
8098

8199
```java
100+
class Solution {
101+
public int smallestChair(int[][] times, int targetFriend) {
102+
int n = times.length;
103+
int[][] ts = new int[n][3];
104+
PriorityQueue<Integer> q = new PriorityQueue<>();
105+
PriorityQueue<int[]> busy = new PriorityQueue<>((a, b) -> a[0] - b[0]);
106+
for (int i = 0; i < n; ++i) {
107+
ts[i] = new int[]{times[i][0], times[i][1], i};
108+
q.offer(i);
109+
}
110+
Arrays.sort(ts, (a, b) -> a[0] - b[0]);
111+
for (int[] t : ts) {
112+
int a = t[0], b = t[1], i = t[2];
113+
while (!busy.isEmpty() && busy.peek()[0] <= a) {
114+
q.offer(busy.poll()[1]);
115+
}
116+
int c = q.poll();
117+
if (i == targetFriend) {
118+
return c;
119+
}
120+
busy.offer(new int[]{b, c});
121+
}
122+
return -1;
123+
}
124+
}
125+
```
82126

127+
### **C++**
128+
129+
```cpp
130+
using pii = pair<int, int>;
131+
132+
class Solution {
133+
public:
134+
int smallestChair(vector<vector<int>>& times, int targetFriend) {
135+
priority_queue<int, vector<int>, greater<int>> q;
136+
priority_queue<pii, vector<pii>, greater<pii>> busy;
137+
int n = times.size();
138+
for (int i = 0; i < n; ++i)
139+
{
140+
times[i].push_back(i);
141+
q.push(i);
142+
}
143+
sort(times.begin(), times.end());
144+
for (auto& t : times)
145+
{
146+
int a = t[0], b = t[1], i = t[2];
147+
while (!busy.empty() && busy.top().first <= a)
148+
{
149+
q.push(busy.top().second);
150+
busy.pop();
151+
}
152+
int c = q.top();
153+
q.pop();
154+
if (i == targetFriend) return c;
155+
busy.push({b, c});
156+
}
157+
return -1;
158+
}
159+
};
83160
```
84161
85162
### **...**

Diff for: solution/1900-1999/1942.The Number of the Smallest Unoccupied Chair/README_EN.md

+76-1
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,88 @@ Since friend 0 sat on chair 2, we return 2.
6565
### **Python3**
6666

6767
```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
6985
```
7086

7187
### **Java**
7288

7389
```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+
```
74116

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+
};
75150
```
76151
77152
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
using pii = pair<int, int>;
2+
3+
class Solution {
4+
public:
5+
int smallestChair(vector<vector<int>>& times, int targetFriend) {
6+
priority_queue<int, vector<int>, greater<int>> q;
7+
priority_queue<pii, vector<pii>, greater<pii>> busy;
8+
int n = times.size();
9+
for (int i = 0; i < n; ++i)
10+
{
11+
times[i].push_back(i);
12+
q.push(i);
13+
}
14+
sort(times.begin(), times.end());
15+
for (auto& t : times)
16+
{
17+
int a = t[0], b = t[1], i = t[2];
18+
while (!busy.empty() && busy.top().first <= a)
19+
{
20+
q.push(busy.top().second);
21+
busy.pop();
22+
}
23+
int c = q.top();
24+
q.pop();
25+
if (i == targetFriend) return c;
26+
busy.push({b, c});
27+
}
28+
return -1;
29+
}
30+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class Solution {
2+
public int smallestChair(int[][] times, int targetFriend) {
3+
int n = times.length;
4+
int[][] ts = new int[n][3];
5+
PriorityQueue<Integer> q = new PriorityQueue<>();
6+
PriorityQueue<int[]> busy = new PriorityQueue<>((a, b) -> a[0] - b[0]);
7+
for (int i = 0; i < n; ++i) {
8+
ts[i] = new int[]{times[i][0], times[i][1], i};
9+
q.offer(i);
10+
}
11+
Arrays.sort(ts, (a, b) -> a[0] - b[0]);
12+
for (int[] t : ts) {
13+
int a = t[0], b = t[1], i = t[2];
14+
while (!busy.isEmpty() && busy.peek()[0] <= a) {
15+
q.offer(busy.poll()[1]);
16+
}
17+
int c = q.poll();
18+
if (i == targetFriend) {
19+
return c;
20+
}
21+
busy.offer(new int[]{b, c});
22+
}
23+
return -1;
24+
}
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution:
2+
def smallestChair(self, times: List[List[int]], targetFriend: int) -> int:
3+
n = len(times)
4+
h = list(range(n))
5+
heapify(h)
6+
for i in range(n):
7+
times[i].append(i)
8+
times.sort()
9+
busy = []
10+
for a, b, i in times:
11+
while busy and busy[0][0] <= a:
12+
heappush(h, heappop(busy)[1])
13+
c = heappop(h)
14+
if i == targetFriend:
15+
return c
16+
heappush(busy, (b, c))
17+
return -1

0 commit comments

Comments
 (0)