Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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/Jeehay28.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// 🚀 Greedy Algorithm
// ✅ Time Complexity: O(n log n), where n is the number of intervals
// - Sorting the intervals: O(n log n)
// - Iterating through intervals: O(n)

// ✅ Space Complexity: O(1), No other data structures are used,

/**
* @param {number[][]} intervals
* @return {number}
*/
var eraseOverlapIntervals = function (intervals) {
// ✅ Sorting by end time ensures that we keep intervals that finish the earliest, reducing the chances of overlap with the subsequent intervals.
// ❌ Sorting by start time would lead to a greedy choice too early, causing unnecessary removals.
intervals.sort((a, b) => a[1] - b[1]);

let removalCnt = 0;

let prevEnd = intervals[0][1];

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

if (start < prevEnd) {
removalCnt += 1; // Increment removal count for an overlap
} else {
prevEnd = end;
}
}
return removalCnt;
};

76 changes: 76 additions & 0 deletions remove-nth-node-from-end-of-list/Jeehay28.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
// 🚀 Optimized Approach: Two-Pointer Method (One-Pass)
// ✅ Time Complexity: O(N), where N is the number of nodes
// ✅ 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) {
let dummy = new ListNode(0, head);
let fast = dummy;
let slow = dummy;

for (let i = 0; i <= n; i++) {
fast = fast.next;
}

while (fast) {
fast = fast.next;
slow = slow.next;
}

slow.next = slow.next.next;

return dummy.next;
};

// ✅ Time Complexity: O(N), where N is the number of nodes
// ✅ 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) {
// let length = 0;

// let node = head;

// // Compute the length of the linked list
// while (node) {
// length += 1;
// node = node.next;
// }

// // Create a dummy node pointing to head (helps handle edge cases)
// let dummy = new ListNode(0, head);
// node = dummy;

// // Move to the node just before the one to be removed
// for (let i = 0; i < length - n; i++) {
// node = node.next;
// }

// // Remove the nth node from the end by updating the next pointer
// node.next = node.next.next;

// // Return the modified linked list (excluding the dummy node)
// return dummy.next;
// };
69 changes: 69 additions & 0 deletions same-tree/Jeehay28.js
Copy link
Contributor

Choose a reason for hiding this comment

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

전 단순 재귀로 푸는것만 생각했는데 Stack을 사용하여 이런 멋진 방법도 고안해낼 수 있네요..!

Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// 🚀 Iterative DFS (Stack)
// ✅ Time Complexity: O(n), where n is the number of nodes in the tree
// ✅ Space Complexity: O(n) (worst case), O(log n) (best case for balanced trees)

/**
* 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) {
let stack = [[p, q]];
while (stack.length > 0) {
const [p, q] = stack.pop();

if (p === null && q === null) continue;

if (p === null || q === null) return false;

if (p.val !== q.val) return false;

stack.push([p.left, q.left]);
stack.push([p.right, q.right]);
}

return true;
};



// 🚀 recursive approach
// ✅ Time Complexity: O(n), where n is the number of nodes in the tree
// ✅ Space Complexity: O(n) (worst case), O(log n) (best case for balanced trees)

/**
* 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) {
// // Base case: If both trees are empty, they are the same
// if (p === null && q === null) return true;

// // If one of the trees is empty and the other is not, return false
// if (p === null || q === null) return false;

// // Compare the values of the current nodes
// if (p.val !== q.val) return false;

// // Recursively compare the left and right subtrees
// return isSameTree(p.left, q.left) && isSameTree(p.right, q.right);
// };