forked from neetcode-gh/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1260-shift-2d-grid.cpp
More file actions
26 lines (24 loc) · 840 Bytes
/
1260-shift-2d-grid.cpp
File metadata and controls
26 lines (24 loc) · 840 Bytes
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
class Solution {
public:
vector<vector<int>> shiftGrid(vector<vector<int>>& grid, int k) {
const int M = grid.size(), N = grid[0].size();
auto posToVal = [&] (int r, int c) -> int {
return r * N + c;};
auto valToPos = [&] (int v) -> int* {
return new int[] {v / N, v % N};};
vector<vector<int>> res;
for(int r = 0; r < M; r++) {
vector<int> row;
for(int c = 0; c < N; c++)
row.push_back(0);
res.push_back(row);
}
for(int r = 0; r < M; r++)
for(int c = 0; c < N; c++) {
int newVal = (posToVal(r, c) + k) % (M * N);
int *newRC = valToPos(newVal);
res[newRC[0]][newRC[1]] = grid[r][c];
}
return res;
}
};