Leetcode - Shift 2D Grid (Python)

preview_player
Показать описание
Leetcode - Shift 2D Grid #1260
Difficulty: Easy
Рекомендации по теме
Комментарии
Автор

Woah you're back! I had no idea. Glad to see you're still doing the daily challenges when you can man!

I did this slightly differently. Since k*rows*cols, was small i just swapped in place starting with the bottom right

class Solution:
def shiftGrid(self, grid: List[List[int]], k: int) -> List[List[int]]:
rows = len(grid)
cols = len(grid[0])

for _ in range(k):
prev = grid[-1][-1]

for row in range(rows):
for col in range(cols):
#save what is curretnly there
temp = grid[row][col]
grid[row][col] = prev
prev = temp

return grid

swapping in place use modular arithmetic was pretty tricky! it took me to a bit to realize we swapped for every multiple of (j+ k) % N. we'd eventually hit the original i we started off with, which is why we advanced it i += 1

janmichaelaustria
join shbcf.ru