#2503

Maximum Number of Points From Grid Queries

expert · 1195 · lc hard +32 · verified · 59.3% accepted · 1,100 likes · top 57%

Description

Given an m x n integer matrix grid and a query array, process each query value by starting at the top-left cell: while the query value is strictly greater than the current cell's value, collect one point (only on first visit) and move to any of the four adjacent cells. Return an array where entry i is the maximum points collectible for queries[i].

Example 1:

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:

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.

Code

1
2
3