|
| 1 | +<h2><a href="https://leetcode.com/problems/find-building-where-alice-and-bob-can-meet/">2940. Find Building Where Alice and Bob Can Meet</a></h2><h3>Hard</h3><hr><div><p>You are given a <strong>0-indexed</strong> array <code>heights</code> of positive integers, where <code>heights[i]</code> represents the height of the <code>i<sup>th</sup></code> building.</p> |
| 2 | + |
| 3 | +<p>If a person is in building <code>i</code>, they can move to any other building <code>j</code> if and only if <code>i < j</code> and <code>heights[i] < heights[j]</code>.</p> |
| 4 | + |
| 5 | +<p>You are also given another array <code>queries</code> where <code>queries[i] = [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>.</p> |
| 6 | + |
| 7 | +<p>Return <em>an array</em> <code>ans</code> <em>where</em> <code>ans[i]</code> <em>is <strong>the index of the leftmost building</strong> where Alice and Bob can meet on the</em> <code>i<sup>th</sup></code> <em>query</em>. <em>If Alice and Bob cannot move to a common building on query</em> <code>i</code>, <em>set</em> <code>ans[i]</code> <em>to</em> <code>-1</code>.</p> |
| 8 | + |
| 9 | +<p> </p> |
| 10 | +<p><strong class="example">Example 1:</strong></p> |
| 11 | + |
| 12 | +<pre><strong>Input:</strong> heights = [6,4,8,5,2,7], queries = [[0,1],[0,3],[2,4],[3,4],[2,2]] |
| 13 | +<strong>Output:</strong> [2,5,-1,5,2] |
| 14 | +<strong>Explanation:</strong> In the first query, Alice and Bob can move to building 2 since heights[0] < heights[2] and heights[1] < heights[2]. |
| 15 | +In the second query, Alice and Bob can move to building 5 since heights[0] < heights[5] and heights[3] < heights[5]. |
| 16 | +In the third query, Alice cannot meet Bob since Alice cannot move to any other building. |
| 17 | +In the fourth query, Alice and Bob can move to building 5 since heights[3] < heights[5] and heights[4] < heights[5]. |
| 18 | +In the fifth query, Alice and Bob are already in the same building. |
| 19 | +For ans[i] != -1, It can be shown that ans[i] is the leftmost building where Alice and Bob can meet. |
| 20 | +For ans[i] == -1, It can be shown that there is no building where Alice and Bob can meet. |
| 21 | +</pre> |
| 22 | + |
| 23 | +<p><strong class="example">Example 2:</strong></p> |
| 24 | + |
| 25 | +<pre><strong>Input:</strong> heights = [5,3,8,2,6,1,4,6], queries = [[0,7],[3,5],[5,2],[3,0],[1,6]] |
| 26 | +<strong>Output:</strong> [7,6,-1,4,6] |
| 27 | +<strong>Explanation:</strong> In the first query, Alice can directly move to Bob's building since heights[0] < heights[7]. |
| 28 | +In the second query, Alice and Bob can move to building 6 since heights[3] < heights[6] and heights[5] < heights[6]. |
| 29 | +In the third query, Alice cannot meet Bob since Bob cannot move to any other building. |
| 30 | +In the fourth query, Alice and Bob can move to building 4 since heights[3] < heights[4] and heights[0] < heights[4]. |
| 31 | +In the fifth query, Alice can directly move to Bob's building since heights[1] < heights[6]. |
| 32 | +For ans[i] != -1, It can be shown that ans[i] is the leftmost building where Alice and Bob can meet. |
| 33 | +For ans[i] == -1, It can be shown that there is no building where Alice and Bob can meet. |
| 34 | + |
| 35 | +</pre> |
| 36 | + |
| 37 | +<p> </p> |
| 38 | +<p><strong>Constraints:</strong></p> |
| 39 | + |
| 40 | +<ul> |
| 41 | + <li><code>1 <= heights.length <= 5 * 10<sup>4</sup></code></li> |
| 42 | + <li><code>1 <= heights[i] <= 10<sup>9</sup></code></li> |
| 43 | + <li><code>1 <= queries.length <= 5 * 10<sup>4</sup></code></li> |
| 44 | + <li><code>queries[i] = [a<sub>i</sub>, b<sub>i</sub>]</code></li> |
| 45 | + <li><code>0 <= a<sub>i</sub>, b<sub>i</sub> <= heights.length - 1</code></li> |
| 46 | +</ul> |
| 47 | +</div> |
0 commit comments