-
-
Notifications
You must be signed in to change notification settings - Fork 195
[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
Changes from all commits
Commits
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,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]; | ||
}; | ||
|
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,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; | ||
} | ||
} | ||
|
||
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,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; | ||
}; | ||
|
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,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; | ||
}; | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 값들을 순서대로 수학적 방식으로 사용해서 공간 복잡도를 줄이는 방식이 있군요!! |
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,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; | ||
} | ||
}; | ||
|
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.
set을 이용해서 풀수도 있군요
새로운 방식을 배웁니다
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.
네 ㅎㅎ visited를 기록할때 set도 쓸수있어서 주로 이방식으로 쓰고있어요~
아래 유튜브영상에서 간략하게 소개해주는 내용이 있어서 공유드립니다.
https://youtu.be/MlvZ2IufTFI?si=y2-nH9uDK91gZB7w&t=291