Skip to content

[hsskey] WEEK 11 Solutions #1582

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 1 commit into from
Jun 15, 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
36 changes: 36 additions & 0 deletions binary-tree-maximum-path-sum/hsskey.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/**
* Definition for a binary tree node.
* function TreeNode(val, left, right) {
* this.val = (val===undefined ? 0 : val);
* this.left = (left===undefined ? null : left);
* this.right = (right===undefined ? null : right);
* }
*/

/**
* @param {TreeNode} root
* @return {number}
*/
var maxPathSum = function(root) {
let res = [root.val];

function dfs(node) {
if (!node) return 0;

let leftMax = dfs(node.left);
let rightMax = dfs(node.right);

leftMax = Math.max(leftMax, 0);
rightMax = Math.max(rightMax, 0);

// 경유지점 포함한 경로 최대값 업데이트
res[0] = Math.max(res[0], node.val + leftMax + rightMax);

// 분기하지 않는 경로에서의 최대값 리턴
return node.val + Math.max(leftMax, rightMax);
}

dfs(root);
return res[0];
};

38 changes: 38 additions & 0 deletions graph-valid-tree/hsskey.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
export class Solution {
/**
* @param {number} n - number of nodes
* @param {number[][]} edges - undirected edges
* @return {boolean}
*/
validTree(n, edges) {
if (n === 0) return true;

// 인접 리스트 생성
const adj = {};
for (let i = 0; i < n; i++) {
adj[i] = [];
}
for (const [n1, n2] of edges) {
adj[n1].push(n2);
adj[n2].push(n1);
}

const visit = new Set();

const dfs = (i, prev) => {
if (visit.has(i)) return false;

visit.add(i);

for (const j of adj[i]) {
if (j === prev) continue;
if (!dfs(j, i)) return false;
}

return true;
};

return dfs(0, -1) && visit.size === n;
}
}

Copy link
Contributor

Choose a reason for hiding this comment

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

set을 이용해서 풀수도 있군요
새로운 방식을 배웁니다

Copy link
Member Author

Choose a reason for hiding this comment

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

네 ㅎㅎ visited를 기록할때 set도 쓸수있어서 주로 이방식으로 쓰고있어요~
아래 유튜브영상에서 간략하게 소개해주는 내용이 있어서 공유드립니다.
https://youtu.be/MlvZ2IufTFI?si=y2-nH9uDK91gZB7w&t=291

26 changes: 26 additions & 0 deletions merge-intervals/hsskey.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
* @param {number[][]} intervals
* @return {number[][]}
*/
var merge = function(intervals) {
if (intervals.length === 0) return [];

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

const output = [intervals[0]];

for (let i = 1; i < intervals.length; i++) {
const [start, end] = intervals[i];
const lastEnd = output[output.length - 1][1];

if (start <= lastEnd) {
output[output.length - 1][1] = Math.max(lastEnd, end);
} else {
output.push([start, end]);
}
}

return output;
};

14 changes: 14 additions & 0 deletions missing-number/hsskey.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/**
* @param {number[]} nums
* @return {number}
*/
var missingNumber = function(nums) {
let res = nums.length;

for (let i = 0; i < nums.length; i++) {
res += i - nums[i];
}

return res;
};

Copy link
Contributor

Choose a reason for hiding this comment

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

값들을 순서대로 수학적 방식으로 사용해서 공간 복잡도를 줄이는 방식이 있군요!!

45 changes: 45 additions & 0 deletions reorder-list/hsskey.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/**
* Definition for singly-linked list.
* function ListNode(val, next) {
* this.val = (val===undefined ? 0 : val)
* this.next = (next===undefined ? null : next)
* }
*/

/**
* @param {ListNode} head
* @return {void} Do not return anything, modify head in-place instead.
*/
var reorderList = function(head) {
if (!head || !head.next) return;

let slow = head, fast = head.next;
while (fast && fast.next) {
slow = slow.next;
fast = fast.next.next;
}

let second = slow.next;
let prev = null;
slow.next = null;
while (second) {
let tmp = second.next;
second.next = prev;
prev = second;
second = tmp;
}

let first = head;
second = prev;
while (second) {
let tmp1 = first.next;
let tmp2 = second.next;

first.next = second;
second.next = tmp1;

first = tmp1;
second = tmp2;
}
};