Skip to content

Commit 083d1b7

Browse files
committed
Dec 22
1 parent 3e91083 commit 083d1b7

File tree

2 files changed

+46
-2
lines changed

2 files changed

+46
-2
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
from heapq import heappush, heappop
2+
from typing import List
3+
4+
5+
class Solution:
6+
def leftmostBuildingQueries(self, heights: List[int], queries: List[List[int]]) -> List[int]:
7+
max_idx = []
8+
results = [-1] * len(queries)
9+
store_queries = [[] for _ in heights]
10+
11+
for idx, (a, b) in enumerate(queries):
12+
if a < b and heights[a] < heights[b]:
13+
results[idx] = b
14+
elif a > b and heights[a] > heights[b]:
15+
results[idx] = a
16+
elif a == b:
17+
results[idx] = a
18+
else:
19+
store_queries[max(a, b)].append(
20+
(max(heights[a], heights[b]), idx)
21+
)
22+
23+
for idx, height in enumerate(heights):
24+
while max_idx and max_idx[0][0] < height:
25+
_, q_idx = heappop(max_idx)
26+
results[q_idx] = idx
27+
for element in store_queries[idx]:
28+
heappush(max_idx, element)
29+
30+
return results
31+
32+
33+
def main():
34+
heights = [6, 4, 8, 5, 2, 7]
35+
queries = [[0, 1], [0, 3], [2, 4], [3, 4], [2, 2]]
36+
assert Solution().leftmostBuildingQueries(heights, queries) == [2, 5, -1, 5, 2]
37+
38+
heights = [5, 3, 8, 2, 6, 1, 4, 6]
39+
queries = [[0, 7], [3, 5], [5, 2], [3, 0], [1, 6]]
40+
assert Solution().leftmostBuildingQueries(heights, queries) == [7, 6, -1, 4, 6]
41+
42+
43+
if __name__ == '__main__':
44+
main()

2024-12-December-LeetCoding-Challenge/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
| December 19 | [769. Max Chunks To Make Sorted](https://leetcode.com/problems/max-chunks-to-make-sorted/) | Medium | Unsolved |
2525
| December 20 | [2415. Reverse Odd Levels of Binary Tree](https://leetcode.com/problems/reverse-odd-levels-of-binary-tree/) | Medium | Solved |
2626
| December 21 | [2872. Maximum Number of K-Divisible Components](https://leetcode.com/problems/maximum-number-of-k-divisible-components/) | Hard | Unsolved |
27-
| December 22 | []() | | |
27+
| December 22 | [2940. Find Building Where Alice and Bob Can Meet](https://leetcode.com/problems/find-building-where-alice-and-bob-can-meet/) | Hard | Unsolved |
2828
| December 23 | []() | | |
2929
| December 24 | []() | | |
3030
| December 25 | []() | | |
@@ -40,4 +40,4 @@
4040
| --- | --- | --- | --- |
4141
| Easy | 5 | 5 | 0 |
4242
| Medium | 15 | 9 | 6 |
43-
| Hard | 1 | 0 | 1 |
43+
| Hard | 2 | 0 | 2 |

0 commit comments

Comments
 (0)