Skip to content

Commit bbf780e

Browse files
authored
[oh-chaeyeon] 25.02.20 (#46)
* 배열 자르기 / 기초 * 배열 원소의 길이 / 기초 * 배열 회전시키기 / 기초 * 중복된 숫자 개수 / 기초 * 제일 작은 수 제거하기 / 중급 * 행렬의 덧셈 / 중급 * 나누어 떨어지는 숫자 배열 / 중급 * 행렬의 곱셈 / 심화 * 교점에 별 만들기 / 심화 * n^2 배열 자르기 / 심화 * String Reversal / 기초 * Control Z / 기초 * Hate English / 기초 * String Calculation / 기초 * Crane Game / 중급 * Valid Parentheses / 중급 * Dart Game / 중급 * Rotate Parentheses / 심화 * Stock Price / 심화 * Delivery Box / 심화 * 순서쌍의 개수 / 기초 * 점의 위치 구하기 / 기초 * 로그인 성공? / 기초 * 특이한 정렬 / 기초 * 카드 뭉치 / 중급 * 공원 산책 / 중급 * 햄버거 만들기 / 중급 * 모스부호1 / 기초 * A로 B만들기 / 기초 * 진로 순서 정하기 / 기초 * 등수 매기기 / 기초 * 완주하지 못한 선수 / 중급 * 포켓몬 / 중급 * 추억 점수 / 중급 * 할인 행사 / 심화 * 오픈채팅방 / 심화 * Maximum Depth of Binary Tree / 초급 * Binary Tree Inorder Traversal / 초급 * Same Tree / 초급 * Invert Binary Tree / 초급 * Binary Tree Level Order Traversal / 중급 * Validate Binary Search Tree / 중급 * Construct Binary Tree from Preorder and Inorder Traversal / 중급 * 이진 변환 반복하기 / 심화 * Binary Tree Maximum Path Sum / 심화 * Find the Town Judge / 기초 * Find if Path Exists in Graph / 기초 * Find Center of Star Graph / 기초 * 게임 맵 최단거리 / 중급 * 타겟 넘버 / 중급 * 전력망을 둘로 나누기 / 심화 * 배달 / 심화
1 parent a495e21 commit bbf780e

File tree

7 files changed

+189
-0
lines changed

7 files changed

+189
-0
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/**
2+
* @param {number[][]} edges
3+
* @return {number}
4+
*/
5+
var findCenter = function (edges) {
6+
return edges[0][0] === edges[1][0] || edges[0][0] === edges[1][1]
7+
? edges[0][0]
8+
: edges[0][1];
9+
};
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* @param {number} n
3+
* @param {number[][]} edges
4+
* @param {number} source
5+
* @param {number} destination
6+
* @return {boolean}
7+
*/
8+
var validPath = function (n, edges, source, destination) {
9+
const graph = Array.from({ length: n }, () => []);
10+
11+
edges.forEach(([u, v]) => {
12+
graph[u].push(v);
13+
graph[v].push(u);
14+
});
15+
16+
const visited = new Set();
17+
18+
const dfs = (node) => {
19+
if (node === destination) return true;
20+
visited.add(node);
21+
22+
for (const neighbor of graph[node]) {
23+
if (!visited.has(neighbor) && dfs(neighbor)) {
24+
return true;
25+
}
26+
}
27+
28+
return false;
29+
};
30+
31+
return dfs(source);
32+
};
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* @param {number} n
3+
* @param {number[][]} trust
4+
* @return {number}
5+
*/
6+
var findJudge = function (n, trust) {
7+
const trustCount = new Array(n + 1).fill(0);
8+
9+
for (const [a, b] of trust) {
10+
trustCount[a]--;
11+
trustCount[b]++;
12+
}
13+
14+
for (let i = 1; i <= n; i++) {
15+
if (trustCount[i] === n - 1) {
16+
return i;
17+
}
18+
}
19+
20+
return -1;
21+
};
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
function solution(maps) {
2+
const n = maps.length;
3+
const m = maps[0].length;
4+
const directions = [
5+
[1, 0],
6+
[-1, 0],
7+
[0, 1],
8+
[0, -1],
9+
];
10+
const queue = [[0, 0]];
11+
const visited = Array.from({ length: n }, () => Array(m).fill(false));
12+
visited[0][0] = true;
13+
let count = 1;
14+
15+
while (queue.length > 0) {
16+
const size = queue.length;
17+
for (let i = 0; i < size; i++) {
18+
const [x, y] = queue.shift();
19+
20+
if (x === n - 1 && y === m - 1) {
21+
return count;
22+
}
23+
24+
for (const [dx, dy] of directions) {
25+
const nx = x + dx;
26+
const ny = y + dy;
27+
28+
if (
29+
nx >= 0 &&
30+
ny >= 0 &&
31+
nx < n &&
32+
ny < m &&
33+
maps[nx][ny] === 1 &&
34+
!visited[nx][ny]
35+
) {
36+
visited[nx][ny] = true;
37+
queue.push([nx, ny]);
38+
}
39+
}
40+
}
41+
count++;
42+
}
43+
44+
return -1;
45+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
function solution(N, road, K) {
2+
const graph = Array.from({ length: N + 1 }, () => []);
3+
4+
for (const [a, b, c] of road) {
5+
graph[a].push([b, c]);
6+
graph[b].push([a, c]);
7+
}
8+
9+
const distances = Array(N + 1).fill(Infinity);
10+
distances[1] = 0;
11+
const priorityQueue = [[0, 1]];
12+
13+
while (priorityQueue.length > 0) {
14+
const [currentDistance, currentNode] = priorityQueue.shift();
15+
16+
if (currentDistance > distances[currentNode]) {
17+
continue;
18+
}
19+
20+
for (const [neighbor, time] of graph[currentNode]) {
21+
const newDistance = currentDistance + time;
22+
if (newDistance < distances[neighbor]) {
23+
distances[neighbor] = newDistance;
24+
priorityQueue.push([newDistance, neighbor]);
25+
priorityQueue.sort((a, b) => a[0] - b[0]);
26+
}
27+
}
28+
}
29+
30+
return distances.filter((distance) => distance <= K).length;
31+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
function solution(n, wires) {
2+
const graph = Array.from({ length: n + 1 }, () => []);
3+
4+
for (const [v1, v2] of wires) {
5+
graph[v1].push(v2);
6+
graph[v2].push(v1);
7+
}
8+
9+
let minDifference = Number.MAX_VALUE;
10+
11+
const dfs = (node, visited) => {
12+
let count = 1;
13+
visited[node] = true;
14+
15+
for (const neighbor of graph[node]) {
16+
if (!visited[neighbor]) {
17+
count += dfs(neighbor, visited);
18+
}
19+
}
20+
21+
return count;
22+
};
23+
24+
for (const [v1, v2] of wires) {
25+
const visited = Array(n + 1).fill(false);
26+
visited[v1] = true;
27+
const count = dfs(v2, visited);
28+
const difference = Math.abs(count - (n - count));
29+
minDifference = Math.min(minDifference, difference);
30+
}
31+
32+
return minDifference;
33+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
function solution(numbers, target) {
2+
const dp = new Map();
3+
dp.set(0, 1);
4+
5+
for (const number of numbers) {
6+
const nextDp = new Map();
7+
for (const [sum, count] of dp) {
8+
nextDp.set(sum + number, (nextDp.get(sum + number) || 0) + count);
9+
nextDp.set(sum - number, (nextDp.get(sum - number) || 0) + count);
10+
}
11+
dp.clear();
12+
for (const [sum, count] of nextDp) {
13+
dp.set(sum, count);
14+
}
15+
}
16+
17+
return dp.get(target) || 0;
18+
}

0 commit comments

Comments
 (0)