2503. Maximum Number of Points From Grid Queries #1489
-
Topics: You are given an Find an array
After the process, Return the resulting array Example 1:
Example 2:
Constraints:
Hint:
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
We need to determine the maximum number of points we can collect starting from the top-left cell of a grid for each query. The points are earned by visiting cells with values strictly less than the query value, and movement is allowed in all four directions (up, down, left, right). Approach
Let's implement this solution in PHP: 2503. Maximum Number of Points From Grid Queries <?php
/**
* @param Integer[][] $grid
* @param Integer[] $queries
* @return Integer[]
*/
function maxPoints($grid, $queries) {
$rows = count($grid);
$cols = count($grid[0]);
$INF = PHP_INT_MAX;
// Initialize distance array with INF, except the starting cell
$dist = array_fill(0, $rows, array_fill(0, $cols, $INF));
$dist[0][0] = $grid[0][0];
// Priority queue to process cells in order of their current max value (min-heap)
$pq = new SplPriorityQueue();
$pq->setExtractFlags(SplPriorityQueue::EXTR_BOTH);
$pq->insert([0, 0], -$dist[0][0]); // Using negative to simulate min-heap
$dirs = [[-1, 0], [1, 0], [0, -1], [0, 1]];
while (!$pq->isEmpty()) {
$element = $pq->extract();
$current_max = -$element['priority'];
$i = $element['data'][0];
$j = $element['data'][1];
// Skip if a shorter path (smaller max) was already found
if ($current_max > $dist[$i][$j]) {
continue;
}
foreach ($dirs as $d) {
$ni = $i + $d[0];
$nj = $j + $d[1];
if ($ni >= 0 && $ni < $rows && $nj >= 0 && $nj < $cols) {
$new_max = max($current_max, $grid[$ni][$nj]);
if ($new_max < $dist[$ni][$nj]) {
$dist[$ni][$nj] = $new_max;
$pq->insert([$ni, $nj], -$new_max);
}
}
}
}
// Collect all minimal max values into a list and sort them
$min_max_list = array();
foreach ($dist as $row) {
foreach ($row as $val) {
$min_max_list[] = $val;
}
}
sort($min_max_list);
// Prepare answers for each query using binary search
$answers = array();
foreach ($queries as $q) {
$low = 0;
$high = count($min_max_list);
while ($low < $high) {
$mid = intdiv($low + $high, 2);
if ($min_max_list[$mid] < $q) {
$low = $mid + 1;
} else {
$high = $mid;
}
}
$answers[] = $low;
}
return $answers;
}
// Example 1
$grid1 = [
[1, 2, 3],
[2, 5, 7],
[3, 5, 1]
];
$queries1 = [5, 6, 2];
print_r(maxPoints($grid1, $queries1)); // Output: [5, 8, 1]
// Example 2
$grid2 = [
[5, 2, 1],
[1, 1, 2]
];
$queries2 = [3];
print_r(maxPoints($grid2, $queries2)); // Output: [0]?> Explanation:
This approach efficiently processes the grid and queries, ensuring optimal performance even for large grids and numerous queries. |
Beta Was this translation helpful? Give feedback.
We need to determine the maximum number of points we can collect starting from the top-left cell of a grid for each query. The points are earned by visiting cells with values strictly less than the query value, and movement is allowed in all four directions (up, down, left, right).
Approach