Skip to content

Commit 09f2871

Browse files
committed
Add solution #2503
1 parent 23e56fa commit 09f2871

File tree

2 files changed

+70
-1
lines changed

2 files changed

+70
-1
lines changed

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,035 LeetCode solutions in JavaScript
1+
# 1,036 LeetCode solutions in JavaScript
22

33
[https://leetcode.com/](https://leetcode.com/)
44

@@ -964,6 +964,7 @@
964964
2482|[Difference Between Ones and Zeros in Row and Column](./solutions/2482-difference-between-ones-and-zeros-in-row-and-column.js)|Medium|
965965
2490|[Circular Sentence](./solutions/2490-circular-sentence.js)|Easy|
966966
2493|[Divide Nodes Into the Maximum Number of Groups](./solutions/2493-divide-nodes-into-the-maximum-number-of-groups.js)|Hard|
967+
2503|[Maximum Number of Points From Grid Queries](./solutions/2503-maximum-number-of-points-from-grid-queries.js)|Hard|
967968
2523|[Closest Prime Numbers in Range](./solutions/2523-closest-prime-numbers-in-range.js)|Medium|
968969
2529|[Maximum Count of Positive Integer and Negative Integer](./solutions/2529-maximum-count-of-positive-integer-and-negative-integer.js)|Easy|
969970
2535|[Difference Between Element Sum and Digit Sum of an Array](./solutions/2535-difference-between-element-sum-and-digit-sum-of-an-array.js)|Easy|
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/**
2+
* 2503. Maximum Number of Points From Grid Queries
3+
* https://leetcode.com/problems/maximum-number-of-points-from-grid-queries/
4+
* Difficulty: Hard
5+
*
6+
* You are given an m x n integer matrix grid and an array queries of size k.
7+
*
8+
* Find an array answer of size k such that for each integer queries[i] you start in the top left
9+
* cell of the matrix and repeat the following process:
10+
* - If queries[i] is strictly greater than the value of the current cell that you are in, then
11+
* you get one point if it is your first time visiting this cell, and you can move to any adjacent
12+
* cell in all 4 directions: up, down, left, and right.
13+
* - Otherwise, you do not get any points, and you end this process.
14+
*
15+
* After the process, answer[i] is the maximum number of points you can get. Note that for each
16+
* query you are allowed to visit the same cell multiple times.
17+
*
18+
* Return the resulting array answer.
19+
*/
20+
21+
/**
22+
* @param {number[][]} grid
23+
* @param {number[]} queries
24+
* @return {number[]}
25+
*/
26+
var maxPoints = function(grid, queries) {
27+
const rows = grid.length;
28+
const cols = grid[0].length;
29+
const result = new Array(queries.length);
30+
const sortedQueries = queries
31+
.map((value, index) => ({ value, index }))
32+
.sort((a, b) => a.value - b.value);
33+
const directions = [[1, 0], [0, 1], [-1, 0], [0, -1]];
34+
const queue = new MinPriorityQueue(([row, col]) => grid[row][col]);
35+
const visited = new Set();
36+
37+
queue.enqueue([0, 0]);
38+
visited.add('0,0');
39+
40+
let queryIndex = 0;
41+
let points = 0;
42+
while (queue.size()) {
43+
const [row, col] = queue.dequeue();
44+
const currentValue = grid[row][col];
45+
while (queryIndex < sortedQueries.length && currentValue >= sortedQueries[queryIndex].value) {
46+
result[sortedQueries[queryIndex].index] = points;
47+
queryIndex++;
48+
}
49+
if (queryIndex === sortedQueries.length) break;
50+
points++;
51+
for (const [rowOffset, colOffset] of directions) {
52+
const nextRow = row + rowOffset;
53+
const nextCol = col + colOffset;
54+
const positionKey = `${nextRow},${nextCol}`;
55+
if (nextRow >= 0 && nextRow < rows && nextCol >= 0 && nextCol < cols
56+
&& !visited.has(positionKey)) {
57+
visited.add(positionKey);
58+
queue.enqueue([nextRow, nextCol]);
59+
}
60+
}
61+
}
62+
while (queryIndex < sortedQueries.length) {
63+
result[sortedQueries[queryIndex].index] = points;
64+
queryIndex++;
65+
}
66+
67+
return result;
68+
};

0 commit comments

Comments
 (0)