← Home

1260. Shift 2D Grid

LeetCode article · C++ solution
Website made by wuisabel-gif · Original C++ code by keineahnung2345
straightforward implementationC++Markdown
126

Let's make this one less mysterious. For 1260. Shift 2D Grid, the solution in this repository is mainly a straightforward implementation solution.

Guide

What?

Before optimizing anything, pin down what information is still useful after each move. Instead of trying to be clever immediately, read the code as a sequence of questions:

  • What state are we keeping?
  • How do we move from one state to the next?
  • When do we know the answer is already determined?

For this file, the main tools are: straightforward implementation.

Guide

When?

Reach for this shape when a direct simulation would work logically but waste time revisiting the same information. The accepted code reduces that pressure by storing exactly the information that remains useful later.

The solution is organized around the main LeetCode entry point and a few local helpers.

Guide

Why?

The solution works because it narrows the problem until every update has a clear reason to exist.

  • The two-dimensional vector is the memory of the solution: grid state, DP state, or adjacency shape.
  • The final return is not magic; it is the invariant after the loops or recursion have finished doing their accounting.

Guide

How?

Walk through the solution in this order:

  1. Initialize the memory or helper structure.
  2. Process candidates in the order the invariant expects.
  3. Update the answer only when the current state is valid.
  4. Return the value that represents the fully processed input.

The most important competitive-programming habit here is to trust the invariant. Once the invariant is right, the loops become much less scary.

Guide

Complexity

  • Time: O(m*n), space: O(m*n)
  • Space: O(n) in the usual case for auxiliary containers or recursion

Guide

C++ Solution

Your submission

The accepted solution

solution.cpp
01//Runtime: 88 ms, faster than 23.45% of C++ online submissions for Shift 2D Grid.
02//Memory Usage: 14.4 MB, less than 100.00% of C++ online submissions for Shift 2D Grid.
03class Solution {
04public:
05    vector<vector<int>> shiftGrid(vector<vector<int>>& grid, int k) {
06        int m = grid.size(), n = grid[0].size();
07        vector<vector<pair<int, int>>> pos(m, vector<pair<int, int>>(n));
08        vector<vector<int>> ans(m, vector<int>(n));
09        
10        for(int i = 0; i < m; i++){
11            for(int j = 0; j < n; j++){
12                pos[i][j] = make_pair(i, j);
13            }
14        }
15        
16        //calculate each index is shifted to where
17        for(int time = 0; time < k; time++){
18            // cout << "time: " << time << endl;
19            for(int i = 0; i < m; i++){
20                for(int j = 0; j < n; j++){
21                    // cout << pos[i][j].first << ", " << pos[i][j].second << endl;
22                    if(pos[i][j].second < n-1){
23                        //(i,j)->(i,j+1)
24                        pos[i][j].second += 1;
25                    }else if(pos[i][j].first < m-1){
26                        //(i,n-1)->(i+1,0)
27                        pos[i][j] = make_pair(pos[i][j].first+1, 0);
28                    }else{
29                        //(m-1,n-1) -> (0,0)
30                        pos[i][j] = make_pair(0,0);
31                    }
32                }
33            }
34        }
35        
36        //(pos[i][j].first, pos[i][j].second): new position of original (i,j)
37        for(int i = 0; i < m; i++){
38            for(int j = 0; j < n; j++){
39                //(i, j) now move to (pos[i][j].first, pos[i][j].second)
40                ans[pos[i][j].first][pos[i][j].second] = grid[i][j];
41            }
42        }
43        
44        return ans;
45    }
46};
47
48//Approach 3: Using Modulo Arithmetic
49//time: O(m*n), space: O(m*n)
50//Runtime: 68 ms, faster than 75.78% of C++ online submissions for Shift 2D Grid.
51//Memory Usage: 13.4 MB, less than 100.00% of C++ online submissions for Shift 2D Grid.
52class Solution {
53public:
54    vector<vector<int>> shiftGrid(vector<vector<int>>& grid, int k) {
55        int m = grid.size(), n = grid[0].size();
56        vector<vector<int>> ans(m, vector<int>(n, 0));
57        
58        for(int i = 0 ; i < m; i++){
59            for(int j = 0; j < n; j++){
60                int new_j = (j + k)%n;
61                int new_i = (i + (j + k)/n)%m;
62                ans[new_i][new_j] = grid[i][j];
63            }
64        }
65        
66        return ans;
67    }
68};

Cost

Complexity

Time
O(m*n), space: O(m*n)
Dominated by the main traversal, recursion, or data-structure operations in the code.
Space
O(n) in the usual case for auxiliary containers or recursion
Auxiliary state plus the answer structure where the problem requires one.