-
-
Notifications
You must be signed in to change notification settings - Fork 195
[YeomChaeeun] Week 11 #1040
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
[YeomChaeeun] Week 11 #1040
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/** | ||
* Definition for a binary tree node. | ||
* class TreeNode { | ||
* val: number | ||
* left: TreeNode | null | ||
* right: TreeNode | null | ||
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) { | ||
* this.val = (val===undefined ? 0 : val) | ||
* this.left = (left===undefined ? null : left) | ||
* this.right = (right===undefined ? null : right) | ||
* } | ||
* } | ||
*/ | ||
/** | ||
* 트리의 깊이 구하기 | ||
* 알고리즘 복잡도 | ||
* - 시간 복잡도: O(n) | ||
* - 공간 복잡도: O(n) | ||
* @param root | ||
*/ | ||
function maxDepth(root: TreeNode | null): number { | ||
if(!root) return 0; | ||
let max = 0; | ||
let stack: [TreeNode | null, number][] = [[root, 1]]; | ||
while(stack.length > 0) { | ||
const [node, depth] = stack.pop(); | ||
max = Math.max(depth, max); | ||
if (node.left) stack.push([node.left, depth + 1]); | ||
if (node.right) stack.push([node.right, depth + 1]); | ||
} | ||
return max | ||
} |
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. 코드 전체 흐름이 너무 좋네요. 한가지 말씀드리고 싶은 부분은 변수명을 x,y 로 처리해 주셨는데 좀더 명확한 변수명을 사용한다면 의도가 더 확실하게 전달될 수 있을것 같아요. 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. 그렇겠네요 ㅎㅎ 피드백 감사합니다! |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/** | ||
* 겹치는 구간 합치기 | ||
* 알고리즘 복잡도 | ||
* - 시간 복잡도: O(n log n) - sort() 영향 | ||
* - 공간 복잡도: O(n) | ||
* @param intervals | ||
*/ | ||
function merge(intervals: number[][]): number[][] { | ||
if(intervals.length === 1) return intervals | ||
|
||
intervals.sort((a, b) => a[0] - b[0]); | ||
|
||
let results: number[][] = [] | ||
let [tempX, tempY] = intervals[0] | ||
|
||
for(let i = 1; i < intervals.length; i++) { | ||
let [x, y] = intervals[i] | ||
if(x <= tempY) { | ||
tempY = Math.max(tempY, y) | ||
} else { | ||
results.push([tempX, tempY]) | ||
tempX = x | ||
tempY = y | ||
} | ||
} | ||
results.push([tempX, tempY]) | ||
|
||
return results; | ||
} |
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. stack에 모든 노드를 저장하여 문제 풀이 진행 해주셔서 공간 복잡도가 O(n)이 나왔네요! 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. 넵! 특성을 고려하여 도전해보겠습니다~! 감사합니다 ㅎㅎ |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/** | ||
* Definition for singly-linked list. | ||
* class ListNode { | ||
* val: number | ||
* next: ListNode | null | ||
* constructor(val?: number, next?: ListNode | null) { | ||
* this.val = (val===undefined ? 0 : val) | ||
* this.next = (next===undefined ? null : next) | ||
* } | ||
* } | ||
*/ | ||
|
||
/** | ||
Do not return anything, modify head in-place instead. | ||
*/ | ||
|
||
/** | ||
* 리스트 재정렬 하기 (0 -> n -> 1 -> n-1 -> ...) | ||
* 알고리즘 복잡도 | ||
* - 시간 복잡도: O(n) | ||
* - 공간 복잡도: O(n) | ||
* @param head | ||
*/ | ||
function reorderList(head: ListNode | null): void { | ||
if (!head || !head.next) return; | ||
|
||
const stack: ListNode[] = []; | ||
let node = head; | ||
while (node) { | ||
stack.push(node); | ||
node = node.next; | ||
} | ||
|
||
let left = 0; | ||
let right = stack.length - 1; | ||
|
||
while (left < right) { | ||
// 현재 노드의 다음에 마지막 노드 연결 | ||
stack[left].next = stack[right]; | ||
left++; | ||
|
||
// 남은 노드가 있으면 마지막 노드의 다음에 다음 왼쪽 노드 연결 | ||
if (left < right) { | ||
stack[right].next = stack[left]; | ||
right--; | ||
} | ||
} | ||
|
||
// 마지막 노드의 next를 null로 설정 | ||
stack[left].next = null; | ||
} |
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.
stack 을 사용하여 while 반복문으로 풀이해주셨네요!
DFS를 사용해서 공간 복잡도를 최소한으로 하는 풀이도 도전해보시면 좋을것 같습니다.
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.
감사합니다! 다른 방법도 공부해보겠습니다 ~