Problem solution · C++

Find Building Where Alice and Bob Can Meet

Find Building Where Alice and Bob Can Meet: a C++ 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
65 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

  • 65 lines of C++ from the credited upstream file 2940.cpp.
  • The implementation visibly relies on sequence storage.
  • 4 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 · C++C++
Use this to learn the idea, then write your own version.
struct IndexedQuery {  int queryIndex;  int a;  // Alice's index  int b;  // Bob's index}; class Solution { public:  // Similar to 2736. Maximum Sum Queries  vector<int> leftmostBuildingQueries(vector<int>& heights,                                      vector<vector<int>>& queries) {    vector<int> ans(queries.size(), -1);    // Store indices (heightsIndex) of heights with heights[heightsIndex] in    // descending order.    vector<int> stack;     // Iterate through queries and heights simultaneously.    int heightsIndex = heights.size() - 1;    for (const auto& [queryIndex, a, b] : getIndexedQueries(queries)) {      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.empty() &&                 heights[stack.back()] <= heights[heightsIndex])            stack.pop_back();          stack.push_back(heightsIndex--);        }        // Binary search to find the smallest index j such that j > b and        // heights[j] > heights[a], thereby ensuring heights[j] > heights[b].        if (const auto it = upper_bound(                stack.rbegin(), stack.rend(), a,                [&](int a, int b) { return heights[a] < heights[b]; });            it != stack.rend())          ans[queryIndex] = *it;      }    }     return ans;  }  private:  vector<IndexedQuery> getIndexedQueries(const vector<vector<int>>& queries) {    vector<IndexedQuery> indexedQueries;    for (int i = 0; i < queries.size(); ++i) {      // Make sure that a <= b.      const int a = min(queries[i][0], queries[i][1]);      const int b = max(queries[i][0], queries[i][1]);      indexedQueries.push_back({i, a, b});    }    ranges::sort(        indexedQueries,        [](const IndexedQuery& a, const IndexedQuery& b) { return a.b > b.b; });    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 ↗