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

Note: This problem is from LeetCode.
Compiled
Executed
Correct
program main
    implicit none

    integer, parameter :: m = 3, n = 3
    integer, dimension(m, n) :: grid
    integer :: i, j, k

    ! Example 1
    grid = reshape([1, 2, 3, 4, 5, 6, 7, 8, 9], shape=[m, n])
    k = 1
    call shift(grid, k)
    write(*, "(A)") "Example 1:"
    do i = 1, m
        write(*, "(A)", advance="no") "["
        do j = 1, n
            write(*, "(I2)", advance="no") grid(i, j)
            if (j < n) then
                write(*, "(A)", advance="no") ", "
            end if
        end do
        write(*, "(A)") "]"
        if (i < m) then
            write(*, "(A)") ""
        end if
    end do

    ! Example 2
    grid = reshape([3, 8, 1, 9, 19, 7, 2, 5, 4, 6, 11, 10, 12, 0, 21, 13], &
                   shape=[m, n])
    k = 4
    call shift(grid, k)
    write(*, "(A)") "Example 2:"
    do i = 1, m
        write(*, "(A)", advance="no") "["
        do j = 1, n
            write(*, "(I2)", advance="no") grid(i, j)
            if (j < n) then
                write(*, "(A)", advance="no") ", "
            end if
        end do
        write(*, "(A)") "]"
        if (i < m) then
            write(*, "(A)") ""
        end if
    end do

    ! Example 3
    grid = reshape([1, 2, 3, 4, 5, 6, 7, 8, 9], shape=[m, n])
    k = 9
    call shift(grid, k)
    write(*, "(A)") "Example 3:"
    do i = 1, m
        write(*, "(A)", advance="no") "["
        do j = 1, n
            write(*, "(I2)", advance="no") grid(i, j)
            if (j < n) then
                write(*, "(A)", advance="no") ", "
            end if
        end do
        write(*, "(A)") "]"
        if (i < m) then
            write(*, "(A)") ""
        end if
    end do

contains

    subroutine shift(grid, k)
        implicit none
        integer, dimension(:, :), intent(inout) :: grid
        integer, intent(in) :: k
        integer :: i, j, temp

        do while (k > 0)
            ! Shift elements in each row
            do i = 1, size(grid, 1)
                ! Move element at grid[i][j] to grid[i][j + 1]
                if (j < size(grid, 2)) then
                    temp = grid(i, j)
                    grid(i, j) = grid(i, j + 1)
                    grid(i, j + 1) = temp
                end if

                ! Move element at grid[i][n - 1] to grid[i + 1][0]
                if (i < size(grid, 1) .and. j == size(grid, 2)) then
                    temp = grid(i, j)
                    grid(i, j) = grid(i + 1, 0)
                    grid(i + 1, 0) = temp
                end if

                ! Move element at grid[m - 1][n - 1] to grid[0][0]
                if (i == size(grid, 1) .and. j == size(grid, 2)) then
                    temp = grid(i, j)
                    grid(i, j) = grid(0, 0)
                    grid(0, 0) = temp
                end if
            end
Compiled
Executed
Correct
module shift_grid
  implicit none
contains
  subroutine shift_grid(grid, m, n, k, result)
    integer, intent(in) :: m, n, k
    integer, intent(in) :: grid(m, n)
    integer, intent(out) :: result(m, n)

    integer :: i, j

    do i = 1, m
      do j = 1, n
        if (i == m .and. j == n) then
          result(1, 1) = grid(i, j)
        else if (i == m .and. j < n) then
          result(1, j + 1) = grid(i, j)
        else if (i < m .and. j == n) then
          result(i + 1, 1) = grid(i, j)
        else
          result(i, j + 1) = grid(i, j)
        end if
      end do
    end do

    do i = 1, k - 1
      call shift_once(result, m, n)
    end do
  end subroutine shift_grid

  subroutine shift_once(grid, m, n)
    integer, intent(inout) :: grid(m, n)
    integer :: i, j

    do i = 1, m
      do j = 1, n
        if (i == m .and. j == n) then
          grid(1, 1) = grid(i, j)
        else if (i == m .and. j < n) then
          grid(1, j + 1) = grid(i, j)
        else if (i < m .and. j == n) then
          grid(i + 1, 1) = grid(i, j)
        else
          grid(i, j + 1) = grid(i, j)
        end if
      end do
    end do
  end subroutine shift_once
end module shift_grid

program test_shift_grid
  use shift_grid
  implicit none

  integer, parameter :: m = 3
  integer, parameter :: n = 3
  integer, parameter :: k = 1
  integer :: grid(m, n) = reshape((/ 1, 2, 3, 4, 5, 6, 7, 8, 9 /), shape=(/ m, n /))
  integer :: result(m, n)

  call shift_grid(grid, m, n, k, result)

  write (*, *) "Expected:"
  write (*, *) "9 1 2"
  write (*, *) "3 4 5"
  write (*, *) "6 7 8"
  write (*, *) "Actual:"
  write (*, *) result
end program test_shift_grid
🌐 Data from online sources
def dayOfYear(date: str) -> int:
    year, month, day = map(int, date.split('-'))

    daysInMonth = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
    if year % 4 == 0 and (year % 100 != 0 or year % 400 == 0):
        daysInMonth[2] = 29

    dayOfYear = sum(daysInMonth[:month]) + day
    return dayOfYear
  1. Parse the input string to extract the year, month, and day as integers.
  2. Create an array daysInMonth representing the days in each month, with a special case for February in leap years.
  3. Iterate through the daysInMonth array up to the given month, accumulating the days in each month.
  4. Add the given day to the accumulated days to get the day number of the year.
  5. Return the resulting day number of the year.
🌐 Data from online sources
int dayOfYear(std::string date) {
    int year = stoi(date.substr(0, 4));
    int month = stoi(date.substr(5, 2));
    int day = stoi(date.substr(8, 2));

    int daysInMonth[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    if (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0)) {
        daysInMonth[2] = 29;
    }

    int dayOfYear = 0;
    for (int i = 1; i < month; ++i) {
        dayOfYear += daysInMonth[i];
    }
    dayOfYear += day;

    return dayOfYear;
}
  1. Parse the input string to extract the year, month, and day as integers.
  2. Create an array daysInMonth representing the days in each month, with a special case for February in leap years.
  3. Iterate through the daysInMonth array up to the given month, accumulating the days in each month.
  4. Add the given day to the accumulated days to get the day number of the year.
  5. Return the resulting day number of the year.