Shift 2D Grid - Array - Easy - LeetCode
💻 coding

Shift 2D Grid - Array - Easy - LeetCode

3 min read 587 words
3 min read
ShareWhatsAppPost on X
  • 1The problem involves shifting a 2D grid k times, with specific rules for element movement.
  • 2Shifting elements involves moving them to the right, with overflow moving to the next row or wrapping to the first row.
  • 3The solution has a time complexity of O(n^2) and a space complexity of O(n) for storing the result.

AI-generated summary · May not capture all nuances

Key Insight
AskGif

"The problem involves shifting a 2D grid k times, with specific rules for element movement."

Shift 2D Grid - Array - Easy - LeetCode

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

Solution:

using System;
using System.Collections.Generic;
using System.Text;

namespace LeetCode.AskGif.Easy.Array
{
 public class ShiftGridSoln
 {
 public IList<IList<int>> ShiftGrid(int[][] grid, int k)
 {
 var res = new List<IList<int>>();

 //initialize list with dummy values
 for (int i = 0; i < grid.Length; i++)
 {
 var row = new List<int>();
 for (int j = 0; j < grid[0].Length; j++)
 {
 row.Add(0);
 }
 res.Add(row);
 }

 for (int i = 0; i < grid.Length; i++)
 {
 for (int j = 0; j < grid[0].Length; j++)
 {
 int[] pos = GenerateNewPosition(grid.Length, grid[0].Length, i, j, k);
 res[pos[0]][pos[1]] = grid[i][j];
 }
 }

 return res;
 }

 private int[] GenerateNewPosition(int row, int column, int i, int j, int k)
 {
 if(j+k > column-1)
 {
 i = i + ((j + k) / column);
 j = (j + k) % column; 
 i = i % row;
 }
 else
 {
 j = j + k;
 }

 return new int[] { i, j };
 }
 }
}

Time Complexity: O(n^2)

Space Complexity: O(n) To Store Result

Unit Tests:

using LeetCode.AskGif.Easy.Array;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using System.Text;

namespace CodingUnitTest.Easy.Array
{
 [TestClass]
 public class ShiftGridSolnTests
 {
 [TestMethod]
 public void ShiftGridSoln_First()
 {
 var arr = new int[,] {
 { 1, 2, 3 },
 { 4, 5, 6 },
 { 7, 8, 9 }
 };
 var k = 1;
 var output = new int[,] {
 { 9, 1, 2 },
 { 3, 4, 5 },
 { 6, 7, 8 }
 };

 var res = new ShiftGridSoln().ShiftGrid(ArrayMapper(arr), k);

 AreEqual(output, res);
 }

 [TestMethod]
 public void ShiftGridSoln_Second()
 {
 var arr = new int[,] {
 { 1, 2, 3 },
 { 4, 5, 6 },
 { 7, 8, 9 }
 };
 var k = 9;
 var output = new int[,] {
 { 1, 2, 3 },
 { 4, 5, 6 },
 { 7, 8, 9 }
 };

 var res = new ShiftGridSoln().ShiftGrid(ArrayMapper(arr), k);

 AreEqual(output, res);
 }

 [TestMethod]
 public void ShiftGridSoln_Third()
 {
 var arr = new int[,] {
 { 3, 8, 1, 9 },
 { 19, 7, 2, 5 },
 { 4, 6, 11, 10 },
 { 12, 0, 21, 13 }
 };
 var k = 4;
 var output = new int[,] {
 { 12, 0, 21, 13 },
 { 3, 8, 1, 9 },
 { 19, 7, 2, 5 },
 { 4, 6, 11, 10 }
 };

 var res = new ShiftGridSoln().ShiftGrid(ArrayMapper(arr), k);

 AreEqual(output, res);
 }

 private void AreEqual(int[,] output, IList<IList<int>> res)
 {
 for (int i = 0; i < res.Count; i++)
 {
 for (int j = 0; j < res[i].Count; j++)
 {
 Assert.AreEqual(output[i, j], res[i][j]);
 }
 }
 }

 private int[][] ArrayMapper(int[,] matrix)
 {
 var arr = new int[matrix.GetLength(0)][];
 for (int i = 0; i < matrix.GetLength(0); i++)
 {
 arr[i] = new int[matrix.GetLength(1)];
 for (int j = 0; j < matrix.GetLength(1); j++)
 {
 arr[i][j] = matrix[i, j];
 }
 }

 return arr;
 }
 }
}

Enjoyed this article?

Share it with someone who'd find it useful.

ShareWhatsAppPost on X

AskGif

Published on 11 June 2020 · 3 min read · 587 words

Part of AskGif Blog · coding

You might also like

Shift 2D Grid - Array - Easy - LeetCode | AskGif Blog