|
1 | 1 | # [2940.Find Building Where Alice and Bob Can Meet][title] |
2 | 2 |
|
3 | | -> [!WARNING|style:flat] |
4 | | -> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm) |
5 | | -
|
6 | 3 | ## Description |
| 4 | +You are given a **0-indexed** array `heights` of positive integers, where `heights[i]` represents the height of the `ith` building. |
| 5 | + |
| 6 | +If a person is in building `i`, they can move to any other building `j` if and only if `i < j` and `heights[i] < heights[j]`. |
| 7 | + |
| 8 | +You are also given another array `queries` where `queries[i] = [ai, bi]`. On the `ith` query, Alice is in building `ai` while Bob is in building `bi`. |
| 9 | + |
| 10 | +Return an array `ans` where `ans[i]` is **the index of the leftmost building** where Alice and Bob can meet on the `ith` query. If Alice and Bob cannot move to a common building on query `i`, set `ans[i]` to `-1`. |
7 | 11 |
|
8 | 12 | **Example 1:** |
9 | 13 |
|
10 | 14 | ``` |
11 | | -Input: a = "11", b = "1" |
12 | | -Output: "100" |
| 15 | +Input: heights = [6,4,8,5,2,7], queries = [[0,1],[0,3],[2,4],[3,4],[2,2]] |
| 16 | +Output: [2,5,-1,5,2] |
| 17 | +Explanation: In the first query, Alice and Bob can move to building 2 since heights[0] < heights[2] and heights[1] < heights[2]. |
| 18 | +In the second query, Alice and Bob can move to building 5 since heights[0] < heights[5] and heights[3] < heights[5]. |
| 19 | +In the third query, Alice cannot meet Bob since Alice cannot move to any other building. |
| 20 | +In the fourth query, Alice and Bob can move to building 5 since heights[3] < heights[5] and heights[4] < heights[5]. |
| 21 | +In the fifth query, Alice and Bob are already in the same building. |
| 22 | +For ans[i] != -1, It can be shown that ans[i] is the leftmost building where Alice and Bob can meet. |
| 23 | +For ans[i] == -1, It can be shown that there is no building where Alice and Bob can meet. |
13 | 24 | ``` |
14 | 25 |
|
15 | | -## 题意 |
16 | | -> ... |
| 26 | +**Example 2:** |
17 | 27 |
|
18 | | -## 题解 |
19 | | - |
20 | | -### 思路1 |
21 | | -> ... |
22 | | -Find Building Where Alice and Bob Can Meet |
23 | | -```go |
24 | 28 | ``` |
25 | | - |
| 29 | +Input: heights = [5,3,8,2,6,1,4,6], queries = [[0,7],[3,5],[5,2],[3,0],[1,6]] |
| 30 | +Output: [7,6,-1,4,6] |
| 31 | +Explanation: In the first query, Alice can directly move to Bob's building since heights[0] < heights[7]. |
| 32 | +In the second query, Alice and Bob can move to building 6 since heights[3] < heights[6] and heights[5] < heights[6]. |
| 33 | +In the third query, Alice cannot meet Bob since Bob cannot move to any other building. |
| 34 | +In the fourth query, Alice and Bob can move to building 4 since heights[3] < heights[4] and heights[0] < heights[4]. |
| 35 | +In the fifth query, Alice can directly move to Bob's building since heights[1] < heights[6]. |
| 36 | +For ans[i] != -1, It can be shown that ans[i] is the leftmost building where Alice and Bob can meet. |
| 37 | +For ans[i] == -1, It can be shown that there is no building where Alice and Bob can meet. |
| 38 | +``` |
26 | 39 |
|
27 | 40 | ## 结语 |
28 | 41 |
|
|
0 commit comments