Skip to content

[hsskey] Week 12 solutions #1603

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 2 commits into from
Jun 22, 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
24 changes: 24 additions & 0 deletions non-overlapping-intervals/hsskey.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/**
* @param {number[][]} intervals
* @return {number}
*/
var eraseOverlapIntervals = function(intervals) {
intervals.sort((a, b) => a[0] - b[0]);

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

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

if (start >= prevEnd) {
prevEnd = end;
} else {
res += 1;
prevEnd = Math.min(end, prevEnd);
}
}

return res;
};

44 changes: 44 additions & 0 deletions number-of-connected-components-in-an-undirected-graph/hsskey.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
export class Solution {
/**
* @param {number} n - the number of vertices
* @param {number[][]} edges - the edges of undirected graph
* @return {number} - the number of connected components
*/
countComponents(n, edges) {
const par = Array.from({ length: n }, (_, i) => i);
const rank = Array(n).fill(1);

const find = (n1) => {
let res = n1;
while (res !== par[res]) {
par[res] = par[par[res]]; // path compression
res = par[res];
}
return res;
};

const union = (n1, n2) => {
const p1 = find(n1);
const p2 = find(n2);
if (p1 === p2) return 0;

if (rank[p2] > rank[p1]) {
par[p1] = p2;
rank[p2] += rank[p1];
} else {
par[p2] = p1;
rank[p1] += rank[p2];
}

return 1;
};

let res = n;
for (const [n1, n2] of edges) {
res -= union(n1, n2);
}

return res;
}
}

36 changes: 36 additions & 0 deletions remove-nth-node-from-end-of-list/hsskey.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/**
* 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 dummy = new ListNode(0, head);
let left = dummy;
let right = head;

// advance right pointer n steps ahead
while (n > 0 && right) {
right = right.next;
n--;
}

// move both pointers until right reaches the end
while (right) {
left = left.next;
right = right.next;
}

// delete the nth node from end
left.next = left.next.next;

return dummy.next;
};

24 changes: 24 additions & 0 deletions same-tree/hsskey.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/**
* 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) {
if (!p && !q) {
return true;
}
if (!p || !q || p.val !== q.val) {
return false;
}
return isSameTree(p.left, q.left) && isSameTree(p.right, q.right);
};

56 changes: 56 additions & 0 deletions serialize-and-deserialize-binary-tree/hsskey.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/**
* Definition for a binary tree node.
* function TreeNode(val) {
* this.val = val;
* this.left = this.right = null;
* }
*/

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

const dfs = (node) => {
if (!node) {
res.push("N");
return;
}
res.push(String(node.val));
dfs(node.left);
dfs(node.right);
};

dfs(root);
return res.join(".");
};

/**
* Decodes your encoded data to tree.
*
* @param {string} data
* @return {TreeNode}
*/
var deserialize = function(data) {
const vals = data.split(".");
let i = 0;

const dfs = () => {
if (vals[i] === "N") {
i++;
return null;
}
const node = new TreeNode(parseInt(vals[i]));
i++;
node.left = dfs();
node.right = dfs();
return node;
};

return dfs();
};