Skip to content

[moonjonghoo] WEEK 11 Solutions #1579

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 3 commits into from
Jun 16, 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
17 changes: 17 additions & 0 deletions binary-tree-maximum-path-sum/moonjonghoo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
var maxPathSum = function (root) {
let maxSum = -Infinity;

function dfs(node) {
if (node === null) return 0; // 6) Base Case
const left = Math.max(dfs(node.left), 0); // 8) Pruning
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

재귀 함수로 명확하게 짜신 것 같네요!, 저도 풀 떄 참고하겠습니다.

const right = Math.max(dfs(node.right), 0); // 8) Pruning

const currentSum = left + node.val + right; // 9) Pivot Sum
maxSum = Math.max(maxSum, currentSum); // 7) Global Max

return node.val + Math.max(left, right); // 10) Return Value
}

dfs(root); // 4) DFS 시작
console.log(maxSum); // 최종 답 출력
};
21 changes: 21 additions & 0 deletions merge-intervals/moonjonghoo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
function merge(intervals) {
if (intervals.length === 0) return [];

// 1) 시작점 기준 정렬
intervals.sort((a, b) => a[0] - b[0]);

const merged = [];
for (const interval of intervals) {
// 2) 결과 배열의 마지막 구간
const last = merged[merged.length - 1];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

마지막 구간을 확실히 분리해서 관리하니까 코드가 명확하네요


// 3) 겹치지 않으면 새 구간 추가
if (!last || interval[0] > last[1]) {
merged.push(interval);
} else {
// 4) 겹치면 병합: 끝점 확장
last[1] = Math.max(last[1], interval[1]);
}
}
return merged;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Time Complexity: O(n + e) — 노드 n개와 간선 e개를 한 번씩 순회
// Space Complexity: O(n + e) — 인접 리스트 저장 O(n+e), 재귀 호출 스택 최악 O(n)
function countComponents(n, edges) {
// 인접 리스트 생성
const adj = Array.from({ length: n }, () => []);
for (const [u, v] of edges) {
adj[u].push(v);
adj[v].push(u);
}

const visited = Array(n).fill(false);
let count = 0;

function dfs(u) {
visited[u] = true;
for (const v of adj[u]) {
if (!visited[v]) dfs(v);
}
}

// 모든 노드 순회
for (let i = 0; i < n; i++) {
if (!visited[i]) {
count++;
dfs(i);
}
}

return count;
}