-
-
Notifications
You must be signed in to change notification settings - Fork 245
[jaejeong1] Week 08 Solutions #513
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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의 길이 | ||
// 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; | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.