Skip to content

Commit 89b4d10

Browse files
authored
Merge pull request #1161 from 0xff-dev/2503
Add solution and test-cases for problem 2503
2 parents e0c1622 + c22bb6b commit 89b4d10

File tree

5 files changed

+111
-13
lines changed

5 files changed

+111
-13
lines changed
Loading
Loading
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# [2503.Maximum Number of Points From Grid Queries][title]
2+
3+
## Description
4+
5+
You are given an `m x n` integer matrix `grid` and an array `queries` of size `k`.
6+
7+
Find an array `answer` of size k such that for each integer `queries[i]` you start in the **top left** cell of the matrix and repeat the following process:
8+
9+
- If `queries[i]` is **strictly** greater than the value of the current cell that you are in, then you get one point if it is your first time visiting this cell, and you can move to any **adjacent** cell in all `4` directions: up, down, left, and right.
10+
- Otherwise, you do not get any points, and you end this process.
11+
12+
After the process, `answer[i]` is the **maximum** number of points you can get. **Note** that for each query you are allowed to visit the same cell **multiple** times.
13+
14+
Return the resulting array `answer`.
15+
16+
**Example 1:**
17+
18+
![1](./1.png)
19+
20+
```
21+
Input: grid = [[1,2,3],[2,5,7],[3,5,1]], queries = [5,6,2]
22+
Output: [5,8,1]
23+
Explanation: The diagrams above show which cells we visit to get points for each query.
24+
```
25+
26+
**EXample 2:**
27+
28+
![2](./2.png)
29+
30+
```
31+
Input: grid = [[5,2,1],[1,1,2]], queries = [3]
32+
Output: [0]
33+
Explanation: We can not get any points because the value of the top left cell is already greater than or equal to 3.
34+
```
35+
36+
## 结语
37+
38+
如果你同我一样热爱数据结构、算法、LeetCode,可以关注我 GitHub 上的 LeetCode 题解:[awesome-golang-algorithm][me]
39+
40+
[title]: https://leetcode.com/problems/maximum-number-of-points-from-grid-queries/
41+
[me]: https://github.com/kylesliu/awesome-golang-algorithm
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,62 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
import "sort"
4+
5+
func Solution(grid [][]int, queries []int) []int {
6+
var dirs = [][2]int{
7+
{0, 1}, {1, 0}, {0, -1}, {-1, 0},
8+
}
9+
rows, cols := len(grid), len(grid[0])
10+
visited := make([][]bool, rows)
11+
for i := range rows {
12+
visited[i] = make([]bool, cols)
13+
}
14+
indies := make([]int, len(queries))
15+
for i := range queries {
16+
indies[i] = i
17+
}
18+
sort.Slice(indies, func(i, j int) bool {
19+
ai, bi := indies[i], indies[j]
20+
return queries[ai] < queries[bi]
21+
})
22+
23+
ans := make([]int, len(queries))
24+
queue := [][2]int{{0, 0}}
25+
visited[0][0] = true
26+
cal := make(map[int]int)
27+
cnt := 0
28+
for _, index := range indies {
29+
target := queries[index]
30+
if v, ok := cal[target]; ok {
31+
ans[index] = v
32+
continue
33+
}
34+
for {
35+
nq := make([][2]int, 0)
36+
all := true
37+
for _, cur := range queue {
38+
if grid[cur[0]][cur[1]] >= target {
39+
nq = append(nq, [2]int{cur[0], cur[1]})
40+
continue
41+
}
42+
all = false
43+
cnt++
44+
for _, d := range dirs {
45+
nx, ny := cur[0]+d[0], cur[1]+d[1]
46+
if nx < 0 || nx >= rows || ny < 0 || ny >= cols || visited[nx][ny] {
47+
continue
48+
}
49+
nq = append(nq, [2]int{nx, ny})
50+
visited[nx][ny] = true
51+
}
52+
}
53+
queue = nq
54+
if all {
55+
break
56+
}
57+
}
58+
cal[target] = cnt
59+
ans[index] = cnt
60+
}
61+
return ans
562
}

leetcode/2501-2600/2503.Maximum-Number-of-Points-From-Grid-Queries/Solution_test.go

+11-11
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+
grid [][]int
14+
queries []int
15+
expect []int
1516
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
17+
{"TestCase1", [][]int{{1, 2, 3}, {2, 5, 7}, {3, 5, 1}}, []int{5, 6, 2}, []int{5, 8, 1}},
18+
{"TestCase2", [][]int{{5, 2, 1}, {1, 1, 2}}, []int{3}, []int{0}},
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.grid, 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.grid, 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)