diff --git "a/leetcode3/\353\263\200\354\247\200\355\230\221/v2/1034. Coloring A Border.py" "b/leetcode3/\353\263\200\354\247\200\355\230\221/v2/1034. Coloring A Border.py" new file mode 100644 index 00000000..b038114c --- /dev/null +++ "b/leetcode3/\353\263\200\354\247\200\355\230\221/v2/1034. Coloring A Border.py" @@ -0,0 +1,39 @@ +from collections import deque + +class Solution: + def colorBorder(self, grid: List[List[int]], row: int, col: int, color: int) -> List[List[int]]: + queue = deque() + + m, n = len(grid), len(grid[0]) + + visited = [[0] * n for _ in range(m)] + tmp = [[0] * n for _ in range(m)] + + queue.append((col,row)) + c = grid[row][col] + dirs = [(0,1),(0,-1),(1,0),(-1,0)] + + + while True: + if len(queue) == 0: + break + + x,y = queue.popleft() + visited[y][x] = 1 + + if x == 0 or x == n-1 or y == 0 or y == m-1: + tmp[y][x] = 1 + elif not (grid[y+1][x] == c and grid[y-1][x] == c and grid[y][x+1] == c and grid[y][x-1] == c): + tmp[y][x] = 1 + + for dx,dy in dirs: + if 0 <= x + dx < n and 0 <= y + dy < m and visited[y+dy][x+dx] == 0 and grid[y+dy][x+dx] == c: + queue.append((x+dx,y+dy)) + + for y in range(m): + for x in range(n): + if tmp[y][x] == 1: + grid[y][x] = color + + return grid + \ No newline at end of file diff --git "a/leetcode3/\353\263\200\354\247\200\355\230\221/v2/3838. Weighted Word Mapping.py" "b/leetcode3/\353\263\200\354\247\200\355\230\221/v2/3838. Weighted Word Mapping.py" new file mode 100644 index 00000000..3451d4e2 --- /dev/null +++ "b/leetcode3/\353\263\200\354\247\200\355\230\221/v2/3838. Weighted Word Mapping.py" @@ -0,0 +1,27 @@ + +''' +1. 아이디어 : +alphabet dic, dic_rev 만들고, 이에 매핑해서 구한다. + +2. 시간복잡도 : + o(n * m) n: words 길이, m: word 길이 +3. 자료구조/알고리즘 : +''' +class Solution: + def mapWordWeights(self, words: List[str], weights: List[int]) -> str: + alphabet = 'abcdefghijklmnopqrstuvwxyz' + dic = {} + dic_rev = {} + wn = len(weights) + for i in range(wn): + dic[alphabet[i]] = weights[i] + dic_rev[25-i] = alphabet[i] + + ans = '' + for word in words: + tmp = sum([dic[w] for w in word]) + ans += dic_rev[tmp%26] + + return ans + + \ No newline at end of file