diff --git "a/leetcode3/\354\265\234\354\233\220\354\244\200/1861. Rotating the Box.py" "b/leetcode3/\354\265\234\354\233\220\354\244\200/1861. Rotating the Box.py" new file mode 100644 index 00000000..3cb39f32 --- /dev/null +++ "b/leetcode3/\354\265\234\354\233\220\354\244\200/1861. Rotating the Box.py" @@ -0,0 +1,61 @@ +# + +''' +1. 아이디어 : +- 돌을 오른쪽으로 미는 메서드 +오른쪽으로 밀때는 장애물을 만나기전까지 돌을 센 후, 장애물을 만나거나 끝까지 가면 다시 뒤로 가며 돌로 채운다. + +- 90도로 회전하는 메서드 + +2. 시간복잡도 : + O(n * m * m) + +3. 자료구조/알고리즘 : +- + +''' +class Solution: + def rotateTheBox(self, boxGrid: list[list[str]]) -> list[list[str]]: + n = len(boxGrid) + m = len(boxGrid[0]) + + def push_right(box): + for row in box: + stones = 0 + for i in range(m): + cell = row[i] + if cell == "#": + stones += 1 + row[i] = "." + elif cell == ".": + pass + elif cell == "*": + for j in range(i-1, i-1-stones,-1): + row[j] = "#" + stones = 0 + for i in range(m-1, m-1-stones, -1): + row[i] = "#" + return box + + boxGrid_push = push_right(boxGrid) + + + def rotate_90(box): + boxGrid90 = [[[""] for _ in range(n)] for _ in range(m)] + for i in range(n): + for j in range(m): + boxGrid90[j][n-i-1] = box[i][j] + return boxGrid90 + + return rotate_90(boxGrid_push) + + """ + [["#",".","*","."] + ["#","#","*","."]] + [ + [[''], ['']], + [[''], ['']], + [[''], ['']], + [[''], ['']] + ] + """ diff --git "a/leetcode3/\354\265\234\354\233\220\354\244\200/1914. Cyclically Rotating a Grid.py" "b/leetcode3/\354\265\234\354\233\220\354\244\200/1914. Cyclically Rotating a Grid.py" new file mode 100644 index 00000000..fd84c5d0 --- /dev/null +++ "b/leetcode3/\354\265\234\354\233\220\354\244\200/1914. Cyclically Rotating a Grid.py" @@ -0,0 +1,63 @@ +# + +''' +1. 아이디어 : +가장 바깥쪽 layer부터 [0,0], [1,1], ...[n,n]원소를 가장 처음으로 원을 그리며 숫자와 위치를 각 배열에 저장. +배열에 저장된 숫자의 offset을 계산해서 offset이 위치하는 곳부터 가장 처음 원을 시작한 곳부터 채운다. + +2. 시간복잡도 : + O( min(n, m)/2 * n * m * 2) + +3. 자료구조/알고리즘 : +- + +''' +class Solution: + def rotateGrid(self, grid: List[List[int]], k: int) -> List[List[int]]: + n = len(grid) + m = len(grid[0]) + max_layer = min(n//2, m//2) + layers = {} + + dx = [0,1,0,-1] + dy = [1,0,-1,0] + + for layer in range(max_layer): + pos =[] + nums = [] + + x, y = layer, layer + direction = 0 + + top = layer + left = layer + bottom = n - layer - 1 + right = m - layer - 1 + + length = 2 * (bottom - top + right - left) + + for _ in range(length): + pos.append((x, y)) + nums.append(grid[x][y]) + + nx = x + dx[direction] + ny = y + dy[direction] + + if not (top <= nx <= bottom and left <= ny <= right): + direction = (direction + 1 ) % 4 + nx = x + dx[direction] + ny = y + dy[direction] + + x, y = nx, ny + + offset = k % length + nums = nums[offset:] + nums[:offset] + + for i in range(len(pos)): + x, y = pos[i] + grid[x][y] = nums[i] + + return grid + + +