-
-
Notifications
You must be signed in to change notification settings - Fork 236
[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
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
44
number-of-connected-components-in-an-undirected-graph/hsskey.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}; | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}; | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.