Rotate a Grid in Row-Major Order. Shift grid to the right k times. Each shift moves an element one column right; the last column continues into the next row's first column, and the final cell wraps to the first cell.
### Problem
Shift grid to the right k times. Each shift moves an element one column right; the last column continues into the next row's first column, and the final cell wraps to the first cell. Return the resulting grid with the same dimensions.
```cpp
class Solution {
public:
vector<vector<int>> shiftGrid(vector<vector<int>>& grid, int k);
};
```
### Examples
**Example 1:**
```text
Input: grid = [[1,2],[3,4]], k = 2
Output: [[3,4],[1,2]]
Explanation: A two-place cyclic rotation of1,2,3,4 puts3,4 first.
```
**Example 2:**
```text
Input: grid = [[5,6,7]], k = 4
Output: [[7,5,6]]
Explanation: Four shifts on three cells are equivalent to one shift.
```
### Constraints
- grid has m rows and n columns with `1 <= m,n <= 50`.
- Every entry is an integer in `[-1000,1000]`.
- k is an integer from 0 through 100.
### Contract considerations
Input dimensions and all values are retained; this is one row-major cycle rather than separate row rotations.
```hint Read the boundary condition
Where does a last-column value move, and what happens when the final cell is shifted?
```
### Boundary reminders
Input dimensions and all values are retained; this is one row-major cycle rather than separate row rotations.
Overview: Rotate a Grid in Row-Major Order. Shift grid to the right k times. Each shift moves an element one column right; the last column continues into the next row's first column, and the final cell wraps to the first cell.
Shift grid to the right k times. Each shift moves an element one column right; the last column continues into the next row's first column, and the final cell wraps to the first cell. Return the resulting grid with the same dimensions.
class Solution {
public:
vector<vector<int>> shiftGrid(vector<vector<int>>& grid, int k);
};
Examples
Example 1:
Input: grid = [[1,2],[3,4]], k = 2
Output: [[3,4],[1,2]]
Explanation: A two-place cyclic rotation of1,2,3,4 puts3,4 first.
Example 2:
Input: grid = [[5,6,7]], k = 4
Output: [[7,5,6]]
Explanation: Four shifts on three cells are equivalent to one shift.
Constraints
grid has m rows and n columns with
1 <= m,n <= 50
.
Every entry is an integer in
[-1000,1000]
.
k is an integer from 0 through 100.
Contract considerations
Input dimensions and all values are retained; this is one row-major cycle rather than separate row rotations.
Boundary reminders
Input dimensions and all values are retained; this is one row-major cycle rather than separate row rotations.