Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions leetcode3/최원준/2305. Fair Distribution of Cookies.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#

'''
1. 아이디어 :
모든 경우의 수를 구할 수 있다.

2. 시간복잡도 :
O(n ** k)

3. 자료구조/알고리즘 :
백트래킹

'''
class Solution:
ans = float('inf')

def distributeCookies(self, cookies: list[int], k: int) -> int:
child_total = [0] * k

def backtrack(idx):
if idx == len(cookies):
self.ans = min(self.ans, max(child_total))
return

for i in range(k):
child_total[i] += cookies[idx]

if child_total[i] < self.ans:
backtrack(idx+1)

child_total[i] -= cookies[idx]

backtrack(0)
return self.ans
45 changes: 45 additions & 0 deletions leetcode3/최원준/988. Smallest String Starting From Leaf.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# 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. 아이디어 :
dfs를 통해 leaf까지 도달할때까지 char를 더합니다.
리턴조건은 4개:
- 리프 노드
- 왼쪽 노드만 존재
- 오른쪽 노드만 존재
- 중간 노드

2. 시간복잡도 :
O(n)

3. 자료구조/알고리즘 :
dfs

'''
class Solution:
def smallestFromLeaf(self, node: TreeNode | None, path: str = "") -> str:
if not node:
return ""

path = chr(97 + node.val) + path

if not node.left and not node.right:
return path

if not node.left:
return self.smallestFromLeaf(node.right, path)

if not node.right:
return self.smallestFromLeaf(node.left, path)

return min(
self.smallestFromLeaf(node.left, path),
self.smallestFromLeaf(node.right, path)
)
Loading