Tap here to switch tabs
Problem
Submissions

Problem: Find Building Where Alice and Bob Can Meet

Try to solve the Find Building Where Alice and Bob Can Meet problem.

Statement

You are given a 00 indexed array of positive integers heights representing the heights of buildings.

You are also given a 22D array queries, where queries[i] = [a_i, b_i]. In the iith query, Alice is located at building a_i and Bob is located at building b_i.

A person at building i can move to building j if and only if i < j and heights[i] < heights[j].

For each query, determine the index of the leftmost building where Alice and Bob can both meet. A person can either stay at their starting building or move to a building with a greater index and strictly greater height.

Return an array ans where ans[i] is the index of the leftmost building where Alice and Bob can meet for the iith query, or −1-1 if they cannot meet at any building.

Constraints:

  • 1≤1 \leq heights.length ≤5×104\leq 5 \times 10^4

  • 1≤1 \leq heights[i] ≤109\leq 10^9

  • 1≤1 \leq queries.length ≤5×104\leq 5 \times 10^4

  • queries[i] =[ai,bi]= [a_i, b_i]

  • 0≤ai,bi≤0 \leq a_i, b_i \leq heights.length −1- 1

⋮
Tap here to switch tabs
Problem
Submissions

Problem: Find Building Where Alice and Bob Can Meet

Try to solve the Find Building Where Alice and Bob Can Meet problem.

Statement

You are given a 00 indexed array of positive integers heights representing the heights of buildings.

You are also given a 22D array queries, where queries[i] = [a_i, b_i]. In the iith query, Alice is located at building a_i and Bob is located at building b_i.

A person at building i can move to building j if and only if i < j and heights[i] < heights[j].

For each query, determine the index of the leftmost building where Alice and Bob can both meet. A person can either stay at their starting building or move to a building with a greater index and strictly greater height.

Return an array ans where ans[i] is the index of the leftmost building where Alice and Bob can meet for the iith query, or −1-1 if they cannot meet at any building.

Constraints:

  • 1≤1 \leq heights.length ≤5×104\leq 5 \times 10^4

  • 1≤1 \leq heights[i] ≤109\leq 10^9

  • 1≤1 \leq queries.length ≤5×104\leq 5 \times 10^4

  • queries[i] =[ai,bi]= [a_i, b_i]

  • 0≤ai,bi≤0 \leq a_i, b_i \leq heights.length −1- 1