diff --git a/non-overlapping-intervals/jdy8739.js b/non-overlapping-intervals/jdy8739.js new file mode 100644 index 000000000..d918c856b --- /dev/null +++ b/non-overlapping-intervals/jdy8739.js @@ -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) 특별한 자료구조가 사용되지 않아 상수 공간복잡도 발생 diff --git a/remove-nth-node-from-end-of-list/jdy8739.js b/remove-nth-node-from-end-of-list/jdy8739.js new file mode 100644 index 000000000..fa70d27f5 --- /dev/null +++ b/remove-nth-node-from-end-of-list/jdy8739.js @@ -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) - 순회 이후에 제거할 노드의 바로 앞 노드레 접근하기 위하여 모든 노드를 배열에 저장 diff --git a/same-tree/jdy8739.js b/same-tree/jdy8739.js new file mode 100644 index 000000000..88363498a --- /dev/null +++ b/same-tree/jdy8739.js @@ -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) 깊이우선탐색을 사용하여 트리의 최대 높이만큼 실행환경이 함수호출스택에 저장되므로