Imagine a futuristic city where people can only move between buildings by jumping from one to another, but there's a catch - you can only jump to a taller building that's positioned to your right!

You're given an array heights where heights[i] represents the height of the i-th building. A person in building i can move to building j if and only if:

  • i < j (building j is to the right)
  • heights[i] < heights[j] (building j is taller)

You have multiple queries where Alice starts at building a[i] and Bob starts at building b[i]. For each query, find the leftmost building where both Alice and Bob can meet. If they can't meet anywhere, return -1.

The key challenge: Both Alice and Bob must be able to reach the same building following the jumping rules!

Input & Output

example_1.py โ€” Basic Meeting Scenario
$ Input: heights = [6,4,8,5,2,7], queries = [[0,1],[0,3],[2,4],[3,4],[2,2]]
โ€บ Output: [2,5,-1,-1,2]
๐Ÿ’ก Note: Query [0,1]: Alice at building 0 (height 6), Bob at building 1 (height 4). Both can reach building 2 (height 8). Query [0,3]: Alice at 0 (height 6), Bob at 3 (height 5). Both can reach building 5 (height 7). Query [2,4]: Alice at 2 (height 8), Bob at 4 (height 2). No building to the right is taller than 8. Query [3,4]: Alice at 3 (height 5), Bob at 4 (height 2). No valid meeting building. Query [2,2]: Alice and Bob both at building 2, so answer is 2.
example_2.py โ€” Direct Jump Possible
$ Input: heights = [5,3,8,2,6,1,4,6], queries = [[0,7],[3,5],[5,2]]
โ€บ Output: [7,5,-1]
๐Ÿ’ก Note: Query [0,7]: Alice at 0 (height 5), Bob at 7 (height 6). Alice can jump directly to Bob since 5 < 6. Query [3,5]: Alice at 3 (height 2), Bob at 5 (height 1). Alice can reach Bob at building 5. Query [5,2]: Alice at 5 (height 1), Bob at 2 (height 8). Since Alice is to the right of Bob but can't reach any building taller than Bob's, answer is -1.
example_3.py โ€” No Valid Meeting Point
$ Input: heights = [1,2,1,2,1,2], queries = [[0,0],[0,1],[0,2],[0,3],[0,4],[0,5]]
โ€บ Output: [0,1,3,3,5,5]
๐Ÿ’ก Note: Query [0,0]: Same building, answer is 0. Query [0,1]: Alice at 0 (height 1), Bob at 1 (height 2). Alice can jump to Bob. Query [0,2]: Both need to reach a building taller than max(1,1)=1, which is building 3. Query [0,3]: Alice can jump to building 3 directly. Similar logic for remaining queries.

Visualization

Tap to expand
AliceHeight: 5BobHeight: 8BuildingHeight: 12Height: 9Meeting!Height: 15Building Jump Rulesโœ… Valid Jumps:โ€ข Jump to taller building (right side)โ€ข Building index must be greaterโ€ข Both players follow same rulesโŒ Invalid Jumps:โ€ข Jump to shorter buildingโ€ข Jump backwards (left direction)Optimal Algorithm:1. Use monotonic decreasing stack2. Binary search for meeting points3. Process queries efficiently: O((n+m)log n)AB๐Ÿค
Understanding the Visualization
1
Starting Positions
Alice and Bob begin at their respective buildings with specific heights
2
Jump Constraints
They can only jump to buildings that are taller and positioned to the right
3
Finding Meeting Point
Search for the leftmost building both can reach following the jump rules
4
Optimal Processing
Use monotonic stack to efficiently process multiple queries
Key Takeaway
๐ŸŽฏ Key Insight: The problem transforms into finding the leftmost 'next greater element' that satisfies both players' constraints. Using a monotonic stack with binary search achieves optimal O((n+m)log n) complexity!

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n * m)

For each of m queries, we potentially check all n buildings

n
2n
โœ“ Linear Growth
Space Complexity
O(1)

Only using constant extra space for variables

n
2n
โœ“ Linear Space

Constraints

  • 1 โ‰ค heights.length โ‰ค 5 ร— 104
  • 1 โ‰ค heights[i] โ‰ค 109
  • 1 โ‰ค queries.length โ‰ค 5 ร— 104
  • queries[i] = [ai, bi]
  • 0 โ‰ค ai, bi โ‰ค heights.length - 1
Asked in
Google 12 Meta 8 Amazon 6 Microsoft 4
34.8K Views
Medium-High Frequency
~35 min Avg. Time
1.5K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen