From 78eaafdfdbc6af6cae59e6794c378dd58a9b430b Mon Sep 17 00:00:00 2001 From: Won Joon Thomas Choi <113500771+724thomas@users.noreply.github.com> Date: Thu, 1 Oct 2026 22:47:59 +0900 Subject: [PATCH 1/2] Create 543. Diameter of Binary Tree.py --- .../543. Diameter of Binary Tree.py" | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 "leetcode3/\354\265\234\354\233\220\354\244\200/543. Diameter of Binary Tree.py" diff --git "a/leetcode3/\354\265\234\354\233\220\354\244\200/543. Diameter of Binary Tree.py" "b/leetcode3/\354\265\234\354\233\220\354\244\200/543. Diameter of Binary Tree.py" new file mode 100644 index 00000000..4037db92 --- /dev/null +++ "b/leetcode3/\354\265\234\354\233\220\354\244\200/543. Diameter of Binary Tree.py" @@ -0,0 +1,35 @@ +# Definition for a binary tree node. +# class TreeNode: +# def __init__(self, val=0, left=None, right=None): +# self.val = val +# self.left = left +# self.right = right +# + +''' +1. 아이디어 : +- 현재 노드를 포함한게 가장 길수도 있기에 왼쪽/오른쪽 중 깊은것 리턴 +- 현재 노드 자식들을 계산한게 가장 길수도 있기에 왼쪽 깊이 + 오른쪽 깊이를 리턴 + + +2. 시간복잡도 : + O(n) + +3. 자료구조/알고리즘 : +dfs + +''' + +class Solution: + def diameterOfBinaryTree(self, root: Optional[TreeNode]) -> int: + + def max_length(node): #deepest, longest + if not node: + return 0, 0 + + left_deepest, left_longest = max_length(node.left) + right_deepest, right_longest = max_length(node.right) + + return max(left_deepest, right_deepest) + 1, max(left_longest, right_longest, left_deepest + right_deepest) + + return max_length(root)[1] From 83223d885ada8950d8176d49787c61a049105f42 Mon Sep 17 00:00:00 2001 From: Won Joon Thomas Choi <113500771+724thomas@users.noreply.github.com> Date: Thu, 1 Oct 2026 22:48:28 +0900 Subject: [PATCH 2/2] Create 1219. Path with Maximum Gold --- .../1219. Path with Maximum Gold" | 59 +++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 "leetcode3/\354\265\234\354\233\220\354\244\200/1219. Path with Maximum Gold" diff --git "a/leetcode3/\354\265\234\354\233\220\354\244\200/1219. Path with Maximum Gold" "b/leetcode3/\354\265\234\354\233\220\354\244\200/1219. Path with Maximum Gold" new file mode 100644 index 00000000..0ced48ab --- /dev/null +++ "b/leetcode3/\354\265\234\354\233\220\354\244\200/1219. Path with Maximum Gold" @@ -0,0 +1,59 @@ +# + +''' +1. 아이디어 : +모든 선택은 상태값을 들고 있어야한다. +재귀 백트래킹을 통해 롤백. + +2. 시간복잡도 : + O(4 ** 25) + +3. 자료구조/알고리즘 : +dfs, backtracking + +''' +class Solution: + def getMaximumGold(self, grid: list[list[int]]) -> int: + n = len(grid) + m = len(grid[0]) + # n <= 15 + # 비싼쪽으로X 다익스트라X 백트래킹? 4**25 압축? + + dx = [0,0,1,-1] + dy = [1,-1,0,0] + + def dfs(row, col): + gold = grid[row][col] + grid[row][col] = 0 # + + max_gold = 0 + for i in range(4): + nx = row + dx[i] + ny = col + dy[i] + + if 0<= nx