forked from liuyubobobo/Play-Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
42 lines (32 loc) · 912 Bytes
/
main.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
/// Source : https://leetcode.com/contest/weekly-contest-163/problems/shift-2d-grid/
/// Author : liuyubobobo
/// Time : 2019-11-16
#include <iostream>
#include <vector>
using namespace std;
/// Brute Force
/// Time Complexity: O(k * m * n)
/// Space Complexity: O(1)
class Solution {
public:
vector<vector<int>> shiftGrid(vector<vector<int>>& grid, int k) {
int n = grid.size(), m = grid[0].size();
vector<vector<int>> res = grid;
while(k --)
res = go(res, n, m);
return res;
}
private:
vector<vector<int>> go(const vector<vector<int>>& g, int n, int m){
vector<vector<int>> res(n, vector<int>(m));
for(int i = 0; i < n; i ++){
res[i][0] = g[(i - 1 + n) % n][m - 1];
for(int j = 1; j < m; j ++)
res[i][j] = g[i][j - 1];
}
return res;
}
};
int main() {
return 0;
}