Skip to content

Commit 3037fac

Browse files
committed
+ problem 1847
1 parent 6fe5c7a commit 3037fac

File tree

5 files changed

+183
-0
lines changed

5 files changed

+183
-0
lines changed
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# 1847. Closest Room
2+
There is a hotel with `n` rooms. The rooms are represented by a 2D integer array `rooms` where <code>rooms[i] = [roomId<sub>i</sub>, size<sub>i</sub>]</code> denotes that there is a room with room number <code>roomId<sub>i</sub></code> and size equal to <code>size<sub>i</sub></code>. Each <code>roomId<sub>i</sub></code> is guaranteed to be **unique**.
3+
4+
You are also given `k` queries in a 2D array `queries` where <code>queries[j] = [preferred<sub>j</sub>, minSize<sub>j</sub>]</code>. The answer to the <code>j<sup>th</sup></code> query is the room number `id` of a room such that:
5+
6+
* The room has a size of **at least** <code>minSize<sub>j</sub></code>, and
7+
* <code>abs(id - preferred<sub>j</sub>)</code> is **minimized**, where `abs(x)` is the absolute value of x.
8+
9+
If there is a **tie** in the absolute difference, then use the room with the **smallest** such `id`. If there is **no such room**, the answer is `-1`.
10+
11+
Return *an array* `answer` *of length* `k` *where* `answer[j]` *contains the answer to the* <code>j<sup>th</sup></code> *query*.
12+
13+
#### Example 1:
14+
<pre>
15+
<strong>Input:</strong> rooms = [[2,2],[1,2],[3,2]], queries = [[3,1],[3,3],[5,2]]
16+
<strong>Output:</strong> [3,-1,3]
17+
<strong>Explanation:</strong> The answers to the queries are as follows:
18+
Query = [3,1]: Room number 3 is the closest as abs(3 - 3) = 0, and its size of 2 is at least 1. The answer is 3.
19+
Query = [3,3]: There are no rooms with a size of at least 3, so the answer is -1.
20+
Query = [5,2]: Room number 3 is the closest as abs(3 - 5) = 2, and its size of 2 is at least 2. The answer is 3.
21+
</pre>
22+
23+
#### Example 2:
24+
<pre>
25+
<strong>Input:</strong> rooms = [[1,4],[2,3],[3,5],[4,1],[5,2]], queries = [[2,3],[2,4],[2,5]]
26+
<strong>Output:</strong> [2,1,3]
27+
<strong>Explanation:</strong> The answers to the queries are as follows:
28+
Query = [2,3]: Room number 2 is the closest as abs(2 - 2) = 0, and its size of 3 is at least 3. The answer is 2.
29+
Query = [2,4]: Room numbers 1 and 3 both have sizes of at least 4. The answer is 1 since it is smaller.
30+
Query = [2,5]: Room number 3 is the only room with a size of at least 5. The answer is 3.
31+
</pre>
32+
33+
#### Constraints:
34+
* `n == rooms.length`
35+
* <code>1 <= n <= 10<sup>5</sup></code>
36+
* `k == queries.length`
37+
* <code>1 <= k <= 10<sup>4</sup></code>
38+
* <code>1 <= roomId<sub>i</sub>, preferred<sub>j</sub> <= 10<sup>7</sup></code>
39+
* <code>1 <= size<sub>i</sub>, minSize<sub>j</sub> <= 10<sup>7</sup></code>
40+
41+
## Solutions (Python)
42+
43+
### 1. Solution
44+
```Python
45+
from sortedcontainers import SortedList
46+
47+
48+
class Solution:
49+
def closestRoom(self, rooms: List[List[int]], queries: List[List[int]]) -> List[int]:
50+
queries = list(enumerate(queries))
51+
roomids = SortedList()
52+
answer = [-1] * len(queries)
53+
54+
rooms.sort(key=lambda x: x[1])
55+
queries.sort(key=lambda x: x[1][1], reverse=True)
56+
57+
for (j, [prefered, minsize]) in queries:
58+
while len(rooms) > 0 and rooms[-1][1] >= minsize:
59+
roomids.add(rooms.pop()[0])
60+
if len(roomids) == 0:
61+
continue
62+
63+
i = roomids.bisect_left(prefered)
64+
if i == 0:
65+
answer[j] = roomids[0]
66+
elif i == len(roomids):
67+
answer[j] = roomids[-1]
68+
elif abs(roomids[i] - prefered) < abs(roomids[i - 1] - prefered):
69+
answer[j] = roomids[i]
70+
else:
71+
answer[j] = roomids[i - 1]
72+
73+
return answer
74+
```
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# 1847. 最近的房间
2+
一个酒店里有 `n` 个房间,这些房间用二维整数数组 `rooms` 表示,其中 <code>rooms[i] = [roomId<sub>i</sub>, size<sub>i</sub>]</code> 表示有一个房间号为 <code>roomId<sub>i</sub></code> 的房间且它的面积为 <code>size<sub>i</sub></code> 。每一个房间号 <code>roomId<sub>i</sub></code> 保证是 **独一无二** 的。
3+
4+
同时给你 `k` 个查询,用二维数组 `queries` 表示,其中 <code>queries[j] = [preferred<sub>j</sub>, minSize<sub>j</sub>]</code> 。第 `j` 个查询的答案是满足如下条件的房间 `id`
5+
6+
* 房间的面积 **至少** 为 <code>minSize<sub>j</sub></code> ,且
7+
* <code>abs(id - preferred<sub>j</sub>)</code> 的值 **最小** ,其中 `abs(x)``x` 的绝对值。
8+
9+
如果差的绝对值有 **相等** 的,选择 **最小**`id` 。如果 **没有满足条件的房间** ,答案为 `-1`
10+
11+
请你返回长度为 `k` 的数组 `answer` ,其中 `answer[j]` 为第 `j` 个查询的结果。
12+
13+
#### 示例 1:
14+
<pre>
15+
<strong>输入:</strong> rooms = [[2,2],[1,2],[3,2]], queries = [[3,1],[3,3],[5,2]]
16+
<strong>输出:</strong> [3,-1,3]
17+
<strong>解释:</strong> 查询的答案如下:
18+
查询 [3,1] :房间 3 的面积为 2 ,大于等于 1 ,且号码是最接近 3 的,为 abs(3 - 3) = 0 ,所以答案为 3 。
19+
查询 [3,3] :没有房间的面积至少为 3 ,所以答案为 -1 。
20+
查询 [5,2] :房间 3 的面积为 2 ,大于等于 2 ,且号码是最接近 5 的,为 abs(3 - 5) = 2 ,所以答案为 3 。
21+
</pre>
22+
23+
#### 示例 2:
24+
<pre>
25+
<strong>输入:</strong> rooms = [[1,4],[2,3],[3,5],[4,1],[5,2]], queries = [[2,3],[2,4],[2,5]]
26+
<strong>输出:</strong> [2,1,3]
27+
<strong>解释:</strong> 查询的答案如下:
28+
查询 [2,3] :房间 2 的面积为 3 ,大于等于 3 ,且号码是最接近的,为 abs(2 - 2) = 0 ,所以答案为 2 。
29+
查询 [2,4] :房间 1 和 3 的面积都至少为 4 ,答案为 1 因为它房间编号更小。
30+
查询 [2,5] :房间 3 是唯一面积大于等于 5 的,所以答案为 3 。
31+
</pre>
32+
33+
#### 提示:
34+
* `n == rooms.length`
35+
* <code>1 <= n <= 10<sup>5</sup></code>
36+
* `k == queries.length`
37+
* <code>1 <= k <= 10<sup>4</sup></code>
38+
* <code>1 <= roomId<sub>i</sub>, preferred<sub>j</sub> <= 10<sup>7</sup></code>
39+
* <code>1 <= size<sub>i</sub>, minSize<sub>j</sub> <= 10<sup>7</sup></code>
40+
41+
## 题解 (Python)
42+
43+
### 1. 题解
44+
```Python
45+
from sortedcontainers import SortedList
46+
47+
48+
class Solution:
49+
def closestRoom(self, rooms: List[List[int]], queries: List[List[int]]) -> List[int]:
50+
queries = list(enumerate(queries))
51+
roomids = SortedList()
52+
answer = [-1] * len(queries)
53+
54+
rooms.sort(key=lambda x: x[1])
55+
queries.sort(key=lambda x: x[1][1], reverse=True)
56+
57+
for (j, [prefered, minsize]) in queries:
58+
while len(rooms) > 0 and rooms[-1][1] >= minsize:
59+
roomids.add(rooms.pop()[0])
60+
if len(roomids) == 0:
61+
continue
62+
63+
i = roomids.bisect_left(prefered)
64+
if i == 0:
65+
answer[j] = roomids[0]
66+
elif i == len(roomids):
67+
answer[j] = roomids[-1]
68+
elif abs(roomids[i] - prefered) < abs(roomids[i - 1] - prefered):
69+
answer[j] = roomids[i]
70+
else:
71+
answer[j] = roomids[i - 1]
72+
73+
return answer
74+
```
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
from sortedcontainers import SortedList
2+
3+
4+
class Solution:
5+
def closestRoom(self, rooms: List[List[int]], queries: List[List[int]]) -> List[int]:
6+
queries = list(enumerate(queries))
7+
roomids = SortedList()
8+
answer = [-1] * len(queries)
9+
10+
rooms.sort(key=lambda x: x[1])
11+
queries.sort(key=lambda x: x[1][1], reverse=True)
12+
13+
for (j, [prefered, minsize]) in queries:
14+
while len(rooms) > 0 and rooms[-1][1] >= minsize:
15+
roomids.add(rooms.pop()[0])
16+
if len(roomids) == 0:
17+
continue
18+
19+
i = roomids.bisect_left(prefered)
20+
if i == 0:
21+
answer[j] = roomids[0]
22+
elif i == len(roomids):
23+
answer[j] = roomids[-1]
24+
elif abs(roomids[i] - prefered) < abs(roomids[i - 1] - prefered):
25+
answer[j] = roomids[i]
26+
else:
27+
answer[j] = roomids[i - 1]
28+
29+
return answer

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -950,6 +950,7 @@
950950
[1844][1844l]|[Replace All Digits with Characters][1844] |![rs]
951951
[1845][1845l]|[Seat Reservation Manager][1845] |![py]
952952
[1846][1846l]|[Maximum Element After Decreasing and Rearranging][1846] |![rs]
953+
[1847][1847l]|[Closest Room][1847] |![py]
953954
[1848][1848l]|[Minimum Distance to the Target Element][1848] |![rs]
954955
[1854][1854l]|[Maximum Population Year][1854] |![rs]
955956
[1855][1855l]|[Maximum Distance Between a Pair of Values][1855] |![py]
@@ -2232,6 +2233,7 @@
22322233
[1844]:Problemset/1844-Replace%20All%20Digits%20with%20Characters/README.md#1844-replace-all-digits-with-characters
22332234
[1845]:Problemset/1845-Seat%20Reservation%20Manager/README.md#1845-seat-reservation-manager
22342235
[1846]:Problemset/1846-Maximum%20Element%20After%20Decreasing%20and%20Rearranging/README.md#1846-maximum-element-after-decreasing-and-rearranging
2236+
[1847]:Problemset/1847-Closest%20Room/README.md#1847-closest-room
22352237
[1848]:Problemset/1848-Minimum%20Distance%20to%20the%20Target%20Element/README.md#1848-minimum-distance-to-the-target-element
22362238
[1854]:Problemset/1854-Maximum%20Population%20Year/README.md#1854-maximum-population-year
22372239
[1855]:Problemset/1855-Maximum%20Distance%20Between%20a%20Pair%20of%20Values/README.md#1855-maximum-distance-between-a-pair-of-values
@@ -3517,6 +3519,7 @@
35173519
[1844l]:https://leetcode.com/problems/replace-all-digits-with-characters/
35183520
[1845l]:https://leetcode.com/problems/seat-reservation-manager/
35193521
[1846l]:https://leetcode.com/problems/maximum-element-after-decreasing-and-rearranging/
3522+
[1847l]:https://leetcode.com/problems/closest-room/
35203523
[1848l]:https://leetcode.com/problems/minimum-distance-to-the-target-element/
35213524
[1854l]:https://leetcode.com/problems/maximum-population-year/
35223525
[1855l]:https://leetcode.com/problems/maximum-distance-between-a-pair-of-values/

