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:
grid[i][j] moves to grid[i][j + 1].grid[i][n - 1] moves to grid[i + 1][0].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.lengthn == grid[i].length1 <= m <= 501 <= n <= 50-1000 <= grid[i][j] <= 10000 <= k <= 100program 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
temp.f95:97:15:
97 | end
| 1
Error: END DO statement expected at (1)
f951: Error: Unexpected end of file in ‘temp.f95’
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
temp.f95:4:23:
4 | subroutine shift_grid(grid, m, n, k, result)
| 1
Error: MODULE attribute of ‘shift_grid’ conflicts with PROCEDURE attribute at (1)
temp.f95:5:34:
5 | integer, intent(in) :: m, n, k
| 1
Error: Unexpected data declaration statement in CONTAINS section at (1)
temp.f95:6:37:
6 | integer, intent(in) :: grid(m, n)
| 1
Error: Explicit array shape at (1) must be constant of INTEGER type and not UNKNOWN type
temp.f95:7:40:
7 | integer, intent(out) :: result(m, n)
| 1
Error: Explicit array shape at (1) must be constant of INTEGER type and not UNKNOWN type
temp.f95:9:19:
9 | integer :: i, j
| 1
Error: Unexpected data declaration statement in CONTAINS section at (1)
temp.f95:11:15:
11 | do i = 1, m
| 1
Error: Unexpected DO statement in CONTAINS section at (1)
temp.f95:12:17:
12 | do j = 1, n
| 1
Error: Unexpected DO statement in CONTAINS section at (1)
temp.f95:13:37:
13 | if (i == m .and. j == n) then
| 1
Error: Unexpected block IF statement in CONTAINS section at (1)
temp.f95:14:35:
14 | result(1, 1) = grid(i, j)
| 1
Error: Unexpected assignment statement in CONTAINS section at (1)
temp.f95:15:41:
15 | else if (i == m .and. j < n) then
| 1
Error: Unexpected ELSE IF statement in CONTAINS section at (1)
temp.f95:16:39:
16 | result(1, j + 1) = grid(i, j)
| 1
Error: Unexpected assignment statement in CONTAINS section at (1)
temp.f95:17:41:
17 | else if (i < m .and. j == n) then
| 1
Error: Unexpected ELSE IF statement in CONTAINS section at (1)
temp.f95:18:39:
18 | result(i + 1, 1) = grid(i, j)
| 1
Error: Unexpected assignment statement in CONTAINS section at (1)
temp.f95:19:12:
19 | else
| 1
Error: Unexpected ELSE statement in CONTAINS section at (1)
temp.f95:20:39:
20 | result(i, j + 1) = grid(i, j)
| 1
Error: Unexpected assignment statement in CONTAINS section at (1)
temp.f95:21:11:
21 | end if
| 1
Error: Expecting END MODULE statement at (1)
temp.f95:22:9:
22 | end do
| 1
Error: Expecting END MODULE statement at (1)
temp.f95:23:7:
23 | end do
| 1
Error: Expecting END MODULE statement at (1)
temp.f95:25:19:
25 | do i = 1, k - 1
| 1
Error: Unexpected DO statement in CONTAINS section at (1)
temp.f95:26:35:
26 | call shift_once(result, m, n)
| 1
Error: Unexpected CALL statement in CONTAINS section at (1)
temp.f95:27:7:
27 | end do
| 1
Error: Expecting END MODULE statement at (1)
temp.f95:28:5:
28 | end subroutine shift_grid
| 1
Error: Expecting END MODULE statement at (1)
temp.f95:30:31:
30 | subroutine shift_once(grid, m, n)
| 1
Error: Symbol ‘m’ at (1) has no IMPLICIT type
temp.f95:30:34:
30 | subroutine shift_once(grid, m, n)
| 1
Error: Symbol ‘n’ at (1) has no IMPLICIT type
temp.f95:51:7:
51 | use shift_grid
| 1
Fatal Error: Cannot open module file ‘shift_grid.mod’ for reading at (1): No such file or directory
compilation terminated.
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
daysInMonth representing the days in each month, with a special case for February in leap years.daysInMonth array up to the given month, accumulating the days in each month.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;
}
daysInMonth representing the days in each month, with a special case for February in leap years.daysInMonth array up to the given month, accumulating the days in each month.