forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
k-highest-ranked-items-within-a-price-range.cpp
47 lines (45 loc) · 1.83 KB
/
k-highest-ranked-items-within-a-price-range.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
// Time: O(m * n + klogk)
// Space: O(m * n)
// bfs, quick select
class Solution {
public:
vector<vector<int>> highestRankedKItems(vector<vector<int>>& grid, vector<int>& pricing, vector<int>& start, int k) {
static const vector<pair<int, int>> directions{{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
vector<pair<int, int>> q = {{start[0], start[1]}};
vector<vector<int>> lookup(size(grid), vector<int>(size(grid[0]), -1));
int d = lookup[start[0]][start[1]] = 0;
auto compare = [&](const auto& x, const auto& y) {
return make_tuple(lookup[x[0]][x[1]], grid[x[0]][x[1]], x[0], x[1]) <
make_tuple(lookup[y[0]][y[1]], grid[y[0]][y[1]], y[0], y[1]);
};
vector<vector<int>> result;
while (!empty(q)) {
if (size(result) >= k) {
if (size(result) > k) {
nth_element(begin(result), begin(result) + k, end(result), compare);
result.resize(k);
}
break;
}
vector<pair<int, int>> new_q;
for (const auto& [r, c] : q) {
if (pricing[0] <= grid[r][c] && grid[r][c] <= pricing[1]) {
result.push_back({r, c});
}
for (const auto& [dr, dc] : directions) {
const int nr = r + dr, nc = c + dc;
if (!((0 <= nr && nr < size(grid)) && (0 <= nc && nc < size(grid[0])) &&
grid[nr][nc] && lookup[nr][nc] == -1)) {
continue;
}
lookup[nr][nc] = d + 1;
new_q.emplace_back(nr, nc);
}
}
q = move(new_q);
++d;
}
sort(begin(result), end(result), compare);
return result;
}
};