README_CN.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -950,6 +950,7 @@
950950
[1844][1844l]|[将所有数字用字符替换][1844] |![rs]
951951
[1845][1845l]|[座位预约管理系统][1845] |![py]
952952
[1846][1846l]|[减小和重新排列数组后的最大元素][1846] |![rs]
953+
[1847][1847l]|[最近的房间][1847] |![py]
953954
[1848][1848l]|[到目标元素的最小距离][1848] |![rs]
954955
[1854][1854l]|[人口最多的年份][1854] |![rs]
955956
[1855][1855l]|[下标对中的最大距离][1855] |![py]
@@ -2232,6 +2233,7 @@
22322233
[1844]:Problemset/1844-Replace%20All%20Digits%20with%20Characters/README_CN.md#1844-将所有数字用字符替换
22332234
[1845]:Problemset/1845-Seat%20Reservation%20Manager/README_CN.md#1845-座位预约管理系统
22342235
[1846]:Problemset/1846-Maximum%20Element%20After%20Decreasing%20and%20Rearranging/README_CN.md#1846-减小和重新排列数组后的最大元素
2236+
[1847]:Problemset/1847-Closest%20Room/README_CN.md#1847-最近的房间
22352237
[1848]:Problemset/1848-Minimum%20Distance%20to%20the%20Target%20Element/README_CN.md#1848-到目标元素的最小距离
22362238
[1854]:Problemset/1854-Maximum%20Population%20Year/README_CN.md#1854-人口最多的年份
22372239
[1855]:Problemset/1855-Maximum%20Distance%20Between%20a%20Pair%20of%20Values/README_CN.md#1855-下标对中的最大距离
@@ -3517,6 +3519,7 @@
35173519
[1844l]:https://leetcode.cn/problems/replace-all-digits-with-character/
35183520
[1845l]:https://leetcode.cn/problems/seat-reservation-manager/
35193521
[1846l]:https://leetcode.cn/problems/maximum-element-after-decreasing-and-rearranging/
3522+
[1847l]:https://leetcode.cn/problems/closest-room/
35203523
[1848l]:https://leetcode.cn/problems/minimum-distance-to-the-target-element/
35213524
[1854l]:https://leetcode.cn/problems/maximum-population-year/
35223525
[1855l]:https://leetcode.cn/problems/maximum-distance-between-a-pair-of-values/

0 commit comments

Comments
 (0)