Skip to content

[환미니니] Week12 Solutions #571

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 3 commits into from
Nov 3, 2024
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
41 changes: 41 additions & 0 deletions number-of-connected-components-in-an-undirected-graph/hwanmini.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// V는 정점, E는 간선
// 시간 복잡도: O(V + E)
// 공간 복잡도: O(V + E)

class Solution {
/**
* @param {number} n
* @param {number[][]} edges
* @returns {number}
*/
countComponents(n, edges) {
const visited = Array.from({length: n}, () => false);
const graph = new Map();

for (const [v, d] of edges) {
if (!graph.has(v)) graph.set(v, []);
if (!graph.has(d)) graph.set(d, []);

graph.get(v).push(d);
graph.get(d).push(v);
}

const dfs = (node) => {
visited[node] = true
for (let nei of graph.get(node) || []) {
if (!visited[nei]) dfs(nei)
}
}

let count = 0;
for (let i = 0; i < n; i++) {
if (!visited[i]) {
dfs(i);
count++;
}
}


return count
}
}
23 changes: 23 additions & 0 deletions same-tree/hwanmini.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// 시간복잡도: O(n)
// 공간복잡도: O(n)

/**
* 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) || (!p && q)) return false;
if (!p || !q) return true
if (p.val === q.val) return isSameTree(p.left, q.left) && isSameTree(p.right, q.right)
return false
};