Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[jaejeong1] Week 08 Solutions #513

Merged
merged 3 commits into from
Oct 6, 2024
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
56 changes: 56 additions & 0 deletions clone-graph/jaejeong1.java
Original file line number Diff line number Diff line change
@@ -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<Node> neighbors;
public Node() {
val = 0;
neighbors = new ArrayList<Node>();
}
public Node(int _val) {
val = _val;
neighbors = new ArrayList<Node>();
}
public Node(int _val, ArrayList<Node> _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<Node, Node> cloneMap = new HashMap<>();
Node clone = new Node(node.val);
cloneMap.put(node, clone);

Queue<Node> 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;
}
}
31 changes: 31 additions & 0 deletions longest-repeating-character-replacement/jaejeong1.java
Original file line number Diff line number Diff line change
@@ -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<Character, Integer> 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;
}
}
71 changes: 71 additions & 0 deletions merge-two-sorted-lists/jaejeong1.java
Original file line number Diff line number Diff line change
@@ -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의 길이
jaejeong1 marked this conversation as resolved.
Show resolved Hide resolved
// 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;
}
}