Skip to content

Commit 6b74e8c

Browse files
authored
Create 2503. Maximum Number of Points From Grid Queries (#754)
2 parents 9449866 + c250cd4 commit 6b74e8c

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

Diff for: 2503. Maximum Number of Points From Grid Queries

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
class Solution {
2+
vector<pair<int, int>> dirs = {{-1, 0}, {1, 0}, {0, 1}, {0, -1}};
3+
public:
4+
vector<int> maxPoints(vector<vector<int>>& grid, vector<int>& queries) {
5+
int n = grid.size(), m = grid[0].size(), k = queries.size();
6+
vector<pair<int, int>> qi;
7+
for (int i = 0; i < k; i++){
8+
qi.push_back(make_pair(queries[i], i));
9+
}
10+
sort(qi.begin(), qi.end());
11+
priority_queue<tuple<int, int, int>, vector<tuple<int, int, int>>, greater<>> pq;
12+
vector<vector<bool>> visited(n, vector<bool>(m, false));
13+
pq.push(make_tuple(grid[0][0], 0, 0));
14+
visited[0][0] = true;
15+
vector<int> res(k, 0);
16+
int count = 0;
17+
for (auto& q : qi){
18+
int limit = q.first, idx = q.second;
19+
while (!pq.empty() && get<0>(pq.top()) < limit){
20+
tuple<int, int, int> cur = pq.top();
21+
pq.pop();
22+
int wei = get<0>(cur), row = get<1>(cur), col = get<2>(cur);
23+
count++;
24+
for (auto& dir : dirs){
25+
int dr = dir.first + row, dc = dir.second + col;
26+
if (dr >= 0 && dr < n && dc >= 0 && dc < m && !visited[dr][dc]){
27+
visited[dr][dc] = true;
28+
pq.push(make_tuple(grid[dr][dc], dr, dc));
29+
}
30+
}
31+
}
32+
res[idx] = count;
33+
}
34+
return res;
35+
}
36+
};

0 commit comments

Comments
 (0)