diff --git "a/leetcode3/\353\263\200\354\247\200\355\230\221/v4/1914. Cyclically Rotating a Grid.py" "b/leetcode3/\353\263\200\354\247\200\355\230\221/v4/1914. Cyclically Rotating a Grid.py" new file mode 100644 index 00000000..d8066499 --- /dev/null +++ "b/leetcode3/\353\263\200\354\247\200\355\230\221/v4/1914. Cyclically Rotating a Grid.py" @@ -0,0 +1,63 @@ +''' +시간 초과 +''' +''' +0,0 -> 0,1 +0,1 -> 0,2 +... +0,4 -> 0,5 +0,5 -> 1,5 +1,5 -> 2,5 +2,5 -> 3,5 +3,5 -> 3,4 +''' +class Solution: + def rotate_one(self, grid: List[List[int]]) -> List[List[int]]: + xlen = len(grid[0]) + ylen = len(grid) + vertexs = [(0,0),(0,ylen-1),(xlen-1,ylen-1),(xlen-1,0)] + rotate_num = min(xlen,ylen) // 2 + tmp = [[0] * xlen for _ in range(ylen)] + + while rotate_num > 0: + # print(rotate_num) + # print(vertexs) + + for y in range(vertexs[0][1],vertexs[1][1]): + tmp[y+1][vertexs[0][0]] = grid[y][vertexs[0][0]] + + # print(tmp) + + for x in range(vertexs[1][0],vertexs[2][0]): + tmp[vertexs[1][1]][x+1] = grid[vertexs[1][1]][x] + + # print(tmp) + + for y in range(vertexs[2][1],vertexs[3][1],-1): + tmp[y-1][vertexs[2][0]] = grid[y][vertexs[2][0]] + + # print(tmp) + + for x in range(vertexs[3][0],vertexs[0][0],-1): + tmp[vertexs[3][1]][x-1] = grid[vertexs[3][1]][x] + + tmp2 = [ + (vertexs[0][0]+1,vertexs[0][1]+1), + (vertexs[1][0]+1,vertexs[1][1]-1), + (vertexs[2][0]-1,vertexs[2][1]-1), + (vertexs[3][0]-1,vertexs[3][1]+1) + ] + vertexs = tmp2 + rotate_num -= 1 + return tmp + + def rotateGrid(self, grid: List[List[int]], k: int) -> List[List[int]]: + tmp = grid + + xlen = len(grid[0]) + ylen = len(grid) + + while k > 0: + tmp = self.rotate_one(tmp) + k-=1 + return tmp \ No newline at end of file diff --git "a/leetcode3/\354\206\241\354\243\274\355\230\234/1914. Cyclically Rotating a Grid" "b/leetcode3/\354\206\241\354\243\274\355\230\234/1914. Cyclically Rotating a Grid" new file mode 100644 index 00000000..7b290f92 --- /dev/null +++ "b/leetcode3/\354\206\241\354\243\274\355\230\234/1914. Cyclically Rotating a Grid" @@ -0,0 +1,28 @@ +class Solution: + def rotateGrid(self, grid: List[List[int]], k: int) -> List[List[int]]: + m = len(grid) + n = len(grid[0]) + + minLen = min(m, n) + + for i in range(minLen //2): + for j in range(k): # k만큼 돌기 + tmp = grid[i][i] + + for k in range(i, n-1): # <- + grid[i][k] = grid[i][k+1] + for k in range(i, m-1): # ^ + grid[k][n-1] = grid[k+1][n-1] + for k in range(n-1, i, -1): # -> + grid[m-1][k] = grid[m-1][k-1] + for k in range(m-1, i, -1): + grid[k][i] = grid[k-1][i] + + grid[i+1][i] = tmp + + return grid + + +/** +수정해야함 +**/ diff --git "a/leetcode3/\354\206\241\354\243\274\355\230\234/442. Find All Duplicates in an Array" "b/leetcode3/\354\206\241\354\243\274\355\230\234/442. Find All Duplicates in an Array" new file mode 100644 index 00000000..8b121e72 --- /dev/null +++ "b/leetcode3/\354\206\241\354\243\274\355\230\234/442. Find All Duplicates in an Array" @@ -0,0 +1,16 @@ +class Solution: + def findDuplicates(self, nums: list[int]) -> list[int]: + ans = [] + numbers = {} + + for i in nums: + if i in numbers: + numbers[i] += 1 + else: + numbers[i] = 1 + + for i in numbers: + if numbers[i] ==2: + ans.append(i) + + return ans diff --git "a/leetcode3/\354\206\241\354\243\274\355\230\234/copy" "b/leetcode3/\354\206\241\354\243\274\355\230\234/copy" new file mode 100644 index 00000000..917c84d4 --- /dev/null +++ "b/leetcode3/\354\206\241\354\243\274\355\230\234/copy" @@ -0,0 +1,13 @@ +# + +''' +1. 아이디어 : + + +2. 시간복잡도 : + O() + +3. 자료구조/알고리즘 : + + +''' diff --git "a/leetcode3/\354\230\244\354\235\200\355\230\201/1861. Rotating the Box" "b/leetcode3/\354\230\244\354\235\200\355\230\201/1861. Rotating the Box" new file mode 100644 index 00000000..bd99fc3e --- /dev/null +++ "b/leetcode3/\354\230\244\354\235\200\355\230\201/1861. Rotating the Box" @@ -0,0 +1,11 @@ +class Solution: + def rotateTheBox(self, grid: list[list[str]]) -> list[list[str]]: + + + for y in range(len(grid)) + board=[[] for _ in range(len(grid[0]))] + for y in grid: + for i in range(len(y)): + board[i].append(y[i]) + + diff --git "a/leetcode3/\354\230\244\354\235\200\355\230\201/1914. Cyclically Rotating a Grid" "b/leetcode3/\354\230\244\354\235\200\355\230\201/1914. Cyclically Rotating a Grid" new file mode 100644 index 00000000..22c6a60d --- /dev/null +++ "b/leetcode3/\354\230\244\354\235\200\355\230\201/1914. Cyclically Rotating a Grid" @@ -0,0 +1,46 @@ +from collections import deque +class Solution: + def rotate(self,grid,x,y,xn,yn,k): + dq=deque([]) + cur_x=x + cur_y=y + while cur_x=x: + dq.append((cur_y,cur_x)) + cur_x-=1 + cur_x+=1 + cur_y-=1 + while cur_y>y: + dq.append((cur_y,cur_x)) + cur_y-=1 + return dq + temp=yn*xn-(yn-2)*(xn-2) + k=k%temp + + if k=yn and k List[List[int]]: + return self.rotate(grid,0,0,len(grid[0]),len(grid),k) diff --git "a/leetcode3/\354\230\244\354\235\200\355\230\201/442. Find All Duplicates in an Array" "b/leetcode3/\354\230\244\354\235\200\355\230\201/442. Find All Duplicates in an Array" new file mode 100644 index 00000000..69fd4676 --- /dev/null +++ "b/leetcode3/\354\230\244\354\235\200\355\230\201/442. Find All Duplicates in an Array" @@ -0,0 +1,33 @@ +class Solution: + def c(self,nums,t): + + if isinstance(nums[t],int): + temp=nums[t] + nums[t]='1' + self.c(nums,temp-1) + elif nums[t]=='.': + nums[t]='1' + else: + nums[t]=str(int(nums[t])+1) + + def findDuplicates(self, nums: list[int]) -> list[int]: + ans=[] + for i in range(len(nums)): + if isinstance(nums[i],int): + if nums[i]==i+1: + nums[i]='1' + else: + temp=nums[i] + nums[i]='.' + self.c(nums,temp-1) + + + else:continue + + for i in range(len(nums)): + if nums[i]=='2': + ans.append(i+1) + return ans + + + diff --git "a/leetcode3/\354\230\244\354\235\200\355\230\201/copy" "b/leetcode3/\354\230\244\354\235\200\355\230\201/copy" new file mode 100644 index 00000000..917c84d4 --- /dev/null +++ "b/leetcode3/\354\230\244\354\235\200\355\230\201/copy" @@ -0,0 +1,13 @@ +# + +''' +1. 아이디어 : + + +2. 시간복잡도 : + O() + +3. 자료구조/알고리즘 : + + +''' diff --git "a/leetcode3/\354\235\264\354\247\204\355\235\254/1914. Cyclically Rotating a Grid.java" "b/leetcode3/\354\235\264\354\247\204\355\235\254/1914. Cyclically Rotating a Grid.java" new file mode 100644 index 00000000..63a24f35 --- /dev/null +++ "b/leetcode3/\354\235\264\354\247\204\355\235\254/1914. Cyclically Rotating a Grid.java" @@ -0,0 +1,70 @@ +/* + +1. 아이디어 : 방문 배열과 이동 방향을 미리 정의 후, i,i부터 아래 -> 오른쪽 -> 위 -> 왼쪽 경로의 원소 위치 수집 + k번 만큼 회전(이동)한 위치에 넣는다 - 모듈러 연산 필요 + + +2. 시간복잡도 : O(2*N*M) + +3. 자료구조/알고리즘 : 방향 전환, 배열 + + */ + +class Solution { + private boolean[][] visited; + private int[][] ans; + private int[] dy = {0,1,0,-1}; + private int[] dx = {-1,0,1,0}; + public int[][] rotateGrid(int[][] grid, int k) { + // m, n은 짝수 + // k 횟수 만큼 왼쪽 회전 + + visited = new boolean[grid.length][grid[0].length]; + ans = new int[grid.length][grid[0].length]; + int limit = Math.min(grid.length, grid[0].length)/2; + for(int i=0; i list = new ArrayList<>(); + visited[y][x] = true; + list.add(new int[]{y,x}); + + int startY = y; + int startX = x; + + // 아래 방향 + int dir = 1; + while(true) { + int ny = y+dy[dir]; + int nx = x+dx[dir]; + + if(ny == startY && nx == startX) break; + + if(ny<0 || nx<0 || ny>=grid.length || nx>=grid[0].length || visited[ny][nx]) { + dir=(dir+1)%4; + continue; + } + + y = ny; + x = nx; + + visited[y][x] = true; + list.add(new int[]{y,x}); + } + + int size = list.size(); + int move = k%size; + + for(int i=0; i findDuplicates(int[] nums) { + Listans = new ArrayList<>(); + + for(int i=0; i 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 + + + diff --git "a/leetcode3/\354\265\234\354\233\220\354\244\200/442. Find All Duplicates in an Array.py" "b/leetcode3/\354\265\234\354\233\220\354\244\200/442. Find All Duplicates in an Array.py" new file mode 100644 index 00000000..d38e9d84 --- /dev/null +++ "b/leetcode3/\354\265\234\354\233\220\354\244\200/442. Find All Duplicates in an Array.py" @@ -0,0 +1,19 @@ +class Solution: + def findDuplicates(self, nums: list[int]) -> list[int]: + return [num for num, count in Counter(nums).items() if count == 2] + + + +class Solution: + def findDuplicates(self, nums: list[int]) -> list[int]: + result = [] + + for num in nums: + idx = abs(num) - 1 + + if nums[idx] < 0: + result.append(abs(num)) + else: + nums[idx] *= -1 + + return result