-
-
Notifications
You must be signed in to change notification settings - Fork 248
[Sehwan] Week10 solution with JavaScript #165
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 all commits
Commits
Show all changes
5 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,46 @@ | ||
class Solution { | ||
/** | ||
* @param {number} n | ||
* @param {number[][]} edges | ||
* @returns {boolean} | ||
*/ | ||
validTree(n, edges) { | ||
// A valid tree must have exactly n - 1 edges | ||
if (edges.length !== n - 1) { | ||
return false; | ||
} | ||
|
||
// Initialize the adjacency list | ||
let graph = []; | ||
for (let i = 0; i < n; i++) { | ||
graph.push([]); | ||
} | ||
|
||
// Populate the adjacency list with edges | ||
for (let [node, neighbor] of edges) { | ||
graph[node].push(neighbor); | ||
graph[neighbor].push(node); | ||
} | ||
|
||
let visited = new Set(); | ||
|
||
// Depth-First Search (DFS) to explore the graph | ||
function dfs(node) { | ||
visited.add(node); | ||
for (let neighbor of graph[node]) { | ||
if (!visited.has(neighbor)) { | ||
dfs(neighbor); | ||
} | ||
} | ||
} | ||
|
||
// Start DFS from node 0 | ||
dfs(0); | ||
|
||
// Check if all nodes were visited | ||
return visited.size === n; | ||
} | ||
} | ||
|
||
// TC: O(n) | ||
// SC: O(n) |
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,20 @@ | ||
var rob = function (nums) { | ||
// edge case | ||
if (nums.length === 1) return nums[0]; | ||
|
||
const dp = (start, end) => { | ||
let prev = 0, | ||
curr = 0; | ||
for (let i = start; i < end; i++) { | ||
let temp = curr; | ||
curr = Math.max(nums[i] + prev, curr); | ||
prev = temp; | ||
} | ||
return curr; | ||
}; | ||
|
||
return Math.max(dp(0, nums.length - 1), dp(1, nums.length)); | ||
}; | ||
|
||
// TC: O(n) | ||
// SC: O(1) |
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,16 @@ | ||
var rob = function (nums) { | ||
// dynamic programming | ||
let prev = 0, | ||
curr = 0; | ||
|
||
for (let num of nums) { | ||
let temp = curr; | ||
curr = Math.max(num + prev, curr); | ||
prev = temp; | ||
} | ||
|
||
return curr; | ||
}; | ||
|
||
// TC: O(n) | ||
// SC: O(1) |
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,32 @@ | ||
var longestPalindrome = function (s) { | ||
let maxStart = 0, | ||
maxEnd = 0; | ||
|
||
for (let i = 0; i < s.length; i++) { | ||
let start = i, | ||
end = i; | ||
while (start >= 0 && end < s.length && s[start] === s[end]) { | ||
if (end - start > maxEnd - maxStart) { | ||
maxStart = start; | ||
maxEnd = end; | ||
} | ||
start--; | ||
end++; | ||
} | ||
|
||
(start = i), (end = i + 1); | ||
while (start >= 0 && end < s.length && s[start] === s[end]) { | ||
if (end - start > maxEnd - maxStart) { | ||
maxStart = start; | ||
maxEnd = end; | ||
} | ||
start--; | ||
end++; | ||
} | ||
} | ||
|
||
return s.slice(maxStart, maxEnd + 1); | ||
}; | ||
|
||
// TC: O(n^2) | ||
// SC: O(1) |
38 changes: 38 additions & 0 deletions
38
number-of-connected-components-in-an-undirected-graph/nhistory.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,38 @@ | ||
export class Solution { | ||
/** | ||
* @param n: the number of vertices | ||
* @param edges: the edges of undirected graph | ||
* @return: the number of connected components | ||
*/ | ||
countComponents(n, edges) { | ||
// write your code here | ||
const graph = Array.from({ length: n }, () => []); | ||
|
||
for (const [node, neighbor] of edges) { | ||
graph[node].push(neighbor); | ||
graph[neighbor].push(node); | ||
} | ||
|
||
let visited = new Set(); | ||
let count = 0; | ||
|
||
const dfs = (node) => { | ||
visited.add(node); | ||
for (const nei of graph[node]) { | ||
if (!visited.has(nei)) dfs(nei); | ||
} | ||
}; | ||
|
||
for (let node = 0; node < n; node++) { | ||
if (!visited.has(node)) { | ||
count++; | ||
dfs(node); | ||
} | ||
} | ||
return count; | ||
} | ||
} | ||
|
||
// n: number of node | e: number of edge | ||
// TC: O(n+e) | ||
// SC: O(n+e) |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
오 저는 이 조건으로 거를 생각은 못했네요. 👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
달레님 블로그를 참고했습니다 ㅎㅎ