Problem solution · Python

Find Building Where Alice and Bob Can Meet

Find Building Where Alice and Bob Can Meet: a Python solution using binary search. Learn the idea, check the complexity, and read the full code, with credit to walkccc LeetCode Solutions.

Technique
Binary search
Source
walkccc LeetCode Solutions
Length
70 lines
Start with the idea.

Try the problem first. If you get stuck, read the approach below, then write your own solution. The full code is at the bottom.

Approach

Binary search

For Find Building Where Alice and Bob Can Meet, the implementation exploits a monotonic condition to discard half of the remaining search space after every check.

  1. Identify the ordered answer range or sorted search domain.
  2. Write a predicate whose truth changes only once.
  3. Move the appropriate boundary after each midpoint check and return the final feasible position.

Code notes

  • 70 lines of Python from the credited upstream file 2940.py.
  • The implementation visibly relies on sequence storage.
  • No explicit loop blocks detected.

Complexity

Multiply the logarithmic number of midpoint checks by the cost of one predicate evaluation.

Check the problem constraints before deciding whether this complexity will pass.

Source

Code and credit

This code comes from walkccc LeetCode Solutions by P.-Y. Chen (walkccc) and is used under the MIT licence.

Full codeFind Building Where Alice and Bob Can Meet · PythonPython
Use this to learn the idea, then write your own version.
from dataclasses import dataclass  @dataclassclass IndexedQuery:  queryIndex: int  a: int  # Alice's index  b: int  # Bob's index   def __iter__(self):    yield self.queryIndex    yield self.a    yield self.b  class Solution:  # Similar to 2736. Maximum Sum Queries  def leftmostBuildingQueries(      self,      heights: list[int],      queries: list[list[int]],  ) -> list[int]:    ans = [-1] * len(queries)    # Store indices (heightsIndex) of heights with heights[heightsIndex] in    # descending order.    stack = []     # Iterate through queries and heights simultaneously.    heightsIndex = len(heights) - 1    for queryIndex, a, b in sorted([IndexedQuery(i, min(a, b), max(a, b))                                    for i, (a, b) in enumerate(queries)],                                   key=lambda x: -x.b):      if a == b or heights[a] < heights[b]:        # 1. Alice and Bob are already in the same index (a == b) or        # 2. Alice can jump from a -> b (heights[a] < heights[b]).        ans[queryIndex] = b      else:        # Now, a < b and heights[a] >= heights[b].        # Gradually add heights with an index > b to the monotonic stack.        while heightsIndex > b:          # heights[heightsIndex] is a better candidate, given that          # heightsIndex is smaller than the indices in the stack and          # heights[heightsIndex] is larger or equal to the heights mapped in          # the stack.          while stack and heights[stack[-1]] <= heights[heightsIndex]:            stack.pop()          stack.append(heightsIndex)          heightsIndex -= 1        # Binary search to find the smallest index j such that j > b and        # heights[j] > heights[a], thereby ensuring heights[j] > heights[b].        j = self._lastGreater(stack, a, heights)        if j != -1:          ans[queryIndex] = stack[j]     return ans   def _lastGreater(self, A: list[int], target: int, heights: list[int]):    """    Returns the last index i in A s.t. heights[A.get(i)] is > heights[target].    """    l = -1    r = len(A) - 1    while l < r:      m = (l + r + 1) // 2      if heights[A[m]] > heights[target]:        l = m      else:        r = m - 1    return l 

Did this explanation save you time? I'm a Grade 11 student building this free library to make difficult algorithms easier to understand.

Buy me a coffee ↗