From a440515753853f4588fb9066911c0b19b51011e0 Mon Sep 17 00:00:00 2001 From: std-freejia Date: Wed, 13 Aug 2025 16:38:02 +0900 Subject: [PATCH 1/3] feat: week04 merge-two-sorted-lists --- merge-two-sorted-lists/std-freejia.java | 46 +++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 merge-two-sorted-lists/std-freejia.java diff --git a/merge-two-sorted-lists/std-freejia.java b/merge-two-sorted-lists/std-freejia.java new file mode 100644 index 0000000000..5c4adf3714 --- /dev/null +++ b/merge-two-sorted-lists/std-freejia.java @@ -0,0 +1,46 @@ +/** + * 고생한 이유: ListNode 의 시작점을 따로 둬야 하는 것! + * + * ListNode dummy = new ListNode(0); // 시작점 + * ListNode output = dummy; // 시작점 뒤에 실제 작업용 포인터 + * + * + * Definition for singly-linked list. + * public class ListNode { + * int val; + * ListNode next; + * ListNode() {} + * ListNode(int val) { this.val = val; } + * ListNode(int val, ListNode next) { this.val = val; this.next = next; } + * } + */ +class Solution { + public static ListNode mergeTwoLists(ListNode list1, ListNode list2) { + // 길이가 둘다 null 이면, return null + if (list1 == null && list2 == null) return null; + + ListNode dummy = new ListNode(0); // 시작점 + ListNode output = dummy; // 시작점 뒤에 실제 작업용 포인터 + + while(list1 != null && list2 != null) { + + if (list1.val < list2.val) { + output.next = list1; + list1 = list1.next; // list1 포인터 이동 + } else { + output.next = list2; + list2 = list2.next; // list2 포인터 이동 + } + output = output.next; // output 포인터 이동 + } + + if (list1 != null) { + output.next = list1; + } + if (list2 != null) { + output.next = list2; + } + + return dummy.next; + } +} From f4cc68050e542cd4a3a5e826064bc7b6a0046ea4 Mon Sep 17 00:00:00 2001 From: std-freejia Date: Wed, 13 Aug 2025 22:15:47 +0900 Subject: [PATCH 2/3] feat: week04 maximum-depth-of-binary-tree --- maximum-depth-of-binary-tree/std-freejia.java | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 maximum-depth-of-binary-tree/std-freejia.java diff --git a/maximum-depth-of-binary-tree/std-freejia.java b/maximum-depth-of-binary-tree/std-freejia.java new file mode 100644 index 0000000000..dc1694c3bd --- /dev/null +++ b/maximum-depth-of-binary-tree/std-freejia.java @@ -0,0 +1,29 @@ +/** + * 최대 depth 까지 가봐야 하므로 DFS 탐색 이용 + * + * 모든 노드를 순회해야 하므로 시간복잡도 O(N) + * 재귀 호출 스택 프레임 (= 트리 최대 높이 height) 만큼 공간복잡도 O(H) + * + * Definition for a binary tree node. + * public class TreeNode { + * int val; + * TreeNode left; + * TreeNode right; + * TreeNode() {} + * TreeNode(int val) { this.val = val; } + * TreeNode(int val, TreeNode left, TreeNode right) { + * this.val = val; + * this.left = left; + * this.right = right; + * } + * } + */ +class Solution { + public int maxDepth(TreeNode root) { + if (root == null) return 0; // 자식이 없으면 종료 + + int left = maxDepth(root.left); + int right = maxDepth(root.right); + return Math.max(left, right) + 1; // 루트는 기본 1층이니까 1 더한다 + } +} From 3c9a30f34a010cea4edad111144b917b3843baa0 Mon Sep 17 00:00:00 2001 From: std-freejia Date: Fri, 15 Aug 2025 18:21:45 +0900 Subject: [PATCH 3/3] feat: coin change --- coin-change/std-freejia.java | 54 ++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 coin-change/std-freejia.java diff --git a/coin-change/std-freejia.java b/coin-change/std-freejia.java new file mode 100644 index 0000000000..9a54243bac --- /dev/null +++ b/coin-change/std-freejia.java @@ -0,0 +1,54 @@ +/** + * BFS 는 너비 우선이므로, 가장 얕은 깊이로 목표 금액을 만족하면 '최소 동전 개수'를 사용한 것입니다. + * 너비 == 사용한 동전 개수 + * + * Queue 동전 금액 '합' 을 관리합니다 + * + * 목표 금액이 amount 일 때, + * 0 부터 amount 까지의 합을 체크할 boolean[] 을 활용합니다. + * 합계 금액이 '이미 만들어본 합' 이면 boolean[] 에 체크하여 중복방문을 방지하기 위함. + * + */ +class Solution { + public int coinChange(int[] coins, int amount) { + if (amount == 0) return 0; // 목표 금액 0인 경우, '0개' 리턴 + + Arrays.sort(coins); // 작은 동전부터 더해본다 + + // 이미 만들어본 '합' 은 지나가도록. + boolean[] visitedAmount = new boolean[amount + 1]; + // 합 + Queue amountQueue = new ArrayDeque<>(); + + amountQueue.add(0); // 시작 금액 0 + visitedAmount[0] = true; + + int coinCount = 0; // 사용한 동전 개수 + + while(!amountQueue.isEmpty()) { + coinCount++; // 사용한 동전 개수 1개 증가 + + int queueSize = amountQueue.size(); + + for(int i = 0; i < queueSize; i++) { + // 현재 금액 합 + int currentAmount = amountQueue.poll(); + + // 모든 코인을 더해본다 + for(int coin : coins) { + // 코인을 더해 본 합. + int tempAmount = currentAmount + coin; + + if (tempAmount == amount) return coinCount; // 최소 동전 개수 찾음 + + // 처음 만든 합 이고, 목표 금액보다 적은 합이라면, + if (tempAmount < amount && !visitedAmount[tempAmount]) { + visitedAmount[tempAmount] = true; + amountQueue.add(tempAmount); + } + } + } + } + return -1; + } +}