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
Maximum Number of Points From Grid Queries INPUT Grid (2x3 matrix) 1 2 3 4 5 6 Queries Array 3 4 2 Cells sorted by value: 1, 2, 3, 4, 5, 6 Start: top-left (0,0) ALGORITHM STEPS 1 Sort Queries Keep original indices [2,3,4] with idx [2,0,1] 2 Sort Cells by Value Process in ascending order 3 Union-Find Merge Unite cells with smaller values to form regions 4 Count Reachable Size of component at (0,0) for each query Union-Find Process: 1 --> 1,2 --> 1,2,3 Regions grow as values increase FINAL RESULT Query Results Breakdown Query = 3: Cells with value < 3: {1,2} Points = 2 Query = 4: Cells with value < 4: {1,2,3} Points = 3 Query = 2: Cells with value < 2: {1} Points = 1 Output Array 2 3 1 OK - [2, 3, 1] Key Insight: By sorting both queries and grid cells by value, we can use Union-Find to incrementally build connected components. For each query q, we unite all cells with value < q that are adjacent to already-visited cells. The answer is the size of the component containing (0,0). Time: O(mn * log(mn) + k * log(k)) TutorialsPoint - Maximum Number of Points From Grid Queries | Union-Find with Sorting Approach
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