Skip to content

Commit

Permalink
Dec 22
Browse files Browse the repository at this point in the history
  • Loading branch information
siddydutta committed Dec 22, 2024
1 parent 3e91083 commit 083d1b7
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
from heapq import heappush, heappop
from typing import List


class Solution:
def leftmostBuildingQueries(self, heights: List[int], queries: List[List[int]]) -> List[int]:
max_idx = []
results = [-1] * len(queries)
store_queries = [[] for _ in heights]

for idx, (a, b) in enumerate(queries):
if a < b and heights[a] < heights[b]:
results[idx] = b
elif a > b and heights[a] > heights[b]:
results[idx] = a
elif a == b:
results[idx] = a
else:
store_queries[max(a, b)].append(
(max(heights[a], heights[b]), idx)
)

for idx, height in enumerate(heights):
while max_idx and max_idx[0][0] < height:
_, q_idx = heappop(max_idx)
results[q_idx] = idx
for element in store_queries[idx]:
heappush(max_idx, element)

return results


def main():
heights = [6, 4, 8, 5, 2, 7]
queries = [[0, 1], [0, 3], [2, 4], [3, 4], [2, 2]]
assert Solution().leftmostBuildingQueries(heights, queries) == [2, 5, -1, 5, 2]

heights = [5, 3, 8, 2, 6, 1, 4, 6]
queries = [[0, 7], [3, 5], [5, 2], [3, 0], [1, 6]]
assert Solution().leftmostBuildingQueries(heights, queries) == [7, 6, -1, 4, 6]


if __name__ == '__main__':
main()
4 changes: 2 additions & 2 deletions 2024-12-December-LeetCoding-Challenge/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
| December 19 | [769. Max Chunks To Make Sorted](https://leetcode.com/problems/max-chunks-to-make-sorted/) | Medium | Unsolved |
| December 20 | [2415. Reverse Odd Levels of Binary Tree](https://leetcode.com/problems/reverse-odd-levels-of-binary-tree/) | Medium | Solved |
| December 21 | [2872. Maximum Number of K-Divisible Components](https://leetcode.com/problems/maximum-number-of-k-divisible-components/) | Hard | Unsolved |
| December 22 | []() | | |
| 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 |
| December 23 | []() | | |
| December 24 | []() | | |
| December 25 | []() | | |
Expand All @@ -40,4 +40,4 @@
| --- | --- | --- | --- |
| Easy | 5 | 5 | 0 |
| Medium | 15 | 9 | 6 |
| Hard | 1 | 0 | 1 |
| Hard | 2 | 0 | 2 |

0 comments on commit 083d1b7

Please sign in to comment.