Skip to content

[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 6 commits into from
Jun 13, 2025
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
42 changes: 42 additions & 0 deletions binary-tree-maximum-path-sum/Tessa1217.java
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);
}
}

57 changes: 57 additions & 0 deletions graph-valid-tree/Tessa1217.java
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;
}
}

37 changes: 37 additions & 0 deletions merge-intervals/Tessa1217.java
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);
}
}

24 changes: 24 additions & 0 deletions missing-number/Tessa1217.java
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;
}
}

Comment on lines +1 to +24
Copy link
Contributor

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)로 개선할 수 있어서 참고하시면 좋을 것 같습니다!

59 changes: 59 additions & 0 deletions reorder-list/Tessa1217.java
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;
}
}
}