From b5ce480a3263ba3ad00c4f5ec6ca97123b659f49 Mon Sep 17 00:00:00 2001 From: Tessa1217 Date: Tue, 3 Jun 2025 01:17:38 +0900 Subject: [PATCH 1/6] add invert binary tree solution --- invert-binary-tree/Tessa1217.java | 36 +++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 invert-binary-tree/Tessa1217.java diff --git a/invert-binary-tree/Tessa1217.java b/invert-binary-tree/Tessa1217.java new file mode 100644 index 000000000..b04b3181a --- /dev/null +++ b/invert-binary-tree/Tessa1217.java @@ -0,0 +1,36 @@ +/** + * 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; + * } + * } + */ +/** + * 이진 트리의 root가 주어질 때 이진 트리를 뒤바꾼 후 root를 반환하세요. + */ +class Solution { + public TreeNode invertTree(TreeNode root) { + + if (root == null) { + return root; + } + + TreeNode current = root; + + // 좌우 변경 + TreeNode temp = current.left; + current.left = invertTree(current.right); + current.right = invertTree(temp); + + return root; + } +} + From a197b763a0d82a8c8def68db46d0ef7dbb1174db Mon Sep 17 00:00:00 2001 From: Tessa1217 Date: Fri, 6 Jun 2025 19:19:57 +0900 Subject: [PATCH 2/6] add search in rotated sorted array solution --- search-in-rotated-sorted-array/Tessa1217.java | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 search-in-rotated-sorted-array/Tessa1217.java diff --git a/search-in-rotated-sorted-array/Tessa1217.java b/search-in-rotated-sorted-array/Tessa1217.java new file mode 100644 index 000000000..8ccbee7fc --- /dev/null +++ b/search-in-rotated-sorted-array/Tessa1217.java @@ -0,0 +1,35 @@ +class Solution { + + // 이진 탐색: 시간복잡도: O(log n) + public int search(int[] nums, int target) { + + int left = 0; + int right = nums.length - 1; + + while (left <= right) { + + int mid = left + (right - left)/2; + + if (nums[mid] == target) { + return mid; + } + + // 구간 설정 + if (nums[left] <= nums[mid]) { + if (nums[left] <= target && target < nums[mid]) { + right = mid - 1; + } else { + left = mid + 1; + } + } else { + if (nums[mid] < target && target <= nums[right]) { + left = mid + 1; + } else { + right = mid - 1; + } + } + } + + return -1; + } +} From 2dafc41f5a7bf23f93174d3d5e6ee525d7555e73 Mon Sep 17 00:00:00 2001 From: Tessa1217 Date: Fri, 6 Jun 2025 19:20:07 +0900 Subject: [PATCH 3/6] add jump game solution --- jump-game/Tessa1217.java | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 jump-game/Tessa1217.java diff --git a/jump-game/Tessa1217.java b/jump-game/Tessa1217.java new file mode 100644 index 000000000..3a9195824 --- /dev/null +++ b/jump-game/Tessa1217.java @@ -0,0 +1,22 @@ +class Solution { + + // 시간 복잡도: O(N), 공간복잡도: O(1) + public boolean canJump(int[] nums) { + + int maxJump = 0; + for (int i = 0; i < nums.length; i++) { + // 최대 점프수로 현재 인덱스에 도달할 수 없다면 + if (i > maxJump) { + return false; + } + maxJump = Math.max(maxJump, i + nums[i]); + if (maxJump >= nums.length - 1) { + return true; + } + } + + return true; + + } +} + From 939986bcf809b67e59f16517d3235ccfb9103259 Mon Sep 17 00:00:00 2001 From: Tessa1217 Date: Sat, 7 Jun 2025 10:56:59 +0900 Subject: [PATCH 4/6] add merge k sorted lists solution --- merge-k-sorted-lists/Tessa1217.java | 98 +++++++++++++++++++++++++++++ 1 file changed, 98 insertions(+) create mode 100644 merge-k-sorted-lists/Tessa1217.java diff --git a/merge-k-sorted-lists/Tessa1217.java b/merge-k-sorted-lists/Tessa1217.java new file mode 100644 index 000000000..9698dc276 --- /dev/null +++ b/merge-k-sorted-lists/Tessa1217.java @@ -0,0 +1,98 @@ +import java.util.PriorityQueue; + +/** + * 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 { + + // 우선순위 큐를 활용 + // 시간복잡도: O(N log k) + public ListNode mergeKLists(ListNode[] lists) { + if (lists == null || lists.length == 0) { + return null; + } + + // 우선순위 큐 생성 + PriorityQueue pqueue = new PriorityQueue<>( + (a, b) -> Integer.compare(a.val, b.val) + ); + + // 첫번째 노드를 우선순위 큐에 삽입 + for (ListNode node : lists) { + if (node != null) { + pqueue.offer(node); + } + } + + ListNode temp = new ListNode(-1); + ListNode current = temp; + + // 꺼낸 노드의 다음노드가 있으면 큐에 넣는 것을 반복 + while (!pqueue.isEmpty()) { + ListNode node = pqueue.poll(); + current.next = node; + current = current.next; + + if (node.next != null) { + pqueue.offer(node.next); + } + } + + return temp.next; + } + + // 시간복잡도: O(N log K) + // public ListNode mergeKLists(ListNode[] lists) { + // if (lists == null || lists.length == 0) { + // return null; + // } + + // return mergeKLists(lists, 0, lists.length - 1); + + // } + + // 단계마다 left, right로 나누어 리스트 병합 + // private ListNode mergeKLists(ListNode[] lists, int start, int end) { + + // if (start == end) { + // return lists[start]; + // } + + // int mid = start + (end - start) / 2; + + // ListNode left = mergeKLists(lists, start, mid); + // ListNode right = mergeKLists(lists, mid + 1, end); + + // return mergeLists(left, right); + // } + + // 두 개의 노드에 대해 노드의 값 비교하여 ListNode에 연결하여 하나로 병합 + // private ListNode mergeLists(ListNode node1, ListNode node2) { + // ListNode temp = new ListNode(-1); + // ListNode current = temp; + + // while (node1 != null && node2 != null) { + // if (node1.val < node2.val) { + // current.next = node1; + // node1 = node1.next; + // } else { + // current.next = node2; + // node2 = node2.next; + // } + // current = current.next; + // } + + // if (node1 != null) current.next = node1; + // if (node2 != null) current.next = node2; + + // return temp.next; + // } + +} \ No newline at end of file From 70ebaf13f26b711b56ca14b77f32f9febe24dab9 Mon Sep 17 00:00:00 2001 From: Tessa1217 Date: Sat, 7 Jun 2025 11:20:33 +0900 Subject: [PATCH 5/6] add course schedule solution --- course-schedule/Tessa1217.java | 97 ++++++++++++++++++++++++++++++++++ 1 file changed, 97 insertions(+) create mode 100644 course-schedule/Tessa1217.java diff --git a/course-schedule/Tessa1217.java b/course-schedule/Tessa1217.java new file mode 100644 index 000000000..f607b387b --- /dev/null +++ b/course-schedule/Tessa1217.java @@ -0,0 +1,97 @@ +import java.util.ArrayList; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; + +class Solution { + public boolean canFinish(int numCourses, int[][] prerequisites) { + // return BFS(numCourses, prerequisites); + return DFS(numCourses, prerequisites); + } + + // BFS 방식으로 풀이 + private boolean BFS(int numCourses, int[][] prerequisites) { + + // 연결 그래프 생성 + List> graph = new ArrayList<>(); + for (int i = 0; i < numCourses; i++) { + graph.add(new ArrayList<>()); + } + + // 들어오는 간선의 수 배열 + int[] inDegree = new int[numCourses]; + + for (int[] pre : prerequisites) { + int course = pre[0]; + int prereq = pre[1]; + inDegree[course]++; + graph.get(prereq).add(course); + } + + // 들어오는 간선이 없는 과목 Queue에 삽입 + Queue q = new LinkedList<>(); + for (int i = 0; i < inDegree.length; i++) { + if (inDegree[i] == 0) { + q.offer(i); + } + } + + // 들은 과목 수 + int takeCourse = 0; + while (!q.isEmpty()) { + int course = q.poll(); + takeCourse++; + + for (int connect : graph.get(course)) { + inDegree[connect]--; + if (inDegree[connect] == 0) { + q.offer(connect); + } + } + } + + return takeCourse == numCourses; + } + + // DFS 방식 풀이 + private boolean DFS(int numCourses, int[][] prerequisites) { + List> graph = new ArrayList<>(); + for (int i = 0; i < numCourses; i++) { + graph.add(new ArrayList<>()); + } + + for (int[] pre : prerequisites) { + int course = pre[0]; + int prereq = pre[1]; + graph.get(prereq).add(course); + } + + // 처리 상태 배열 + int[] state = new int[numCourses]; + + for (int i = 0; i < numCourses; i++) { + if (hasCycle(graph, state, i)) { + return false; + } + } + + return true; + } + + // 사이클 여부 확인 + private boolean hasCycle(List> graph, int[] state, int course) { + + if (state[course] == 1) return true; // 사이클이 있다면 + if (state[course] == 2) return false; // 이미 처리 완료되었으므로 return + + state[course] = 1; + for (int connect : graph.get(course)) { + if (hasCycle(graph, state, connect)) { + return true; + } + } + state[course] = 2; // 처리 완료 + return false; + } +} + From 66b88c95ed5baf77917c73b0695b437ba373efd0 Mon Sep 17 00:00:00 2001 From: Tessa1217 Date: Sat, 7 Jun 2025 11:21:42 +0900 Subject: [PATCH 6/6] add line break --- merge-k-sorted-lists/Tessa1217.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/merge-k-sorted-lists/Tessa1217.java b/merge-k-sorted-lists/Tessa1217.java index 9698dc276..5432c0959 100644 --- a/merge-k-sorted-lists/Tessa1217.java +++ b/merge-k-sorted-lists/Tessa1217.java @@ -95,4 +95,5 @@ public ListNode mergeKLists(ListNode[] lists) { // return temp.next; // } -} \ No newline at end of file +} +