Maximum Number of Points From Grid Queries - Problem

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.

Input & Output

Example 1 — Basic Grid Exploration
$ Input: grid = [[1,2,3],[4,5,6]], queries = [3,4,2]
Output: [2,3,1]
💡 Note: Query 3: Can visit cells with value < 3, which are (0,0)=1 and (1,0)=4? No, (1,0)=4. Only (0,0)=1 and (0,1)=2 are reachable → 2 points. Query 4: Can reach (0,0)=1, (0,1)=2, (1,0)=4? No, can't reach (1,0) from (0,1). Wait, let me recalculate. From (0,0) with query=3: visit (0,0)=1, then (0,1)=2. Can't go to (1,0)=4 since 4≥3. Answer is 2. Query 4: visit (0,0)=1, (0,1)=2, can't reach others. Answer is 2. Query 2: visit only (0,0)=1. Answer is 1.
Example 2 — Single Cell Grid
$ Input: grid = [[5]], queries = [3,6]
Output: [0,1]
💡 Note: Query 3: Cannot start since grid[0][0]=5 ≥ 3, so 0 points. Query 6: Can visit grid[0][0]=5 since 5 < 6, so 1 point.
Example 3 — All Cells Accessible
$ Input: grid = [[1,1],[1,1]], queries = [2]
Output: [4]
💡 Note: Query 2: All cells have value 1 < 2, so all 4 cells are reachable from (0,0), giving 4 points total.

Constraints

  • m == grid.length
  • n == grid[i].length
  • 2 ≤ m, n ≤ 1000
  • 4 ≤ m × n ≤ 105
  • k == queries.length
  • 1 ≤ k ≤ 104
  • 1 ≤ grid[i][j], queries[i] ≤ 106

Visualization

Tap to expand
Grid Query Problem: Collect Points Based on Value Thresholds123456Grid: [[1,2,3],[4,5,6]]Queries: [3,4,2]Query 3: cells < 3→ visit [1,2] → 2 pointsQuery 4: cells < 4→ visit [1,2] → 2 pointsBFS Process:1. Start at (0,0)2. Visit cells with value < query3. Count unique visited cells4. Return counts for all queriesFinal Answer: [2,2,1]Points collected for each query🎯 Key Insight: BFS exploration limited by value threshold determines reachable region size
Understanding the Visualization
1
Input Grid
Matrix with cell values and query thresholds
2
BFS Exploration
For each query, explore cells with value < threshold
3
Point Collection
Count unique reachable cells for each query
Key Takeaway
🎯 Key Insight: Process queries efficiently by sorting and using Union-Find to avoid redundant traversals
Asked in
Google 12 Meta 8
12.5K Views
Medium Frequency
~35 min Avg. Time
245 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen