Skip to content

Commit 83c7283

Browse files
authored
Merge pull request #1071 from 0xff-dev/2940
Add solution and test-cases for problem 2940
2 parents a233a34 + de978c8 commit 83c7283

File tree

3 files changed

+89
-27
lines changed

3 files changed

+89
-27
lines changed

leetcode/2901-3000/2940.Find-Building-Where-Alice-and-Bob-Can-Meet/README.md

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,41 @@
11
# [2940.Find Building Where Alice and Bob Can Meet][title]
22

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-
63
## 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`.
711

812
**Example 1:**
913

1014
```
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.
1324
```
1425

15-
## 题意
16-
> ...
26+
**Example 2:**
1727

18-
## 题解
19-
20-
### 思路1
21-
> ...
22-
Find Building Where Alice and Bob Can Meet
23-
```go
2428
```
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+
```
2639

2740
## 结语
2841

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,54 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
import "sort"
4+
5+
func Solution(heights []int, queries [][]int) []int {
6+
// monotonic stack + sort + binarysearch
7+
ll := len(queries)
8+
indies := make([]int, ll)
9+
for i := range ll {
10+
if queries[i][0] > queries[i][1] {
11+
queries[i][0], queries[i][1] = queries[i][1], queries[i][0]
12+
}
13+
indies[i] = i
14+
}
15+
sort.Slice(indies, func(i, j int) bool {
16+
return queries[indies[i]][1] > queries[indies[j]][1]
17+
})
18+
19+
ans := make([]int, ll)
20+
for i := range ll {
21+
ans[i] = -1
22+
}
23+
24+
l := len(heights)
25+
stack := make([]int, l)
26+
stackIndex := l - 1
27+
stack[stackIndex] = l - 1
28+
cur := l - 2
29+
30+
for _, i := range indies {
31+
a, b := queries[i][0], queries[i][1]
32+
if a == b || heights[a] < heights[b] {
33+
ans[i] = b
34+
continue
35+
}
36+
if b == l-1 {
37+
continue
38+
}
39+
target := max(heights[a], heights[b])
40+
for ; cur > b; cur-- {
41+
for ; stackIndex != l && heights[cur] >= heights[stack[stackIndex]]; stackIndex++ {
42+
}
43+
stackIndex--
44+
stack[stackIndex] = cur
45+
}
46+
if idx := sort.Search(l-stackIndex, func(i int) bool {
47+
return heights[stack[i+stackIndex]] > target
48+
}); idx != l-stackIndex {
49+
ans[i] = stack[idx+stackIndex]
50+
}
51+
}
52+
53+
return ans
554
}

leetcode/2901-3000/2940.Find-Building-Where-Alice-and-Bob-Can-Meet/Solution_test.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,31 +9,31 @@ import (
99
func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
12-
name string
13-
inputs bool
14-
expect bool
12+
name string
13+
heights []int
14+
queries [][]int
15+
expect []int
1516
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
17+
{"TestCase1", []int{6, 4, 8, 5, 2, 7}, [][]int{{0, 1}, {0, 3}, {2, 4}, {3, 4}, {2, 2}}, []int{2, 5, -1, 5, 2}},
18+
{"TestCase2", []int{5, 3, 8, 2, 6, 1, 4, 6}, [][]int{{0, 7}, {3, 5}, {5, 2}, {3, 0}, {1, 6}}, []int{7, 6, -1, 4, 6}},
1919
}
2020

2121
// 开始测试
2222
for i, c := range cases {
2323
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
24-
got := Solution(c.inputs)
24+
got := Solution(c.heights, c.queries)
2525
if !reflect.DeepEqual(got, c.expect) {
26-
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
27-
c.expect, got, c.inputs)
26+
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v",
27+
c.expect, got, c.heights, c.queries)
2828
}
2929
})
3030
}
3131
}
3232

33-
// 压力测试
33+
// 压力测试
3434
func BenchmarkSolution(b *testing.B) {
3535
}
3636

37-
// 使用案列
37+
// 使用案列
3838
func ExampleSolution() {
3939
}

0 commit comments

Comments
 (0)