Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add solution and test-cases for problem 2503 #1161

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# [2503.Maximum Number of Points From Grid Queries][title]

## Description

You are given an `m x n` integer matrix `grid` and an array `queries` of size `k`.

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:

- 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.
- Otherwise, you do not get any points, and you end this process.

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.

Return the resulting array `answer`.

**Example 1:**

![1](./1.png)

```
Input: grid = [[1,2,3],[2,5,7],[3,5,1]], queries = [5,6,2]
Output: [5,8,1]
Explanation: The diagrams above show which cells we visit to get points for each query.
```

**EXample 2:**

![2](./2.png)

```
Input: grid = [[5,2,1],[1,1,2]], queries = [3]
Output: [0]
Explanation: We can not get any points because the value of the top left cell is already greater than or equal to 3.
```

## 结语

如果你同我一样热爱数据结构、算法、LeetCode,可以关注我 GitHub 上的 LeetCode 题解:[awesome-golang-algorithm][me]

[title]: https://leetcode.com/problems/maximum-number-of-points-from-grid-queries/
[me]: https://github.com/kylesliu/awesome-golang-algorithm
Original file line number Diff line number Diff line change
@@ -1,5 +1,62 @@
package Solution

func Solution(x bool) bool {
return x
import "sort"

func Solution(grid [][]int, queries []int) []int {
var dirs = [][2]int{
{0, 1}, {1, 0}, {0, -1}, {-1, 0},
}
rows, cols := len(grid), len(grid[0])
visited := make([][]bool, rows)
for i := range rows {
visited[i] = make([]bool, cols)
}
indies := make([]int, len(queries))
for i := range queries {
indies[i] = i
}
sort.Slice(indies, func(i, j int) bool {
ai, bi := indies[i], indies[j]
return queries[ai] < queries[bi]
})

ans := make([]int, len(queries))
queue := [][2]int{{0, 0}}
visited[0][0] = true
cal := make(map[int]int)
cnt := 0
for _, index := range indies {
target := queries[index]
if v, ok := cal[target]; ok {
ans[index] = v
continue
}
for {
nq := make([][2]int, 0)
all := true
for _, cur := range queue {
if grid[cur[0]][cur[1]] >= target {
nq = append(nq, [2]int{cur[0], cur[1]})
continue
}
all = false
cnt++
for _, d := range dirs {
nx, ny := cur[0]+d[0], cur[1]+d[1]
if nx < 0 || nx >= rows || ny < 0 || ny >= cols || visited[nx][ny] {
continue
}
nq = append(nq, [2]int{nx, ny})
visited[nx][ny] = true
}
}
queue = nq
if all {
break
}
}
cal[target] = cnt
ans[index] = cnt
}
return ans
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,31 +9,31 @@ import (
func TestSolution(t *testing.T) {
// 测试用例
cases := []struct {
name string
inputs bool
expect bool
name string
grid [][]int
queries []int
expect []int
}{
{"TestCase", true, true},
{"TestCase", true, true},
{"TestCase", false, false},
{"TestCase1", [][]int{{1, 2, 3}, {2, 5, 7}, {3, 5, 1}}, []int{5, 6, 2}, []int{5, 8, 1}},
{"TestCase2", [][]int{{5, 2, 1}, {1, 1, 2}}, []int{3}, []int{0}},
}

// 开始测试
for i, c := range cases {
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
got := Solution(c.inputs)
got := Solution(c.grid, 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.grid, c.queries)
}
})
}
}

// 压力测试
// 压力测试
func BenchmarkSolution(b *testing.B) {
}

// 使用案列
// 使用案列
func ExampleSolution() {
}
Loading