Skip to content

[jdy8739] week 12 #1049

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 5 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
32 changes: 32 additions & 0 deletions non-overlapping-intervals/jdy8739.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/**
* @param {number[][]} intervals
* @return {number}
*/
var eraseOverlapIntervals = function (intervals) {

const sort = intervals.sort((a, b) => {
if (a[1] === b[1]) {
return a[0] - b[0];
}

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

let count = 0;
let max = sort[0][1];

for (let i = 0; i < sort.length - 1; i++) {
const right = sort[i + 1][0];

if (max > right) {
count++;
} else {
max = sort[i + 1][1];
}
}

return count;
};

// 시간복잡도 - O(nlogn) 인터벌 뒷 숫자 정렬 시 정렬로 인한 시간복잡도 발생
// 공간복잡도 - O(1) 특별한 자료구조가 사용되지 않아 상수 공간복잡도 발생
45 changes: 45 additions & 0 deletions remove-nth-node-from-end-of-list/jdy8739.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 의 특성을 활용한 문제입니다!
잘 활용하시면 공간 복잡도를 O(1) 으로 풀이하실 수 있을거에요.

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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,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
* @param {number} n
* @return {ListNode}
*/
var removeNthFromEnd = function (head, n) {
if (!head) {
return null;
}

let node = head;

const list = [];

while (node) {
list.push(node);
node = node.next;
}

const targetIndex = list.length - n - 1;

if (targetIndex === -1 && list.length === 1) {
return null;
}

if (targetIndex === -1) {
return head.next;
}

if (list[targetIndex]) {
list[targetIndex].next = list[targetIndex]?.next?.next || null;
}

return head;
};

// 시간복잡도 O(n) - 노드를 한번 순회하여 리스트에 저장
// 공간복잡도 O(n) - 순회 이후에 제거할 노드의 바로 앞 노드레 접근하기 위하여 모든 노드를 배열에 저장
48 changes: 48 additions & 0 deletions same-tree/jdy8739.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/**
* 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 = (a, b) => {
const isValSame = a?.val === b?.val;
const isLeftValSame = a?.left?.val === b?.left?.val;
const isRightValSame = a?.right?.val === b?.right?.val;

if (!isValSame || !isLeftValSame || !isRightValSame) {
return true;
}

if (a?.left && b?.left) {
const isLeftDiff = dfs(a.left, b.left);

if (isLeftDiff) {
return true;
}
}
W

if (a?.right && b?.right) {
const isRightDiff = dfs(a.right, b.right);

if (isRightDiff) {
return true;
}
}

}


return !dfs(p, q);
};

// 시간복잡도 - O(n) p와 q가 같다면 모든 노드를 방문하므로
// 공간복잡도 - O(h) 깊이우선탐색을 사용하여 트리의 최대 높이만큼 실행환경이 함수호출스택에 저장되므로