-
-
Notifications
You must be signed in to change notification settings - Fork 195
[Tessa1217] Week 11 Solutions #1569
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
Show all changes
6 commits
Select commit
Hold shift + click to select a range
f63ad48
add missing number solution
Tessa1217 5262708
add binary tree maximum path sum
Tessa1217 3948a06
add merge intervals solution
Tessa1217 727fb27
add graph valid tree solution
Tessa1217 d3ef7fd
add reorder list solution
Tessa1217 477d47c
Merge branch 'main' of https://github.com/Tessa1217/leetcode-study
Tessa1217 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,42 @@ | ||
/** | ||
* 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; | ||
* } | ||
* } | ||
*/ | ||
class Solution { | ||
|
||
// 최대 Path 누적 합 | ||
int maxPathSumVal = Integer.MIN_VALUE; | ||
|
||
// 시간복잡도: O(n), 공간복잡도: O(h) h as height of tree | ||
public int maxPathSum(TreeNode root) { | ||
maxPathSumChecker(root); | ||
return maxPathSumVal; | ||
} | ||
|
||
// 재귀 | ||
private int maxPathSumChecker(TreeNode node) { | ||
|
||
if (node == null) { | ||
return 0; | ||
} | ||
|
||
int leftMax = Math.max(maxPathSumChecker(node.left), 0); | ||
int rightMax = Math.max(maxPathSumChecker(node.right), 0); | ||
|
||
maxPathSumVal = Math.max(node.val + leftMax + rightMax, maxPathSumVal); | ||
|
||
return node.val + Math.max(leftMax, rightMax); | ||
} | ||
} | ||
|
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,57 @@ | ||
public class Solution { | ||
/** | ||
* @param n: An integer | ||
* @param edges: a list of undirected edges | ||
* @return: true if it's a valid tree, or false | ||
*/ | ||
// 시간 복잡도: O(n), 공간복잡도: O(n) | ||
public boolean validTree(int n, int[][] edges) { | ||
|
||
if (edges.length != (n - 1)) { | ||
return false; | ||
} | ||
|
||
List<Integer>[] graph = new ArrayList[n]; | ||
for (int i = 0; i < n; i++) { | ||
graph[i] = new ArrayList<>(); | ||
} | ||
|
||
for (int[] edge : edges) { | ||
graph[edge[0]].add(edge[1]); | ||
graph[edge[1]].add(edge[0]); | ||
} | ||
|
||
boolean[] visited = new boolean[n]; | ||
|
||
if (!dfs(0, -1, visited, graph)) { | ||
return false; | ||
} | ||
|
||
for (boolean v : visited) { | ||
if (!v) { | ||
return false; // Not fully connected | ||
} | ||
} | ||
|
||
return true; | ||
} | ||
|
||
private boolean dfs(int node, int parent, boolean[] visited, List<Integer>[] graph) { | ||
if (visited[node]) { | ||
return false; // Found a cycle | ||
} | ||
|
||
visited[node] = true; | ||
|
||
for (int neighbor : graph[node]) { | ||
if (neighbor != parent) { | ||
if (!dfs(neighbor, node, visited, graph)) { | ||
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,37 @@ | ||
class Solution { | ||
public int[][] merge(int[][] intervals) { | ||
|
||
if (intervals.length <= 1) { | ||
return intervals; | ||
} | ||
|
||
List<int[]> answer = new ArrayList<>(); | ||
|
||
Arrays.sort(intervals, (a, b) -> Integer.compare(a[0], b[0])); | ||
|
||
int start = intervals[0][0]; | ||
int end = intervals[0][1]; | ||
|
||
for (int i = 1; i < intervals.length; i++) { | ||
|
||
// 현재 인터벌 시작점 | ||
int currStart = intervals[i][0]; | ||
// 현재 인터벌 끝점 | ||
int currEnd = intervals[i][1]; | ||
|
||
if (end >= currStart) { | ||
end = Math.max(end, currEnd); | ||
} else { | ||
answer.add(new int[]{start, end}); | ||
start = currStart; | ||
end = currEnd; | ||
} | ||
} | ||
|
||
answer.add(new int[]{start, end}); | ||
|
||
return answer.stream() | ||
.toArray(int[][]::new); | ||
} | ||
} | ||
|
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,24 @@ | ||
/** | ||
* 0 ~ n까지 고유한 숫자들로 이루어진 배열 nums가 주어질 때 해당 범위 내에서 없어진 숫자를 찾으세요. | ||
*/ | ||
class Solution { | ||
|
||
// 시간복잡도: O(n), 공간복잡도: O(n) | ||
public int missingNumber(int[] nums) { | ||
|
||
boolean[] visitNum = new boolean[nums.length + 1]; | ||
|
||
for (int i = 0; i < nums.length; i++) { | ||
visitNum[nums[i]] = true; | ||
} | ||
|
||
for (int i = 0; i < visitNum.length; i++) { | ||
if (!visitNum[i]) { | ||
return i; | ||
} | ||
} | ||
|
||
return 0; | ||
} | ||
} | ||
|
||
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,59 @@ | ||
/** | ||
* 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), 공간복잡도: O(1) | ||
public void reorderList(ListNode head) { | ||
|
||
// Slow, Fast Pointer | ||
// 1 - 2 - 3 - 4 - 5 | ||
ListNode slow = head; | ||
ListNode fast = head; | ||
|
||
// fast가 끝까지 닿을 때 중점 찾기 | ||
// slow = 3 | ||
while (fast != null && fast.next != null) { | ||
slow = slow.next; | ||
fast = fast.next.next; | ||
} | ||
|
||
|
||
// 중간 이후부터 끝까지 (second half of list) => reverse order | ||
// 3 이후부터 -> 4 - 5 | ||
// reverse - 5 - 4 | ||
ListNode backHead = null; // second half of list's new head | ||
ListNode backSide = slow.next; | ||
slow.next = null; | ||
|
||
while (backSide != null) { | ||
ListNode temp = backSide.next; | ||
backSide.next = backHead; | ||
backHead = backSide; | ||
backSide = temp; | ||
} | ||
|
||
// Merge first and second half | ||
|
||
// 1 - 2 - 3 | ||
ListNode firstHalf = head; | ||
// 5 - 4 | ||
ListNode secondHalf = backHead; | ||
|
||
// 1 - 5 - 2 - 4 - 3 | ||
while (secondHalf != null) { | ||
ListNode temp = firstHalf.next; | ||
firstHalf.next = secondHalf; | ||
firstHalf = secondHalf; | ||
secondHalf = temp; | ||
} | ||
} | ||
} | ||
|
||
|
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.
0부터 n까지 모든 숫자가 포함된다는 걸 이용해, 0부터 n까지 모두 더한 값과 주어진 배열의 모든 원소를 더한 값의 차를 반환하는 방법도 있습니다. 이렇게 하면 배열에 저장하지 않아도 돼서 공간복잡도를 O(1)로 개선할 수 있어서 참고하시면 좋을 것 같습니다!