Blogs Hub

Shift 2D Grid - Array - Easy - LeetCode - MiniTV

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;
        }
    }
}