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
33 changes: 33 additions & 0 deletions leetcode3/곽현지/2305. Fair Distribution of Cookies
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
27 changes: 27 additions & 0 deletions leetcode3/곽현지/988. Smallest String Starting From Leaf
Original file line number Diff line number Diff line change
@@ -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;
}
}
Loading