From 0f9c2ee0316de6eed46e57427751104bd04bff1a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9E=A5=EC=9E=AC=EC=A0=95=20=5B=EC=BA=90=ED=85=8C?= =?UTF-8?q?=EC=84=9C=EB=B9=84=EC=8A=A4=5D?= Date: Thu, 3 Oct 2024 20:39:24 +0900 Subject: [PATCH 1/3] merge two sorted lists solution --- merge-two-sorted-lists/jaejeong1.java | 71 +++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 merge-two-sorted-lists/jaejeong1.java diff --git a/merge-two-sorted-lists/jaejeong1.java b/merge-two-sorted-lists/jaejeong1.java new file mode 100644 index 00000000..258eb860 --- /dev/null +++ b/merge-two-sorted-lists/jaejeong1.java @@ -0,0 +1,71 @@ +import java.util.List; + +// Definition for singly-linked list. +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 void main(String[] args) { + Solution s = new Solution(); + var list1 = new ListNode(1); + list1.next = new ListNode(2); + list1.next.next = new ListNode(4); + + var list2 = new ListNode(1); + list2.next = new ListNode(3); + list2.next.next = new ListNode(4); + + System.out.println(s.mergeTwoLists(list1, list2)); + } + + public ListNode mergeTwoLists(ListNode list1, ListNode list2) { + // A와 B 헤드를 비교 + // A가 더 작거나 같으면 A 헤드를 빼서 새 노드로 추가 + // 그렇지 않으면 B 헤드를 빼서 새 노드로 추가 + // TC: O(N+M), N: list1의 길이, M: list2의 길이 + // SC: O(N+M), N: list1의 길이, M: list2의 길이 + ListNode mergedList = null; + + while(list1 != null && list2 != null) { + if (list1.val <= list2.val) { + mergedList = addNode(mergedList, list1.val); + list1 = list1.next; + } else { + mergedList = addNode(mergedList, list2.val); + list2 = list2.next; + } + } + + while(list1 != null) { + mergedList = addNode(mergedList, list1.val); + list1 = list1.next; + } + + while(list2 != null) { + mergedList = addNode(mergedList, list2.val); + list2 = list2.next; + } + + return mergedList; + } + + private ListNode addNode(ListNode node, int val) { + if (node == null) { + node = new ListNode(val); + } else { + var last = node; + while(last.next != null) { + last = last.next; + } + last.next = new ListNode(val); + } + + return node; + } +} From 07eb9b1b0bdbf31f8f294f7174912ae64ff42e46 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9E=A5=EC=9E=AC=EC=A0=95=20=5B=EC=BA=90=ED=85=8C?= =?UTF-8?q?=EC=84=9C=EB=B9=84=EC=8A=A4=5D?= Date: Fri, 4 Oct 2024 22:39:54 +0900 Subject: [PATCH 2/3] longest repeating character replacement solution --- .../jaejeong1.java | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 longest-repeating-character-replacement/jaejeong1.java diff --git a/longest-repeating-character-replacement/jaejeong1.java b/longest-repeating-character-replacement/jaejeong1.java new file mode 100644 index 00000000..d719bb2e --- /dev/null +++ b/longest-repeating-character-replacement/jaejeong1.java @@ -0,0 +1,31 @@ +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; + +class Solution { + public int characterReplacement(String s, int k) { + // 풀이: 슬라이딩 윈도우를 활용해 부분 문자열을 구한다 + // 종료 인덱스를 증가시키고, 부분 문자열 길이에서 가장 많이 들어있는 문자의 수를 뺀 값이 k보다 큰지 비교한다. + // 크다면, 시작 인덱스를 증가시킨다. + // 끝까지 반복하면서 최대 길이를 저장했다가 반환한다. + // TC: O(N) + // SC: O(N) + var maxLength = 0; + var start = 0; + var end = 0; + Map countByChar = new HashMap<>(); + + while (end < s.length()) { + countByChar.put(s.charAt(end), countByChar.getOrDefault(s.charAt(end), 0) + 1); + + while ((end - start + 1 - Collections.max(countByChar.values())) > k) { + countByChar.put(s.charAt(start), countByChar.get(s.charAt(start)) - 1); + start += 1; + } + maxLength = Math.max(end - start + 1, maxLength); + end++; + } + + return maxLength; + } +} From 1e537645d6a6f1472bc63a97e58e716ff38b4cea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9E=A5=EC=9E=AC=EC=A0=95=20=5B=EC=BA=90=ED=85=8C?= =?UTF-8?q?=EC=84=9C=EB=B9=84=EC=8A=A4=5D?= Date: Sat, 5 Oct 2024 14:42:45 +0900 Subject: [PATCH 3/3] clone graph solution --- clone-graph/jaejeong1.java | 56 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 clone-graph/jaejeong1.java diff --git a/clone-graph/jaejeong1.java b/clone-graph/jaejeong1.java new file mode 100644 index 00000000..1d65e090 --- /dev/null +++ b/clone-graph/jaejeong1.java @@ -0,0 +1,56 @@ +import java.util.ArrayList; +import java.util.HashMap; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; +import java.util.Queue; + +// Definition for a Node. +class Node { + public int val; + public List neighbors; + public Node() { + val = 0; + neighbors = new ArrayList(); + } + public Node(int _val) { + val = _val; + neighbors = new ArrayList(); + } + public Node(int _val, ArrayList _neighbors) { + val = _val; + neighbors = _neighbors; + } +} + + +class Solution { + // 풀이: BFS 방식으로 그래프를 순회하면서 현재 노드에 연결된 노드들을 복제 여부에 따라 복제 또는 연결 처리해준다 + // TC: O(N) + // SC: O(N) + public Node cloneGraph(Node node) { + if (node == null) { + return null; + } + + Map cloneMap = new HashMap<>(); + Node clone = new Node(node.val); + cloneMap.put(node, clone); + + Queue queue = new LinkedList<>(); + queue.add(node); + + while (!queue.isEmpty()) { + Node current = queue.poll(); + + for (Node neighbor : current.neighbors) { + if (!cloneMap.containsKey(neighbor)) { + cloneMap.put(neighbor, new Node(neighbor.val)); + queue.add(neighbor); + } + cloneMap.get(current).neighbors.add(cloneMap.get(neighbor)); + } + } + return clone; + } +}