返回介绍

solution / 1200-1299 / 1260.Shift 2D Grid / README_EN

发布于 2024-06-17 01:03:21 字数 5486 浏览 0 评论 0 收藏 0

1260. Shift 2D Grid

中文文档

Description

Given a 2D grid of size m x n and an integer k. You need to shift the grid k times.

In one shift operation:

  • Element at grid[i][j] moves to grid[i][j + 1].
  • Element at grid[i][n - 1] moves to grid[i + 1][0].
  • Element at grid[m - 1][n - 1] moves to grid[0][0].

Return the _2D grid_ after applying shift operation k times.

 

Example 1:

Input: grid = [[1,2,3],[4,5,6],[7,8,9]], k = 1
Output: [[9,1,2],[3,4,5],[6,7,8]]

Example 2:

Input: grid = [[3,8,1,9],[19,7,2,5],[4,6,11,10],[12,0,21,13]], k = 4
Output: [[12,0,21,13],[3,8,1,9],[19,7,2,5],[4,6,11,10]]

Example 3:

Input: grid = [[1,2,3],[4,5,6],[7,8,9]], k = 9
Output: [[1,2,3],[4,5,6],[7,8,9]]

 

Constraints:

  • m == grid.length
  • n == grid[i].length
  • 1 <= m <= 50
  • 1 <= n <= 50
  • -1000 <= grid[i][j] <= 1000
  • 0 <= k <= 100

Solutions

Solution 1: Flattening the 2D Array

According to the problem description, if we flatten the 2D array into a 1D array, then each shift operation is to move the elements in the array one position to the right, with the last element moving to the first position of the array.

Therefore, we can flatten the 2D array into a 1D array, then calculate the final position $idx = (x, y)$ of each element, and update the answer array ans[x][y] = grid[i][j].

The time complexity is $O(m \times n)$, where $m$ and $n$ are the number of rows and columns in the grid array, respectively. We need to traverse the grid array once to calculate the final position of each element. Ignoring the space consumption of the answer array, the space complexity is $O(1)$.

class Solution:
  def shiftGrid(self, grid: List[List[int]], k: int) -> List[List[int]]:
    m, n = len(grid), len(grid[0])
    ans = [[0] * n for _ in range(m)]
    for i, row in enumerate(grid):
      for j, v in enumerate(row):
        x, y = divmod((i * n + j + k) % (m * n), n)
        ans[x][y] = v
    return ans
class Solution {
  public List<List<Integer>> shiftGrid(int[][] grid, int k) {
    int m = grid.length, n = grid[0].length;
    List<List<Integer>> ans = new ArrayList<>();
    for (int i = 0; i < m; ++i) {
      List<Integer> row = new ArrayList<>();
      for (int j = 0; j < n; ++j) {
        row.add(0);
      }
      ans.add(row);
    }
    for (int i = 0; i < m; ++i) {
      for (int j = 0; j < n; ++j) {
        int idx = (i * n + j + k) % (m * n);
        int x = idx / n, y = idx % n;
        ans.get(x).set(y, grid[i][j]);
      }
    }
    return ans;
  }
}
class Solution {
public:
  vector<vector<int>> shiftGrid(vector<vector<int>>& grid, int k) {
    int m = grid.size(), n = grid[0].size();
    vector<vector<int>> ans(m, vector<int>(n));
    for (int i = 0; i < m; ++i) {
      for (int j = 0; j < n; ++j) {
        int idx = (i * n + j + k) % (m * n);
        int x = idx / n, y = idx % n;
        ans[x][y] = grid[i][j];
      }
    }
    return ans;
  }
};
func shiftGrid(grid [][]int, k int) [][]int {
  m, n := len(grid), len(grid[0])
  ans := make([][]int, m)
  for i := range ans {
    ans[i] = make([]int, n)
  }
  for i := 0; i < m; i++ {
    for j := 0; j < n; j++ {
      idx := (i*n + j + k) % (m * n)
      x, y := idx/n, idx%n
      ans[x][y] = grid[i][j]
    }
  }
  return ans
}
function shiftGrid(grid: number[][], k: number): number[][] {
  const [m, n] = [grid.length, grid[0].length];
  const ans: number[][] = Array.from({ length: m }, () => Array.from({ length: n }, () => 0));
  for (let i = 0; i < m; ++i) {
    for (let j = 0; j < n; ++j) {
      const idx = (i * n + j + k) % (m * n);
      const [x, y] = [Math.floor(idx / n), idx % n];
      ans[x][y] = grid[i][j];
    }
  }
  return ans;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文