분류 전체보기(224)
-
[LeetCode] Keys and Rooms (Python)
보호되어 있는 글입니다.
2024.03.09 -
[LeetCods] 01Matrix (Python)
from collections import deque class Solution: def updateMatrix(self, mat: List[List[int]]) -> List[List[int]]: row, col = len(mat), len(mat[0]) dx = [0, 0, 1, -1] dy = [1, -1, 0, 0] INF = 1e9 q = deque() for i in range(row): for j in range(col): if mat[i][j] == 0: q.append((i, j)) else : mat[i][j] = INF while q: x, y = q.popleft() for i in range(4): nx = x + dx[i] ny = y + dy[i] z = mat[x][y] + ..
2024.03.09 -
[LeetCode] Flood Fill (Python)
from collections import deque class Solution: def floodFill(self, image: List[List[int]], sr: int, sc: int, color: int) -> List[List[int]]: before = image[sr][sc] row, col = len(image), len(image[0]) dx = [0, 0, 1, -1] dy = [1, -1, 0, 0] if before != color: q = deque() q.append([sr, sc]) while q: x, y = q.popleft() image[x][y] = color for i in range(4): nx = x + dx[i] ny = y + dy[i] if 0
2024.03.09 -
[LeetCode] Decode String (Python)
보호되어 있는 글입니다.
2024.03.08 -
[LeetCode] Implement Stack using Queues (Python)
from collections import deque class MyStack: def __init__(self): self.stack1 = deque() self.stack2 = deque() def push(self, x: int) -> None: self.stack1.append(x) def pop(self) -> int: while len(self.stack1) > 1: temp = self.stack1.popleft() self.stack2.append(temp) if len(self.stack1) == 1: answer = self.stack1.popleft() self.stack1, self.stack2 = self.stack2, self.stack1 return answer def top(..
2024.03.08 -
[LeetCode] Implement Queue using Stacks (Python)
class MyQueue: def __init__(self): self.stack_input = [] self.stack_output = [] def push(self, x: int) -> None: self.stack_input.append(x) def pop(self) -> int: length = len(self.stack_input) - 1 for _ in range(length): self.stack_output.append(self.stack_input.pop()) front = self.stack_input.pop() for _ in range(length): self.stack_input.append(self.stack_output.pop()) return front def peek(sel..
2024.03.08