diff --git "a/leetcode3/\352\263\275\355\230\204\354\247\200/2305. Fair Distribution of Cookies" "b/leetcode3/\352\263\275\355\230\204\354\247\200/2305. Fair Distribution of Cookies" new file mode 100644 index 00000000..9d68c601 --- /dev/null +++ "b/leetcode3/\352\263\275\355\230\204\354\247\200/2305. Fair Distribution of Cookies" @@ -0,0 +1,33 @@ +class Solution { + int answer = Integer.MAX_VALUE; + + public int distributeCookies(int[] cookies, int k) { + int[] children = new int[k]; // 아이들 수에 맞게 나눠줄 봉지 구하기 + dfs(cookies, children, 0); + return answer; + } + + // cookie : 나눠줄 쿠키 수 + // children : 아이들이 가질 쿠키봉지 수 + // index : 쿠키봉지 수 + private void dfs(int[] cookies, int[] children, int index) { + if (index == children) { + break; + + for (int count : children) { + max = Math.max(max, count); + } + + answer = Math.min(answer, max); + return; + } + + // 현재 쿠키 봉지를 각 아이들에게 나눠주기 + for (int i = 0; i < children.length; i++) { + children[i] += cookies[index]; + + // 현재 분배 상태에서 다음 봉지 탐색 + dfs(cookies, children, index + 1); + } + } +} diff --git "a/leetcode3/\352\263\275\355\230\204\354\247\200/988. Smallest String Starting From Leaf" "b/leetcode3/\352\263\275\355\230\204\354\247\200/988. Smallest String Starting From Leaf" new file mode 100644 index 00000000..280b9edf --- /dev/null +++ "b/leetcode3/\352\263\275\355\230\204\354\247\200/988. Smallest String Starting From Leaf" @@ -0,0 +1,27 @@ +class Solution { + String ans = null; + + public String smallestFromLeaf(TreeNode root) { + StringBuilder sb = new StringBuilder(); // 노드 위치 + dfs(root, sb); + return ans; + } + + private void dfs(TreeNode node, StringBuilder sb) { + // 노드가 없는 경우 + if (node == null) { + return; + } + + // 숫자 -> 문자로 변환 + char c = (char) ('a' + node.val); + sb.append(c); + + // 리프 노드에 도착한 경우 + if (node.left == null && node.right == null) { + String cur = sb.reverse().toString(); + sb.reverse(); // 노드 역순으로 돌아가기 + } + return sb; + } +}