-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
Copy pathdesign-neighbor-sum-service.cpp
48 lines (41 loc) · 1.25 KB
/
design-neighbor-sum-service.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
48
// Time: ctor: O(n^2)
// adjacentSum: O(1)
// diagonalSum: O(1)
// Space: O(n^2)
// hash table
class neighborSum {
public:
neighborSum(vector<vector<int>>& grid)
: grid_(grid)
, lookup_(size(grid) * size(grid[0]))
{
for (int i = 0; i < size(grid); ++i) {
for (int j = 0; j < size(grid[0]); ++j) {
lookup_[grid[i][j]] = {i, j};
}
}
}
int adjacentSum(int value) {
static const vector<pair<int, int>> ADJACENTS{{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
return sum(value, ADJACENTS);
}
int diagonalSum(int value) {
static const vector<pair<int, int>> DIAGONALS{{1, 1}, {1, -1}, {-1, 1}, {-1, -1}};
return sum(value, DIAGONALS);
}
private:
int sum(int value, const auto& directions) {
const auto& [i, j] = lookup_[value];
int total = 0;
for (const auto& [di, dj] : directions) {
const int ni = i + di, nj = j + dj;
if (!(0 <= ni && ni < size(grid_) && 0 <= nj && nj < size(grid_[0]))) {
continue;
}
total += grid_[ni][nj];
}
return total;
}
vector<vector<int>> grid_;
vector<pair<int, int>> lookup_;
};