Maximum Number of Points From Grid Queries - Problem

Imagine you're an explorer trapped in a magical grid where each cell contains a danger level. You start at the top-left corner and want to collect as many points as possible by visiting new cells, but there's a catch: you can only move to adjacent cells (up, down, left, right) if your query power is strictly greater than the cell's danger level!

The Challenge: Given an m Γ— n integer matrix grid and an array queries of size k, find the maximum number of points you can collect for each query.

Rules:

  • 🎯 You get 1 point for each cell you visit for the first time
  • 🚫 You can only enter a cell if queries[i] > grid[row][col]
  • πŸ”„ You can revisit cells multiple times (but only get points once per cell)
  • πŸ“ You always start from the top-left corner (0, 0)

Return an array answer where answer[i] is the maximum points for queries[i].

Input & Output

example_1.py β€” Basic Grid Exploration
$ Input: grid = [[1,2,3],[2,5,7],[3,5,1]], queries = [5,6,2]
β€Ί Output: [5, 8, 1]
πŸ’‘ Note: For query 5: Can visit cells with values [1,2,3,2,3] = 5 points. For query 6: Can visit cells with values [1,2,3,2,5,3,5,1] = 8 points. For query 2: Can only visit cell [1] = 1 point.
example_2.py β€” Single Cell Grid
$ Input: grid = [[5]], queries = [4,6]
β€Ί Output: [0, 1]
πŸ’‘ Note: For query 4: Cannot enter starting cell (5 >= 4) = 0 points. For query 6: Can enter starting cell (5 < 6) = 1 point.
example_3.py β€” Large Values Block Path
$ Input: grid = [[1,10,10],[10,1,10],[10,10,1]], queries = [2,10,11]
β€Ί Output: [1, 1, 9]
πŸ’‘ Note: For queries 2 and 10: Blocked by large values, can only reach starting cell = 1 point each. For query 11: Can reach all cells = 9 points.

Constraints

  • m == grid.length
  • n == grid[i].length
  • 2 ≀ m, n ≀ 1000
  • 4 ≀ m Γ— n ≀ 105
  • 1 ≀ grid[i][j], queries[j] ≀ 106
  • 1 ≀ queries.length ≀ 104
  • You must start from grid[0][0]

Visualization

Tap to expand
πŸ΄β€β˜ οΈ Treasure Hunter's Grid ExplorationπŸ—ΊοΈ Dungeon MapRoom3βš”οΈ DangerRoom8βš”οΈ DangerRoom2βš”οΈ DangerRoom4βš”οΈ DangerπŸ΄β€β˜ οΈ STARTπŸ’ͺ Power Level AnalysisQuery Power = 5Can enter: 3 < 5 βœ…, 2 < 5 βœ…, 4 < 5 βœ…Query Power = 10Can enter: ALL rooms (3,8,2,4 < 10) βœ…Query Power = 2Can only enter: None except start ❌🧠 Smart Algorithm Strategy❌ Brute Forceβ€’ Run BFS for each queryβ€’ Repeat same workβ€’ O(k Γ— m Γ— n) timeβ€’ Very slow for many queriesInefficient! πŸ˜žβœ… Union-Findβ€’ Sort cells by danger levelβ€’ Sort queries by powerβ€’ Build components incrementallyβ€’ Reuse previous workOptimal! πŸ˜ŠπŸ”‘ Key Insights1. Higher power can access everything lower power can2. We can build solutions incrementally3. Union-Find tracks connected components4. Sort to process in optimal order5. Component size = points available
Understanding the Visualization
1
Power Check
Can only enter rooms where your query power > room danger level
2
Connected Exploration
Move to adjacent rooms (up, down, left, right) that you can access
3
Point Collection
Get 1 point for each unique room visited, regardless of revisits
4
Smart Optimization
Sort queries and build reusable component maps instead of exploring separately
Key Takeaway
🎯 Key Insight: Instead of exploring the grid separately for each query, we can sort queries by power level and incrementally build connected components using Union-Find, reusing previous computations for maximum efficiency!
Asked in
Google 42 Meta 38 Amazon 35 Microsoft 28
68.2K Views
Medium-High Frequency
~35 min Avg. Time
1.8K 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