Skip to content
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

[재호] WEEK 12 Solutions #572

Merged
merged 5 commits into from
Nov 3, 2024
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
34 changes: 34 additions & 0 deletions merge-intervals/wogha95.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/**
* 정렬한 다음, 마지막 원소의 end값을 가지고 merge할지 원소 추가할지를 결정합니다.
*
* TC: O(N * logN)
* 정렬에 의해 N * logN 복잡도를 갖습니다.
*
* SC: O(1)
* 정답을 반환하는 공간을 제외하면 O(1), 포함하면 O(N)입니다.
*
* N: intervals.length
*/

/**
* @param {number[][]} intervals
* @return {number[][]}
*/
var merge = function (intervals) {
intervals.sort((a, b) => a[0] - b[0]);

return intervals.reduce((result, current) => {
if (result.length === 0) {
result.push(current);
return result;
}

const previous = result[result.length - 1];
if (previous[1] >= current[0]) {
result[result.length - 1][1] = Math.max(current[1], previous[1]);
} else {
result.push(current);
}
return result;
}, []);
};
89 changes: 89 additions & 0 deletions remove-nth-node-from-end-of-list/wogha95.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/**
* 2차
* n만큼 first가 움직인 다음, first가 끝까지 갈때까지 second를 움직입니다.
* 그럼 그 위치가 끝에서 n번째임을 알 수 있는 풀이입니다.
*
* TC: O(N)
* SC: O(1)
* N: list length
*/

/**
* Definition for singly-linked list.
* function ListNode(val, next) {
* this.val = (val===undefined ? 0 : val)
* this.next = (next===undefined ? null : next)
* }
*/
/**
* @param {ListNode} head
* @param {number} n
* @return {ListNode}
*/
var removeNthFromEnd = function (head, n) {
const resultHead = new ListNode(null, head);
let first = resultHead;
let second = resultHead;

while (n > 0) {
first = first.next;
n -= 1;
}

while (first.next) {
first = first.next;
second = second.next;
}

second.next = second.next.next;

return resultHead.next;
};

/**
* 1차
* 전체 순회로 갯수를 파악하고 해당 위치로가서 링크 연결 수정 작업
*
* TC: O(N)
* SC: O(1)
* N: list length
*/

/**
* Definition for singly-linked list.
* function ListNode(val, next) {
* this.val = (val===undefined ? 0 : val)
* this.next = (next===undefined ? null : next)
* }
*/
/**
* @param {ListNode} head
* @param {number} n
* @return {ListNode}
*/
var removeNthFromEnd = function (head, n) {
let listLength = 0;
let pointer = head;

while (pointer) {
pointer = pointer.next;
listLength += 1;
}

// if target of removal is the first node in list
if (listLength === n) {
return head.next;
}

let nextCount = listLength - n - 1;
pointer = head;

while (nextCount) {
pointer = pointer.next;
nextCount -= 1;
}

pointer.next = pointer.next.next;

return head;
};
48 changes: 48 additions & 0 deletions same-tree/wogha95.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/**
* TC: O(N)
* SC: O(N)
* N: count of node in tree
*/

/**
* 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} p
* @param {TreeNode} q
* @return {boolean}
*/
var isSameTree = function (p, q) {
const queueP = [p];
const queueQ = [q];

while (queueP.length > 0 && queueQ.length > 0) {
const currentP = queueP.shift();
const currentQ = queueQ.shift();

if (currentP === null && currentQ === null) {
continue;
}

if (currentP === null || currentQ === null) {
return false;
}

if (currentP.val !== currentQ.val) {
return false;
}

queueP.push(currentP.left);
queueP.push(currentP.right);

queueQ.push(currentQ.left);
queueQ.push(currentQ.right);
}

return queueP.length === 0 && queueQ.length === 0;
};