From 6b70f408c291880f3bd4dbada3a32088ddbb9629 Mon Sep 17 00:00:00 2001 From: SinnoLn Date: Thu, 12 Nov 2026 22:44:53 +0900 Subject: [PATCH 1/2] 2305. Fair Distribution of Cookies --- .../2305. Fair Distribution of Cookies.java" | 45 +++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 "leetcode3/\354\235\264\354\247\204\355\235\254/2305. Fair Distribution of Cookies.java" diff --git "a/leetcode3/\354\235\264\354\247\204\355\235\254/2305. Fair Distribution of Cookies.java" "b/leetcode3/\354\235\264\354\247\204\355\235\254/2305. Fair Distribution of Cookies.java" new file mode 100644 index 00000000..39ab8839 --- /dev/null +++ "b/leetcode3/\354\235\264\354\247\204\355\235\254/2305. Fair Distribution of Cookies.java" @@ -0,0 +1,45 @@ +/* + +1. 아이디어 : 모든 쿠기를 k개로 분배하며 최소 차이가 나는 값 중 가장 큰 값을 구한다 + 쿠기가 8개밖에 없으므로 백트랙킹으로 전부 구해본다 + +2. 시간복잡도 : O(K^n) + +3. 자료구조/알고리즘 : 그리디, 커스텀 정렬 + + */ + +class Solution { + private int ans = 1000000; + public int distributeCookies(int[] cookies, int k) { + // 모든 쿠키 분배 + // 차이가 최소가 되어야 한다 + // 쿠키는 최대 8개 + + int[] children = new int[k]; + backtrack(cookies, children, 0); + + return ans; + } + + private void backtrack(int[] cookies, int[] children, int idx) { + if(idx == cookies.length) { + int max = 0; + for(int c : children) max = Math.max(max, c); + + ans = Math.min(ans, max); + return; + } + + int curMax = 0; + for(int c : children) curMax = Math.max(curMax, c); + + if(curMax >= ans) return; + + for(int i=0; i Date: Thu, 12 Nov 2026 22:47:21 +0900 Subject: [PATCH 2/2] 988. Smallest String Starting From Leaf --- .... Smallest String Starting From Leaf.java" | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 "leetcode3/\354\235\264\354\247\204\355\235\254/988. Smallest String Starting From Leaf.java" diff --git "a/leetcode3/\354\235\264\354\247\204\355\235\254/988. Smallest String Starting From Leaf.java" "b/leetcode3/\354\235\264\354\247\204\355\235\254/988. Smallest String Starting From Leaf.java" new file mode 100644 index 00000000..dc2c919a --- /dev/null +++ "b/leetcode3/\354\235\264\354\247\204\355\235\254/988. Smallest String Starting From Leaf.java" @@ -0,0 +1,40 @@ +/* +1. 아이디어: root부터 타고 들어가는 그리디 방식은 사전순 배열된 최소 root를 구할 수 없다 + 백트랙킹으로 가지치기 해나가며 값을 구한다 + +2. 시간복잡도: O(Tree 높이 * 노드 수) + +3. 자료구조/알고리즘: 백트랙킹 + +*/ + +class Solution { + private String ans; + public String smallestFromLeaf(TreeNode root) { + // 1. 사전순, 2. 문자열 길이가 짧을 것 + // root 기준 + StringBuilder sb = new StringBuilder(); + + backtrack(root, sb); + return ans; + } + + private void backtrack(TreeNode node, StringBuilder sb) { + if(node == null) return; + + sb.append((char)(node.val+'a')); + + if(node.left == null && node.right == null) { + String tmp = sb.reverse().toString(); + + if(ans == null || tmp.compareTo(ans) < 0) ans = tmp; + + sb.reverse(); + } + + backtrack(node.left, sb); + backtrack(node.right, sb); + + sb.deleteCharAt(sb.length()-1); + } +} \ No newline at end of file