Skip to content

Commit 5d40ea4

Browse files
authored
Merge pull request #572 from wogha95/main
[재호] WEEK 12 Solutions
2 parents 1161152 + a1a9a8c commit 5d40ea4

File tree

3 files changed

+171
-0
lines changed

3 files changed

+171
-0
lines changed

merge-intervals/wogha95.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
* 정렬한 다음, 마지막 원소의 end값을 가지고 merge할지 원소 추가할지를 결정합니다.
3+
*
4+
* TC: O(N * logN)
5+
* 정렬에 의해 N * logN 복잡도를 갖습니다.
6+
*
7+
* SC: O(1)
8+
* 정답을 반환하는 공간을 제외하면 O(1), 포함하면 O(N)입니다.
9+
*
10+
* N: intervals.length
11+
*/
12+
13+
/**
14+
* @param {number[][]} intervals
15+
* @return {number[][]}
16+
*/
17+
var merge = function (intervals) {
18+
intervals.sort((a, b) => a[0] - b[0]);
19+
20+
return intervals.reduce((result, current) => {
21+
if (result.length === 0) {
22+
result.push(current);
23+
return result;
24+
}
25+
26+
const previous = result[result.length - 1];
27+
if (previous[1] >= current[0]) {
28+
result[result.length - 1][1] = Math.max(current[1], previous[1]);
29+
} else {
30+
result.push(current);
31+
}
32+
return result;
33+
}, []);
34+
};
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/**
2+
* 2차
3+
* n만큼 first가 움직인 다음, first가 끝까지 갈때까지 second를 움직입니다.
4+
* 그럼 그 위치가 끝에서 n번째임을 알 수 있는 풀이입니다.
5+
*
6+
* TC: O(N)
7+
* SC: O(1)
8+
* N: list length
9+
*/
10+
11+
/**
12+
* Definition for singly-linked list.
13+
* function ListNode(val, next) {
14+
* this.val = (val===undefined ? 0 : val)
15+
* this.next = (next===undefined ? null : next)
16+
* }
17+
*/
18+
/**
19+
* @param {ListNode} head
20+
* @param {number} n
21+
* @return {ListNode}
22+
*/
23+
var removeNthFromEnd = function (head, n) {
24+
const resultHead = new ListNode(null, head);
25+
let first = resultHead;
26+
let second = resultHead;
27+
28+
while (n > 0) {
29+
first = first.next;
30+
n -= 1;
31+
}
32+
33+
while (first.next) {
34+
first = first.next;
35+
second = second.next;
36+
}
37+
38+
second.next = second.next.next;
39+
40+
return resultHead.next;
41+
};
42+
43+
/**
44+
* 1차
45+
* 전체 순회로 갯수를 파악하고 해당 위치로가서 링크 연결 수정 작업
46+
*
47+
* TC: O(N)
48+
* SC: O(1)
49+
* N: list length
50+
*/
51+
52+
/**
53+
* Definition for singly-linked list.
54+
* function ListNode(val, next) {
55+
* this.val = (val===undefined ? 0 : val)
56+
* this.next = (next===undefined ? null : next)
57+
* }
58+
*/
59+
/**
60+
* @param {ListNode} head
61+
* @param {number} n
62+
* @return {ListNode}
63+
*/
64+
var removeNthFromEnd = function (head, n) {
65+
let listLength = 0;
66+
let pointer = head;
67+
68+
while (pointer) {
69+
pointer = pointer.next;
70+
listLength += 1;
71+
}
72+
73+
// if target of removal is the first node in list
74+
if (listLength === n) {
75+
return head.next;
76+
}
77+
78+
let nextCount = listLength - n - 1;
79+
pointer = head;
80+
81+
while (nextCount) {
82+
pointer = pointer.next;
83+
nextCount -= 1;
84+
}
85+
86+
pointer.next = pointer.next.next;
87+
88+
return head;
89+
};

same-tree/wogha95.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/**
2+
* TC: O(N)
3+
* SC: O(N)
4+
* N: count of node in tree
5+
*/
6+
7+
/**
8+
* Definition for a binary tree node.
9+
* function TreeNode(val, left, right) {
10+
* this.val = (val===undefined ? 0 : val)
11+
* this.left = (left===undefined ? null : left)
12+
* this.right = (right===undefined ? null : right)
13+
* }
14+
*/
15+
/**
16+
* @param {TreeNode} p
17+
* @param {TreeNode} q
18+
* @return {boolean}
19+
*/
20+
var isSameTree = function (p, q) {
21+
const queueP = [p];
22+
const queueQ = [q];
23+
24+
while (queueP.length > 0 && queueQ.length > 0) {
25+
const currentP = queueP.shift();
26+
const currentQ = queueQ.shift();
27+
28+
if (currentP === null && currentQ === null) {
29+
continue;
30+
}
31+
32+
if (currentP === null || currentQ === null) {
33+
return false;
34+
}
35+
36+
if (currentP.val !== currentQ.val) {
37+
return false;
38+
}
39+
40+
queueP.push(currentP.left);
41+
queueP.push(currentP.right);
42+
43+
queueQ.push(currentQ.left);
44+
queueQ.push(currentQ.right);
45+
}
46+
47+
return queueP.length === 0 && queueQ.length === 0;
48+
};

0 commit comments

Comments
 (0)