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:
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.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:
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.
Constraints:
m == grid.lengthn == grid[i].length2 <= m, n <= 10004 <= m * n <= 105k == queries.length1 <= k <= 1041 <= grid[i][j], queries[i] <= 106This approach utilizes a priority queue to perform a modified breadth-first search (BFS). The priority queue helps to always process the cell with the smallest value first, which allows us to explore possible paths that maximize the points by focusing on cells with values less than the query threshold. As you process each cell, push all valid and unvisited neighbors into the priority queue. Repeat this process and count the points until no more cells can be processed for the given query.
This Python solution uses a priority queue from the heapq module to perform BFS starting from the top-left cell (0,0). The BFS only visits cells with values strictly less than the current query. Cells are pushed to the priority queue with their values as the sorting key to ensure that we always prioritize smaller values. Once cells cannot be moved into (i.e., all surrounding cells have values equal or greater than the query), the BFS stops, and the number of points collected is recorded.
C++
Time Complexity: O(m*n*log(m*n)), since we may have to visit each cell once and heap operations involve logarithmic time complexity.
Space Complexity: O(m*n) used for visited matrix and priority queue.
A depth-first search (DFS) with backtracking can also be employed to solve this problem. The idea is to explore each cell recursively and count valid paths as long as the cell values remain less than the query. Backtracking helps to explore all potential paths from a given cell by undoing a move and trying other possible moves, ensuring no path is mistakenly abandoned. Since this is done for each query independently, memoization can be applied if a particular state is reached again for optimization.
The Java solution leverages a recursive DFS function to explore all possible paths from a starting point in the grid. It recursively checks whether moving in a certain direction is valid (staying within bounds, not revisiting, and having a lesser value than query). Points are awarded for each valid cell visited. Backtracking is conducted by marking the cell as unvisited after exploration, enabling other paths to be explored effectively.
JavaScript
Time Complexity: Potentially O(m*n*4^m*n) as each cell is explored with up to 4 possible moves (explorable many times), similar or less with pruning or constraints.
Space Complexity: O(m*n) needed for visited array in recursive stack.
| Approach | Complexity |
|---|---|
| Breadth-First Search with Priority Queue | Time Complexity: O(m*n*log(m*n)), since we may have to visit each cell once and heap operations involve logarithmic time complexity. Space Complexity: O(m*n) used for visited matrix and priority queue. |
| Depth-First Search with Backtracking | Time Complexity: Potentially O(m*n*4^m*n) as each cell is explored with up to 4 possible moves (explorable many times), similar or less with pruning or constraints. Space Complexity: O(m*n) needed for visited array in recursive stack. |
Maximum Number of Points From Grid Queries - Leetcode 2503 - Python • NeetCodeIO • 8,020 views views
Watch 9 more video solutions →Practice Maximum Number of Points From Grid Queries with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor