-
Notifications
You must be signed in to change notification settings - Fork 119
/
Copy pathCyclically Rotating a Grid.cpp
71 lines (61 loc) · 2.13 KB
/
Cyclically Rotating a Grid.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
// Runtime: 58 ms (Top 33.33%) | Memory: 13.5 MB (Top 31.11%)
const vector<pair<int, int>> directions{{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
class Solution
{
protected:
int n, m;
int whichLayer(int x, int y) {
if (x < 0 || y < 0 || x >= n || y >= m) return -1;
int i = min(x, n - x - 1);
int j = min(y, m - y - 1);
return min(i, j);
}
void nextPos(int x, int y, int dir, int& nx, int& ny) {
nx = x + directions[dir].first;
ny = y + directions[dir].second;
}
// Go to next position in the current layer (in clockwise order)
void advancePosition(int& x, int& y, int& d) {
int nx, ny;
nextPos(x, y, d, nx, ny);
if (whichLayer(nx, ny) != whichLayer(x, y)) {
// Change direction
d = (d + 1) % 4;
nextPos(x, y, d, nx, ny);
}
x = nx; y = ny;
}
public:
vector<vector<int>> rotateGrid(vector<vector<int>>& grid, int k)
{
// Check validity of the arguments
if (grid.empty()) throw invalid_argument("empty grid");
m = grid.size();
n = grid[0].size();
if (n == 0) throw invalid_argument("empty grid");
if (k < 0) throw invalid_argument("negative k not accepted");
// Trivial case
if (k == 0) return grid;
const int L = min(n, m) / 2;
for (int l = 0; l < L; l++) {
vector<int> v;
// Flatten a layer from grid into a vector
int j = l, i = l; // start position
int d = 0; // direction
do {
v.push_back(grid[j][i]);
advancePosition(i, j, d);
} while (!(i == l && j == l)); // until the start position is reached
// Unflatten rotated vector back into the grid
j = l; i = l; // start position
d = 0; // direction
int off = k % v.size();
do {
grid[j][i] = v[off];
off = (off + 1) % v.size();
advancePosition(i, j, d);
} while (!(i == l && j == l)); // until the start position is reached
}
return grid;
}
};