diff --git a/leetcode/2501-2600/2503.Maximum-Number-of-Points-From-Grid-Queries/1.png b/leetcode/2501-2600/2503.Maximum-Number-of-Points-From-Grid-Queries/1.png new file mode 100644 index 000000000..c1443894b Binary files /dev/null and b/leetcode/2501-2600/2503.Maximum-Number-of-Points-From-Grid-Queries/1.png differ diff --git a/leetcode/2501-2600/2503.Maximum-Number-of-Points-From-Grid-Queries/2.png b/leetcode/2501-2600/2503.Maximum-Number-of-Points-From-Grid-Queries/2.png new file mode 100644 index 000000000..3643db517 Binary files /dev/null and b/leetcode/2501-2600/2503.Maximum-Number-of-Points-From-Grid-Queries/2.png differ diff --git a/leetcode/2501-2600/2503.Maximum-Number-of-Points-From-Grid-Queries/README.md b/leetcode/2501-2600/2503.Maximum-Number-of-Points-From-Grid-Queries/README.md new file mode 100644 index 000000000..798e36a63 --- /dev/null +++ b/leetcode/2501-2600/2503.Maximum-Number-of-Points-From-Grid-Queries/README.md @@ -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 diff --git a/leetcode/2501-2600/2503.Maximum-Number-of-Points-From-Grid-Queries/Solution.go b/leetcode/2501-2600/2503.Maximum-Number-of-Points-From-Grid-Queries/Solution.go index d115ccf5e..8ea38fe2e 100755 --- a/leetcode/2501-2600/2503.Maximum-Number-of-Points-From-Grid-Queries/Solution.go +++ b/leetcode/2501-2600/2503.Maximum-Number-of-Points-From-Grid-Queries/Solution.go @@ -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 } diff --git a/leetcode/2501-2600/2503.Maximum-Number-of-Points-From-Grid-Queries/Solution_test.go b/leetcode/2501-2600/2503.Maximum-Number-of-Points-From-Grid-Queries/Solution_test.go index 14ff50eb4..ba452cf59 100755 --- a/leetcode/2501-2600/2503.Maximum-Number-of-Points-From-Grid-Queries/Solution_test.go +++ b/leetcode/2501-2600/2503.Maximum-Number-of-Points-From-Grid-Queries/Solution_test.go @@ -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() { }