Skip to content

[ganu] Week 12 #1053

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 6 commits into from
Mar 1, 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
35 changes: 35 additions & 0 deletions non-overlapping-intervals/gwbaik9717.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Time complexity: O(nlogn)
// Space complexity: O(1)

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

return a[0] - b[0];
});

let count = 0;
let prevEnd = intervals[0][1];

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

// 구간이 겹칠 때
if (prevEnd > start) {
count++;
prevEnd = Math.min(prevEnd, end);
continue;
}

// 구간이 겹치지 않을 때
prevEnd = end;
}

return count;
};
50 changes: 50 additions & 0 deletions remove-nth-node-from-end-of-list/gwbaik9717.js
Copy link
Contributor

Choose a reason for hiding this comment

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

오.. LinkedList 를 뒤집어서 푸는건 또 참신한 방법이라고 생각되네요.
다른 해답도 참고해 보시면 더욱 시야가 넓어지실것 같습니다!

Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// Time complexity: O(n)
// Space complexity: O(1)

/**
* 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 reverse = (head) => {
let next = null;
let current = head;

while (current) {
const temp = current.next;
current.next = next;
next = current;
current = temp;
}

return next;
};

// Reverse
let reversedHead = reverse(head);

if (n === 1) {
reversedHead = reversedHead.next;
} else {
let prev = null;
let current = reversedHead;

for (let i = 1; i < n; i++) {
prev = current;
current = current.next;
}

prev.next = current.next;
}

// Reverse Again
return reverse(reversedHead);
};
38 changes: 38 additions & 0 deletions same-tree/gwbaik9717.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// n: number of nodes
// Time complexity: O(n)
// Space complexity: O(n)

/**
* 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 dfs = (currentP, currentQ) => {
if (!currentP && !currentQ) {
return true;
}

if ((!currentP && currentQ) || (currentP && !currentQ)) {
return false;
}

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

return (
dfs(currentP.left, currentQ.left) && dfs(currentP.right, currentQ.right)
);
};

return dfs(p, q);
};
132 changes: 132 additions & 0 deletions serialize-and-deserialize-binary-tree/gwbaik9717.js
Copy link
Contributor

Choose a reason for hiding this comment

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

어려운 문제인데 Queue를 활용하여 잘 풀이해주신것 같습니다.

Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
// n: number of nodes, h: height of tree (max: n)
// Time complexity: O(n)
// Space complexity: O(2^h)

/**
* Definition for a binary tree node.
* function TreeNode(val) {
* this.val = val;
* this.left = this.right = null;
* }
*/

class _Queue {
constructor() {
this.q = [];
this.left = 0;
this.right = 0;
}

push(value) {
this.q.push(value);
this.right++;
}

shift() {
const rv = this.q[this.left];
delete this.q[this.left++];

return rv;
}

isEmpty() {
return this.left === this.right;
}
}

const isValid = (data) => {
if (data === undefined || data === null) {
return false;
}

return true;
};

/**
* Encodes a tree to a single string.
*
* @param {TreeNode} root
* @return {string}
*/
var serialize = function (root) {
const answer = [null];

const bfs = (current) => {
const q = new _Queue();
q.push([1, current]);

while (!q.isEmpty()) {
const [i, current] = q.shift();

if (current === null) {
answer[i] = current;
continue;
}

answer[i] = current.val;

const left = 2 * i;
const right = left + 1;

if (current.left) {
q.push([left, current.left]);
} else {
q.push([left, null]);
}

if (current.right) {
q.push([right, current.right]);
} else {
q.push([right, null]);
}
}
};

bfs(root);

while (answer.length > 1 && !isValid(answer.at(-1))) {
answer.pop();
}

return answer;
};

/**
* Decodes your encoded data to tree.
*
* @param {string} data
* @return {TreeNode}
*/
var deserialize = function (data) {
if (data.length === 1) {
return null;
}

const root = new TreeNode(data[1]);
const q = new _Queue();
q.push([1, root]);

while (!q.isEmpty()) {
const [i, current] = q.shift();

const left = i * 2;
const right = left + 1;

if (left <= data.length && isValid(data[left])) {
current.left = new TreeNode(data[left]);
q.push([left, current.left]);
}

if (right <= data.length && isValid(data[right])) {
current.right = new TreeNode(data[right]);
q.push([right, current.right]);
}
}

return root;
};

/**
* Your functions will be called as such:
* deserialize(serialize(root));
*/