Class Solution
-
- All Implemented Interfaces:
public final class Solution2940 - Find Building Where Alice and Bob Can Meet\.
Hard
You are given a 0-indexed array
heightsof positive integers, whereheights[i]represents the height of the <code>i<sup>th</sup></code> building.If a person is in building
i, they can move to any other buildingjif and only ifi < jandheights[i] < heights[j].You are also given another array
querieswhere <code>queriesi = a<sub>i</sub>, b<sub>i</sub></code>. On the <code>i<sup>th</sup></code> query, Alice is in building <code>a<sub>i</sub></code> while Bob is in building <code>b<sub>i</sub></code>.Return an array
answhereans[i]is the index of the leftmost building where Alice and Bob can meet on the <code>i<sup>th</sup></code> query. If Alice and Bob cannot move to a common building on queryi, setans[i]to-1.Example 1:
Input: heights = 6,4,8,5,2,7, queries = \[\[0,1],0,3,2,4,3,4,2,2]
Output: 2,5,-1,5,2
Explanation: In the first query, Alice and Bob can move to building 2 since heights0< heights2 and heights1< heights2.
In the second query, Alice and Bob can move to building 5 since heights0< heights5 and heights3< heights5.
In the third query, Alice cannot meet Bob since Alice cannot move to any other building.
In the fourth query, Alice and Bob can move to building 5 since heights3< heights5 and heights4< heights5.
In the fifth query, Alice and Bob are already in the same building.
For ansi != -1, It can be shown that ansi is the leftmost building where Alice and Bob can meet.
For ansi == -1, It can be shown that there is no building where Alice and Bob can meet.
Example 2:
Input: heights = 5,3,8,2,6,1,4,6, queries = \[\[0,7],3,5,5,2,3,0,1,6]
Output: 7,6,-1,4,6
Explanation: In the first query, Alice can directly move to Bob's building since heights0< heights7.
In the second query, Alice and Bob can move to building 6 since heights3< heights6 and heights5< heights6.
In the third query, Alice cannot meet Bob since Bob cannot move to any other building.
In the fourth query, Alice and Bob can move to building 4 since heights3< heights4 and heights0< heights4.
In the fifth query, Alice can directly move to Bob's building since heights1< heights6.
For ansi != -1, It can be shown that ansi is the leftmost building where Alice and Bob can meet.
For ansi == -1, It can be shown that there is no building where Alice and Bob can meet.
Constraints:
<code>1 <= heights.length <= 5 * 10<sup>4</sup></code>
<code>1 <= heightsi<= 10<sup>9</sup></code>
<code>1 <= queries.length <= 5 * 10<sup>4</sup></code>
<code>queriesi = a<sub>i</sub>, b<sub>i</sub></code>
<code>0 <= a<sub>i</sub>, b<sub>i</sub><= heights.length - 1</code>
-
-
Constructor Summary
Constructors Constructor Description Solution()
-
Method Summary
Modifier and Type Method Description final IntArrayleftmostBuildingQueries(IntArray heights, Array<IntArray> queries)-
-
Method Detail
-
leftmostBuildingQueries
final IntArray leftmostBuildingQueries(IntArray heights, Array<IntArray> queries)
-
-
-
-