Problem solution · Java

Find Building Where Alice and Bob Can Meet

Find Building Where Alice and Bob Can Meet: a Java 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
72 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

  • 72 lines of Java from the credited upstream file 2940.java.
  • The implementation visibly relies on sequence storage.
  • 5 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 · JavaJava
Use this to learn the idea, then write your own version.
class Solution {  // Similar to 2736. Maximum Sum Queries  public int[] leftmostBuildingQueries(int[] heights, int[][] queries) {    IndexedQuery[] indexedQueries = getIndexedQueries(queries);    int[] ans = new int[queries.length];    Arrays.fill(ans, -1);    // Store indices (heightsIndex) of heights with heights[heightsIndex] in    // descending order.    List<Integer> stack = new ArrayList<>();     // Iterate through queries and heights simultaneously.    int heightsIndex = heights.length - 1;    for (IndexedQuery indexedQuery : indexedQueries) {      final int queryIndex = indexedQuery.queryIndex;      final int a = indexedQuery.a;      final int b = indexedQuery.b;      if (a == b || 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.isEmpty() && heights[stack.get(stack.size() - 1)] <= heights[heightsIndex])            stack.remove(stack.size() - 1);          stack.add(heightsIndex--);        }        // Binary search to find the smallest index j such that j > b and        // heights[j] > heights[a], thereby ensuring heights[t] > heights[b].        final int j = lastGreater(stack, a, heights);        if (j != -1)          ans[queryIndex] = stack.get(j);      }    }     return ans;  }   private record IndexedQuery(int queryIndex, int a, int b){};   // Returns the last index i in A s.t. heights[A.get(i)] is > heights[target].  private int lastGreater(List<Integer> A, int target, int[] heights) {    int l = -1;    int r = A.size() - 1;    while (l < r) {      final int m = (l + r + 1) / 2;      if (heights[A.get(m)] > heights[target])        l = m;      else        r = m - 1;    }    return l;  }   private IndexedQuery[] getIndexedQueries(int[][] queries) {    IndexedQuery[] indexedQueries = new IndexedQuery[queries.length];    for (int i = 0; i < queries.length; ++i) {      // Make sure that a <= b.      final int a = Math.min(queries[i][0], queries[i][1]);      final int b = Math.max(queries[i][0], queries[i][1]);      indexedQueries[i] = new IndexedQuery(i, a, b);    }    Arrays.sort(indexedQueries, Comparator.comparing(IndexedQuery::b, Comparator.reverseOrder()));    return indexedQueries;  }} 

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 ↗