diff --git "a/oh-chaeyeon/7\354\243\274\354\260\250_\352\267\270\353\236\230\355\224\204/Find_Center_of_Star_Graph.js" "b/oh-chaeyeon/7\354\243\274\354\260\250_\352\267\270\353\236\230\355\224\204/Find_Center_of_Star_Graph.js" new file mode 100644 index 0000000..6841e5c --- /dev/null +++ "b/oh-chaeyeon/7\354\243\274\354\260\250_\352\267\270\353\236\230\355\224\204/Find_Center_of_Star_Graph.js" @@ -0,0 +1,9 @@ +/** + * @param {number[][]} edges + * @return {number} + */ +var findCenter = function (edges) { + return edges[0][0] === edges[1][0] || edges[0][0] === edges[1][1] + ? edges[0][0] + : edges[0][1]; +}; diff --git "a/oh-chaeyeon/7\354\243\274\354\260\250_\352\267\270\353\236\230\355\224\204/Find_if_Path_Exists_in_Graph.js" "b/oh-chaeyeon/7\354\243\274\354\260\250_\352\267\270\353\236\230\355\224\204/Find_if_Path_Exists_in_Graph.js" new file mode 100644 index 0000000..f7c4fea --- /dev/null +++ "b/oh-chaeyeon/7\354\243\274\354\260\250_\352\267\270\353\236\230\355\224\204/Find_if_Path_Exists_in_Graph.js" @@ -0,0 +1,32 @@ +/** + * @param {number} n + * @param {number[][]} edges + * @param {number} source + * @param {number} destination + * @return {boolean} + */ +var validPath = function (n, edges, source, destination) { + const graph = Array.from({ length: n }, () => []); + + edges.forEach(([u, v]) => { + graph[u].push(v); + graph[v].push(u); + }); + + const visited = new Set(); + + const dfs = (node) => { + if (node === destination) return true; + visited.add(node); + + for (const neighbor of graph[node]) { + if (!visited.has(neighbor) && dfs(neighbor)) { + return true; + } + } + + return false; + }; + + return dfs(source); +}; diff --git "a/oh-chaeyeon/7\354\243\274\354\260\250_\352\267\270\353\236\230\355\224\204/Find_the_Town_Judge.js" "b/oh-chaeyeon/7\354\243\274\354\260\250_\352\267\270\353\236\230\355\224\204/Find_the_Town_Judge.js" new file mode 100644 index 0000000..27f697c --- /dev/null +++ "b/oh-chaeyeon/7\354\243\274\354\260\250_\352\267\270\353\236\230\355\224\204/Find_the_Town_Judge.js" @@ -0,0 +1,21 @@ +/** + * @param {number} n + * @param {number[][]} trust + * @return {number} + */ +var findJudge = function (n, trust) { + const trustCount = new Array(n + 1).fill(0); + + for (const [a, b] of trust) { + trustCount[a]--; + trustCount[b]++; + } + + for (let i = 1; i <= n; i++) { + if (trustCount[i] === n - 1) { + return i; + } + } + + return -1; +}; diff --git "a/oh-chaeyeon/7\354\243\274\354\260\250_\352\267\270\353\236\230\355\224\204/\352\262\214\354\236\204_\353\247\265_\354\265\234\353\213\250\352\261\260\353\246\254.js" "b/oh-chaeyeon/7\354\243\274\354\260\250_\352\267\270\353\236\230\355\224\204/\352\262\214\354\236\204_\353\247\265_\354\265\234\353\213\250\352\261\260\353\246\254.js" new file mode 100644 index 0000000..e702c95 --- /dev/null +++ "b/oh-chaeyeon/7\354\243\274\354\260\250_\352\267\270\353\236\230\355\224\204/\352\262\214\354\236\204_\353\247\265_\354\265\234\353\213\250\352\261\260\353\246\254.js" @@ -0,0 +1,45 @@ +function solution(maps) { + const n = maps.length; + const m = maps[0].length; + const directions = [ + [1, 0], + [-1, 0], + [0, 1], + [0, -1], + ]; + const queue = [[0, 0]]; + const visited = Array.from({ length: n }, () => Array(m).fill(false)); + visited[0][0] = true; + let count = 1; + + while (queue.length > 0) { + const size = queue.length; + for (let i = 0; i < size; i++) { + const [x, y] = queue.shift(); + + if (x === n - 1 && y === m - 1) { + return count; + } + + for (const [dx, dy] of directions) { + const nx = x + dx; + const ny = y + dy; + + if ( + nx >= 0 && + ny >= 0 && + nx < n && + ny < m && + maps[nx][ny] === 1 && + !visited[nx][ny] + ) { + visited[nx][ny] = true; + queue.push([nx, ny]); + } + } + } + count++; + } + + return -1; +} diff --git "a/oh-chaeyeon/7\354\243\274\354\260\250_\352\267\270\353\236\230\355\224\204/\353\260\260\353\213\254.js" "b/oh-chaeyeon/7\354\243\274\354\260\250_\352\267\270\353\236\230\355\224\204/\353\260\260\353\213\254.js" new file mode 100644 index 0000000..01a4c65 --- /dev/null +++ "b/oh-chaeyeon/7\354\243\274\354\260\250_\352\267\270\353\236\230\355\224\204/\353\260\260\353\213\254.js" @@ -0,0 +1,31 @@ +function solution(N, road, K) { + const graph = Array.from({ length: N + 1 }, () => []); + + for (const [a, b, c] of road) { + graph[a].push([b, c]); + graph[b].push([a, c]); + } + + const distances = Array(N + 1).fill(Infinity); + distances[1] = 0; + const priorityQueue = [[0, 1]]; + + while (priorityQueue.length > 0) { + const [currentDistance, currentNode] = priorityQueue.shift(); + + if (currentDistance > distances[currentNode]) { + continue; + } + + for (const [neighbor, time] of graph[currentNode]) { + const newDistance = currentDistance + time; + if (newDistance < distances[neighbor]) { + distances[neighbor] = newDistance; + priorityQueue.push([newDistance, neighbor]); + priorityQueue.sort((a, b) => a[0] - b[0]); + } + } + } + + return distances.filter((distance) => distance <= K).length; +} diff --git "a/oh-chaeyeon/7\354\243\274\354\260\250_\352\267\270\353\236\230\355\224\204/\354\240\204\353\240\245\353\247\235\354\235\204_\353\217\214\353\241\234_\353\202\230\353\210\204\352\270\260.js" "b/oh-chaeyeon/7\354\243\274\354\260\250_\352\267\270\353\236\230\355\224\204/\354\240\204\353\240\245\353\247\235\354\235\204_\353\217\214\353\241\234_\353\202\230\353\210\204\352\270\260.js" new file mode 100644 index 0000000..4b26735 --- /dev/null +++ "b/oh-chaeyeon/7\354\243\274\354\260\250_\352\267\270\353\236\230\355\224\204/\354\240\204\353\240\245\353\247\235\354\235\204_\353\217\214\353\241\234_\353\202\230\353\210\204\352\270\260.js" @@ -0,0 +1,33 @@ +function solution(n, wires) { + const graph = Array.from({ length: n + 1 }, () => []); + + for (const [v1, v2] of wires) { + graph[v1].push(v2); + graph[v2].push(v1); + } + + let minDifference = Number.MAX_VALUE; + + const dfs = (node, visited) => { + let count = 1; + visited[node] = true; + + for (const neighbor of graph[node]) { + if (!visited[neighbor]) { + count += dfs(neighbor, visited); + } + } + + return count; + }; + + for (const [v1, v2] of wires) { + const visited = Array(n + 1).fill(false); + visited[v1] = true; + const count = dfs(v2, visited); + const difference = Math.abs(count - (n - count)); + minDifference = Math.min(minDifference, difference); + } + + return minDifference; +} diff --git "a/oh-chaeyeon/7\354\243\274\354\260\250_\352\267\270\353\236\230\355\224\204/\355\203\200\354\274\223_\353\204\230\353\262\204.js" "b/oh-chaeyeon/7\354\243\274\354\260\250_\352\267\270\353\236\230\355\224\204/\355\203\200\354\274\223_\353\204\230\353\262\204.js" new file mode 100644 index 0000000..2496c77 --- /dev/null +++ "b/oh-chaeyeon/7\354\243\274\354\260\250_\352\267\270\353\236\230\355\224\204/\355\203\200\354\274\223_\353\204\230\353\262\204.js" @@ -0,0 +1,18 @@ +function solution(numbers, target) { + const dp = new Map(); + dp.set(0, 1); + + for (const number of numbers) { + const nextDp = new Map(); + for (const [sum, count] of dp) { + nextDp.set(sum + number, (nextDp.get(sum + number) || 0) + count); + nextDp.set(sum - number, (nextDp.get(sum - number) || 0) + count); + } + dp.clear(); + for (const [sum, count] of nextDp) { + dp.set(sum, count); + } + } + + return dp.get(target) || 0; +}