diff --git a/leetcode/2901-3000/2940.Find-Building-Where-Alice-and-Bob-Can-Meet/README.md b/leetcode/2901-3000/2940.Find-Building-Where-Alice-and-Bob-Can-Meet/README.md index b751ffef2..5776decf1 100755 --- a/leetcode/2901-3000/2940.Find-Building-Where-Alice-and-Bob-Can-Meet/README.md +++ b/leetcode/2901-3000/2940.Find-Building-Where-Alice-and-Bob-Can-Meet/README.md @@ -1,28 +1,41 @@ # [2940.Find Building Where Alice and Bob Can Meet][title] -> [!WARNING|style:flat] -> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm) - ## Description +You are given a **0-indexed** array `heights` of positive integers, where `heights[i]` represents the height of the `ith` building. + +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]`. + +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`. + +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`. **Example 1:** ``` -Input: a = "11", b = "1" -Output: "100" +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 heights[0] < heights[2] and heights[1] < heights[2]. +In the second query, Alice and Bob can move to building 5 since heights[0] < heights[5] and heights[3] < heights[5]. +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 heights[3] < heights[5] and heights[4] < heights[5]. +In the fifth query, Alice and Bob are already in the same building. +For ans[i] != -1, It can be shown that ans[i] is the leftmost building where Alice and Bob can meet. +For ans[i] == -1, It can be shown that there is no building where Alice and Bob can meet. ``` -## 题意 -> ... +**Example 2:** -## 题解 - -### 思路1 -> ... -Find Building Where Alice and Bob Can Meet -```go ``` - +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 heights[0] < heights[7]. +In the second query, Alice and Bob can move to building 6 since heights[3] < heights[6] and heights[5] < heights[6]. +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 heights[3] < heights[4] and heights[0] < heights[4]. +In the fifth query, Alice can directly move to Bob's building since heights[1] < heights[6]. +For ans[i] != -1, It can be shown that ans[i] is the leftmost building where Alice and Bob can meet. +For ans[i] == -1, It can be shown that there is no building where Alice and Bob can meet. +``` ## 结语 diff --git a/leetcode/2901-3000/2940.Find-Building-Where-Alice-and-Bob-Can-Meet/Solution.go b/leetcode/2901-3000/2940.Find-Building-Where-Alice-and-Bob-Can-Meet/Solution.go index d115ccf5e..2159f369c 100644 --- a/leetcode/2901-3000/2940.Find-Building-Where-Alice-and-Bob-Can-Meet/Solution.go +++ b/leetcode/2901-3000/2940.Find-Building-Where-Alice-and-Bob-Can-Meet/Solution.go @@ -1,5 +1,54 @@ package Solution -func Solution(x bool) bool { - return x +import "sort" + +func Solution(heights []int, queries [][]int) []int { + // monotonic stack + sort + binarysearch + ll := len(queries) + indies := make([]int, ll) + for i := range ll { + if queries[i][0] > queries[i][1] { + queries[i][0], queries[i][1] = queries[i][1], queries[i][0] + } + indies[i] = i + } + sort.Slice(indies, func(i, j int) bool { + return queries[indies[i]][1] > queries[indies[j]][1] + }) + + ans := make([]int, ll) + for i := range ll { + ans[i] = -1 + } + + l := len(heights) + stack := make([]int, l) + stackIndex := l - 1 + stack[stackIndex] = l - 1 + cur := l - 2 + + for _, i := range indies { + a, b := queries[i][0], queries[i][1] + if a == b || heights[a] < heights[b] { + ans[i] = b + continue + } + if b == l-1 { + continue + } + target := max(heights[a], heights[b]) + for ; cur > b; cur-- { + for ; stackIndex != l && heights[cur] >= heights[stack[stackIndex]]; stackIndex++ { + } + stackIndex-- + stack[stackIndex] = cur + } + if idx := sort.Search(l-stackIndex, func(i int) bool { + return heights[stack[i+stackIndex]] > target + }); idx != l-stackIndex { + ans[i] = stack[idx+stackIndex] + } + } + + return ans } diff --git a/leetcode/2901-3000/2940.Find-Building-Where-Alice-and-Bob-Can-Meet/Solution_test.go b/leetcode/2901-3000/2940.Find-Building-Where-Alice-and-Bob-Can-Meet/Solution_test.go index 14ff50eb4..d07ec0687 100644 --- a/leetcode/2901-3000/2940.Find-Building-Where-Alice-and-Bob-Can-Meet/Solution_test.go +++ b/leetcode/2901-3000/2940.Find-Building-Where-Alice-and-Bob-Can-Meet/Solution_test.go @@ -9,31 +9,31 @@ import ( func TestSolution(t *testing.T) { // 测试用例 cases := []struct { - name string - inputs bool - expect bool + name string + heights []int + queries [][]int + expect []int }{ - {"TestCase", true, true}, - {"TestCase", true, true}, - {"TestCase", false, false}, + {"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}}, + {"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}}, } // 开始测试 for i, c := range cases { t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) { - got := Solution(c.inputs) + got := Solution(c.heights, c.queries) if !reflect.DeepEqual(got, c.expect) { - t.Fatalf("expected: %v, but got: %v, with inputs: %v", - c.expect, got, c.inputs) + t.Fatalf("expected: %v, but got: %v, with inputs: %v %v", + c.expect, got, c.heights, c.queries) } }) } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }