Algorithm(56)
-
104. Maximum Depth of Binary Tree
class Solution: def maxDepth(self, root: Optional[TreeNode]) -> int: def dfs(node): if not node: return 0 l = dfs(node.left) r = dfs(node.right) return max(l, r) + 1 return dfs(root)
2025.10.05 -
417. Pacific Atlantic Water Flow
class Solution: def pacificAtlantic(self, heights: List[List[int]]) -> List[List[int]]: def dfs(q, a): while q: y, x, cur_height = q.popleft() for ny, nx in [(y, x + 1), (y, x - 1), (y + 1, x), (y - 1, x)]: if not (0
2025.10.05 -
flood fill
class Solution: def floodFill(self, image: List[List[int]], sr: int, sc: int, color: int) -> List[List[int]]: def dfs(y, x, color, base_color): if image[y][x] == color or image[y][x] != base_color: return image[y][x] = color for oy, ox in [(-1, 0), (1, 0), (0, -1), (0, 1)]: if not (0
2025.10.05 -
3100. Water Bottles II
numBottles와 numExchange가 주어짐 numBottles 개수 만큼의 병을 모두 마시고 empty 개수를 마신만큼 증가 시키며 마신 것의 개수를 증가 시킬 수 있음 혹은 empty 개수가 numExchange 이상인 경우 numExchange 개수 만큼의 empty 개수로 numBottles 하나를 증가하고 numExchange도 하나 증가할 수 있음 이렇게 해서 마신 개수가 최대가 되는 값을 찾기 class Solution: def maxBottlesDrunk(self, numBottles: int, numExchange: int) -> int: def dfs(full, empty, exchange, drunk, mx): if full == 0 and empty < exchange: mx..
2024.04.05 -
2717. Semi-Ordered Permutation
func swap(val1 *int, val2 *int) { temp := *val1 *val1 = *val2 *val2 = temp } func semiOrderedPermutation(nums []int) int { i := 0 one_idx := -1 n_idx := -1 cnt := 0 for idx, val := range nums { if val == 1 { one_idx = idx } } i = one_idx for i > 0 { swap(&nums[i - 1], &nums[i]) cnt += 1 i -= 1 } for idx, val := range nums { if val == len(nums) { n_idx = idx } } i = n_idx for i < len(nums) - 1 { ..
2023.06.05 -
2541. Minimum Operations to Make Array Equal II
two arrays and integer value k are provided. the thing you need to do is to figure out the minumum number of changes by applying operation. the operation is as follows. in the first array, nums1 pick two indices i, j apply nums1[i] + k, nums1[j] - k and check the nums1 and the nums2 are equal, if so, then return the number of operations until then. Python class Solution: def minOperations(self, ..
2023.01.29