-
-
Notifications
You must be signed in to change notification settings - Fork 195
[Ackku] week 11 #1048
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
[Ackku] week 11 #1048
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,27 @@ | ||
// dfs와 분할 정복법으로 해결되는 문제 | ||
// dfs인 것을 알았으나 모든 숫자가 음수일 경우를 판별하지 못해 GPT에게 도움을 처함 | ||
class Solution { | ||
private int maxSum = Integer.MIN_VALUE; // 전체 최대 경로 합 저장 | ||
|
||
public int maxPathSum(TreeNode root) { | ||
dfs(root); | ||
return maxSum; | ||
} | ||
|
||
private int dfs(TreeNode node) { | ||
if (node == null) return 0; // base case | ||
|
||
// 왼쪽, 오른쪽 서브트리의 최대 경로 합 (음수는 포함 X → Math.max(0, value)) | ||
int left = Math.max(0, dfs(node.left)); | ||
int right = Math.max(0, dfs(node.right)); | ||
|
||
// 현재 노드를 포함한 최대 경로 (왼쪽 + 루트 + 오른쪽) | ||
int pathSum = left + node.val + right; | ||
|
||
// 최대 경로 값 갱신 | ||
maxSum = Math.max(maxSum, pathSum); | ||
|
||
// ✅ 현재 노드를 포함하는 최대 경로 값만 반환해야 함 (연결 가능한 경로) | ||
return node.val + Math.max(left, right); | ||
} | ||
} |
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,34 @@ | ||
// 저번주 course-schedule 문제와 유사한 사이클이 존재하지 않아야하는 트리를 찾는 문제 | ||
// 차이가 있다면 course-schedule는 방향 그래프지만 이 문제는 무방향 그래프라는 차이가 있음 | ||
// 무방향 그래프는 이전 노드(부모)를 통해 다시 돌아온 후 연결을 판단해야하는 경우가 있어거 구현이 조금 더 어려움 | ||
public class GraphValidTree { | ||
public boolean validTree(int n, int[][] edges) { | ||
if (edges.length != n - 1) return false; // 트리는 반드시 (n-1)개의 간선 필요 | ||
|
||
// 그래프 인접 리스트 생성 | ||
Map<Integer, List<Integer>> graph = new HashMap<>(); | ||
for (int i = 0; i < n; i++) { | ||
graph.put(i, new ArrayList<>()); | ||
} | ||
for (int[] edge : edges) { | ||
graph.get(edge[0]).add(edge[1]); | ||
graph.get(edge[1]).add(edge[0]); | ||
} | ||
|
||
Set<Integer> visited = new HashSet<>(); | ||
if (!dfs(graph, 0, -1, visited)) return false; | ||
|
||
return visited.size() == n; | ||
} | ||
|
||
private boolean dfs(Map<Integer, List<Integer>> graph, int node, int parent, Set<Integer> visited) { | ||
if (visited.contains(node)) return false; // 사이클 발견 | ||
|
||
visited.add(node); | ||
for (int neighbor : graph.get(node)) { | ||
if (neighbor == parent) continue; | ||
if (!dfs(graph, neighbor, node, visited)) return false; | ||
} | ||
return true; | ||
} | ||
} |
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,8 @@ | ||
// 트리 탐색은 깊이 우선 탐색이 공간복잡도가 낮다. | ||
// 모든 노드(리스트)를 O(N)의 시간복잡도를 갖는다. | ||
class Solution { | ||
public int maxDepth(TreeNode root) { | ||
if (root == null) return 0; | ||
return Math.max(maxDepth(root.left), maxDepth(root.right)) + 1; | ||
} | ||
} |
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 @@ | ||
// 정렬을 하지 않으려면 O(N^2)이 걸림 | ||
// 정렬을 나면 아무리 빨라도 O(N log N)이지만 이후에 문제가 간단해짐 | ||
// currentEnd >= nextStart → 겹치면 end 값을 업데이트 하는 간단한 방식으로 구현 | ||
class Solution { | ||
public int[][] merge(int[][] intervals) { | ||
if (intervals.length <= 1) return intervals; | ||
|
||
Arrays.sort(intervals, (a, b) -> Integer.compare(a[0], b[0])); | ||
|
||
List<int[]> merged = new ArrayList<>(); | ||
int[] currentInterval = intervals[0]; | ||
merged.add(currentInterval); | ||
|
||
for (int[] interval : intervals) { | ||
int currentEnd = currentInterval[1]; | ||
int nextStart = interval[0]; | ||
int nextEnd = interval[1]; | ||
|
||
if (currentEnd >= nextStart) { | ||
// 겹치면 end 값을 업데이트 | ||
currentInterval[1] = Math.max(currentEnd, nextEnd); | ||
} else { | ||
// 겹치지 않으면 새로운 구간으로 추가 | ||
currentInterval = interval; | ||
merged.add(currentInterval); | ||
} | ||
} | ||
|
||
return merged.toArray(new int[merged.size()][]); | ||
} | ||
} |
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. GPT 도움, 모범 답안 참고를 해도 너무 좋다고 생각합니다! 해당 문제를 충분히 잘 이해하고 넘어가신 거라면 더더욱 좋을것 같습니다. |
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,63 @@ | ||
// GPT의 도움을 받아 조금 더 공간복잡도가 낮고 빠른 방식으로 구현 | ||
class Solution { | ||
public void reorderList(ListNode head) { | ||
if (head == null || head.next == null) return; | ||
|
||
ListNode slow = head, fast = head; | ||
while (fast != null && fast.next != null) { | ||
slow = slow.next; | ||
fast = fast.next.next; | ||
} | ||
|
||
ListNode second = reverse(slow.next); | ||
slow.next = null; | ||
|
||
ListNode first = head; | ||
while (second != null) { | ||
ListNode tmp1 = first.next; | ||
ListNode tmp2 = second.next; | ||
|
||
first.next = second; | ||
second.next = tmp1; | ||
|
||
first = tmp1; | ||
second = tmp2; | ||
} | ||
} | ||
|
||
private ListNode reverse(ListNode head) { | ||
ListNode prev = null; | ||
while (head != null) { | ||
ListNode next = head.next; | ||
head.next = prev; | ||
prev = head; | ||
head = next; | ||
} | ||
return prev; | ||
} | ||
} | ||
|
||
// 첫번째 생각한 풀이방식 리스트를 만들고 값을 넣어서 처리하니 시간복잡도는 O(N)으로 이론상 같으나 | ||
// 속도라 평균속도보다 현저하게 낮게나온다. | ||
class Solution { | ||
public void reorderList(ListNode head) { | ||
if (head == null || head.next == null) return; | ||
|
||
List<ListNode> list = new ArrayList<>(); | ||
ListNode temp = head; | ||
while (temp != null) { | ||
list.add(temp); | ||
temp = temp.next; | ||
} | ||
|
||
int i = 0, j = list.size() - 1; | ||
while (i < j) { | ||
list.get(i).next = list.get(j); | ||
i++; | ||
if (i == j) break; | ||
list.get(j).next = list.get(i); | ||
j--; | ||
} | ||
list.get(i).next = null; | ||
} | ||
} |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
for문 내부가 적어주신 대로 간단하게 구현되어 있어서 가독성이 너무 좋은것 같아요 :)