-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Expand file tree
/
Copy pathfill-a-special-grid.cpp
More file actions
55 lines (51 loc) · 1.58 KB
/
fill-a-special-grid.cpp
File metadata and controls
55 lines (51 loc) · 1.58 KB
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
49
50
51
52
53
54
55
// Time: O(4^n)
// Space: O(1)
// array
class Solution {
public:
vector<vector<int>> specialGrid(int n) {
const int total = 1 << n;
vector<vector<int>> result(total, vector<int>(total));
const auto& copy = [&](int l, int r1, int c1, int r2, int c2) {
for (int i = 0; i < l; ++i) {
for (int j = 0; j < l; ++j) {
result[r2 + i][c2 + j] = result[r1 + i][c1 + j] + l * l;
}
}
};
for (int i = 0, l = 1; i < n; ++i, l <<= 1) {
int r = 0, c = total - l;
for (const auto& [dr, dc] : vector<pair<int, int>>{{l, 0}, {0, -l}, {-l, 0}}) {
const int nr = r + dr, nc = c + dc;
copy(l, r, c, nr, nc);
tie(r, c) = pair(nr, nc);
}
}
return result;
}
};
// Time: O(4^n)
// Space: O(n)
// divide and conquer
class Solution2 {
public:
vector<vector<int>> specialGrid(int n) {
const int total = 1 << n;
vector<vector<int>> result(total, vector<int>(total));
int idx = 0;
const function<void (int, int, int)> divide_and_conquer = [&](int l, int r, int c) {
if (l == 1) {
result[r][c] = idx++;
return;
}
l >>= 1;
for (const auto& [dr, dc] : vector<pair<int, int>>{{0, l}, {l, 0}, {0, -l}, {-l, 0}}) {
r += dr;
c += dc;
divide_and_conquer(l, r, c);
}
};
divide_and_conquer(total, 0, 0);
return result;
}
};