Skip to content

Commit cc24632

Browse files
authored
Merge pull request #1569 from Tessa1217/main
[Tessa1217] Week 11 Solutions
2 parents c8bf354 + 477d47c commit cc24632

File tree

5 files changed

+219
-0
lines changed

5 files changed

+219
-0
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* public class TreeNode {
4+
* int val;
5+
* TreeNode left;
6+
* TreeNode right;
7+
* TreeNode() {}
8+
* TreeNode(int val) { this.val = val; }
9+
* TreeNode(int val, TreeNode left, TreeNode right) {
10+
* this.val = val;
11+
* this.left = left;
12+
* this.right = right;
13+
* }
14+
* }
15+
*/
16+
class Solution {
17+
18+
// 최대 Path 누적 합
19+
int maxPathSumVal = Integer.MIN_VALUE;
20+
21+
// 시간복잡도: O(n), 공간복잡도: O(h) h as height of tree
22+
public int maxPathSum(TreeNode root) {
23+
maxPathSumChecker(root);
24+
return maxPathSumVal;
25+
}
26+
27+
// 재귀
28+
private int maxPathSumChecker(TreeNode node) {
29+
30+
if (node == null) {
31+
return 0;
32+
}
33+
34+
int leftMax = Math.max(maxPathSumChecker(node.left), 0);
35+
int rightMax = Math.max(maxPathSumChecker(node.right), 0);
36+
37+
maxPathSumVal = Math.max(node.val + leftMax + rightMax, maxPathSumVal);
38+
39+
return node.val + Math.max(leftMax, rightMax);
40+
}
41+
}
42+

graph-valid-tree/Tessa1217.java

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
public class Solution {
2+
/**
3+
* @param n: An integer
4+
* @param edges: a list of undirected edges
5+
* @return: true if it's a valid tree, or false
6+
*/
7+
// 시간 복잡도: O(n), 공간복잡도: O(n)
8+
public boolean validTree(int n, int[][] edges) {
9+
10+
if (edges.length != (n - 1)) {
11+
return false;
12+
}
13+
14+
List<Integer>[] graph = new ArrayList[n];
15+
for (int i = 0; i < n; i++) {
16+
graph[i] = new ArrayList<>();
17+
}
18+
19+
for (int[] edge : edges) {
20+
graph[edge[0]].add(edge[1]);
21+
graph[edge[1]].add(edge[0]);
22+
}
23+
24+
boolean[] visited = new boolean[n];
25+
26+
if (!dfs(0, -1, visited, graph)) {
27+
return false;
28+
}
29+
30+
for (boolean v : visited) {
31+
if (!v) {
32+
return false; // Not fully connected
33+
}
34+
}
35+
36+
return true;
37+
}
38+
39+
private boolean dfs(int node, int parent, boolean[] visited, List<Integer>[] graph) {
40+
if (visited[node]) {
41+
return false; // Found a cycle
42+
}
43+
44+
visited[node] = true;
45+
46+
for (int neighbor : graph[node]) {
47+
if (neighbor != parent) {
48+
if (!dfs(neighbor, node, visited, graph)) {
49+
return false;
50+
}
51+
}
52+
}
53+
54+
return true;
55+
}
56+
}
57+

merge-intervals/Tessa1217.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
class Solution {
2+
public int[][] merge(int[][] intervals) {
3+
4+
if (intervals.length <= 1) {
5+
return intervals;
6+
}
7+
8+
List<int[]> answer = new ArrayList<>();
9+
10+
Arrays.sort(intervals, (a, b) -> Integer.compare(a[0], b[0]));
11+
12+
int start = intervals[0][0];
13+
int end = intervals[0][1];
14+
15+
for (int i = 1; i < intervals.length; i++) {
16+
17+
// 현재 인터벌 시작점
18+
int currStart = intervals[i][0];
19+
// 현재 인터벌 끝점
20+
int currEnd = intervals[i][1];
21+
22+
if (end >= currStart) {
23+
end = Math.max(end, currEnd);
24+
} else {
25+
answer.add(new int[]{start, end});
26+
start = currStart;
27+
end = currEnd;
28+
}
29+
}
30+
31+
answer.add(new int[]{start, end});
32+
33+
return answer.stream()
34+
.toArray(int[][]::new);
35+
}
36+
}
37+

missing-number/Tessa1217.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* 0 ~ n까지 고유한 숫자들로 이루어진 배열 nums가 주어질 때 해당 범위 내에서 없어진 숫자를 찾으세요.
3+
*/
4+
class Solution {
5+
6+
// 시간복잡도: O(n), 공간복잡도: O(n)
7+
public int missingNumber(int[] nums) {
8+
9+
boolean[] visitNum = new boolean[nums.length + 1];
10+
11+
for (int i = 0; i < nums.length; i++) {
12+
visitNum[nums[i]] = true;
13+
}
14+
15+
for (int i = 0; i < visitNum.length; i++) {
16+
if (!visitNum[i]) {
17+
return i;
18+
}
19+
}
20+
21+
return 0;
22+
}
23+
}
24+

reorder-list/Tessa1217.java

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* public class ListNode {
4+
* int val;
5+
* ListNode next;
6+
* ListNode() {}
7+
* ListNode(int val) { this.val = val; }
8+
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
9+
* }
10+
*/
11+
class Solution {
12+
// 시간복잡도: O(n), 공간복잡도: O(1)
13+
public void reorderList(ListNode head) {
14+
15+
// Slow, Fast Pointer
16+
// 1 - 2 - 3 - 4 - 5
17+
ListNode slow = head;
18+
ListNode fast = head;
19+
20+
// fast가 끝까지 닿을 때 중점 찾기
21+
// slow = 3
22+
while (fast != null && fast.next != null) {
23+
slow = slow.next;
24+
fast = fast.next.next;
25+
}
26+
27+
28+
// 중간 이후부터 끝까지 (second half of list) => reverse order
29+
// 3 이후부터 -> 4 - 5
30+
// reverse - 5 - 4
31+
ListNode backHead = null; // second half of list's new head
32+
ListNode backSide = slow.next;
33+
slow.next = null;
34+
35+
while (backSide != null) {
36+
ListNode temp = backSide.next;
37+
backSide.next = backHead;
38+
backHead = backSide;
39+
backSide = temp;
40+
}
41+
42+
// Merge first and second half
43+
44+
// 1 - 2 - 3
45+
ListNode firstHalf = head;
46+
// 5 - 4
47+
ListNode secondHalf = backHead;
48+
49+
// 1 - 5 - 2 - 4 - 3
50+
while (secondHalf != null) {
51+
ListNode temp = firstHalf.next;
52+
firstHalf.next = secondHalf;
53+
firstHalf = secondHalf;
54+
secondHalf = temp;
55+
}
56+
}
57+
}
58+
59+

0 commit comments

Comments
 (0)