Skip to content

Commit a495e21

Browse files
authored
[JustDevRae] 25.02.22 (#47)
* remove smallest number / 以� * divisible numbers array / 以� * duplicate number count / 湲곗� * matrix addition / 以� * array element length / 湲곗� * rotate array / 湲곗� * slice array / 湲곗� * array algorithm md file * reverse string / 기초 * control Z / 기초 * dart game / 중급 * valid parentheses / 중급 * crane claw game / 중급 * pair count / 湲곗� * find point position / 湲곗� * login success / 湲곗� * card bundle / 以� * make hamburger / 以� * process / �ы� * morse code / 湲곗� * make B with A / 湲곗� * setting order care / 湲곗� * not finish runner / 以� * rank order / 湲곗� * sameTree / 湲곗� * invert binary tree / 湲곗� * maximum depth of binary / 湲곗� * binary tree inorder traversal / 湲곗� * binary tree level order traversal / 以� * 以났�� 臾몄� �帷굅 / 湲곗� * �� 踰�留� �깆媓�� 臾몄� / 湲곗� * 臾댁��¦濡� K媛黝� �� 戮린 / 湲곗� * ���몄���� / 以� * Contains Duplicate / 湲곗� * 寃��¦ 留� 理蕤�④굅由� / 以� * �¢寃� �� / 以� * 諛곕�� / �ы� * Find if Path Exists in Graph / 湲곗� * Find the Town Judge / 湲곗�
1 parent b4692f5 commit a495e21

File tree

5 files changed

+121
-0
lines changed

5 files changed

+121
-0
lines changed

JustDevRae/Graph/delivery.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
function solution(N, road, K) {
2+
const graph = Array.from({ length: N + 1 }, () => []);
3+
for (const [a, b, c] of road) {
4+
graph[a].push({ node: b, cost: c });
5+
graph[b].push({ node: a, cost: c });
6+
}
7+
8+
const distance = new Array(N + 1).fill(Infinity);
9+
distance[1] = 0;
10+
11+
const visited = new Array(N + 1).fill(false);
12+
13+
for (let i = 1; i <= N; i++) {
14+
let current = -1;
15+
16+
for (let j = 1; j <= N; j++) {
17+
if (!visited[j] && (current === -1 || distance[j] < distance[current])) {
18+
current = j;
19+
}
20+
}
21+
22+
if (current === -1) break;
23+
24+
visited[current] = true;
25+
26+
for (const { node, cost } of graph[current]) {
27+
if (distance[node] > distance[current] + cost) {
28+
distance[node] = distance[current] + cost;
29+
}
30+
}
31+
}
32+
return distance.filter((time) => time <= K).length;
33+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
var validPath = function (n, edges, source, destination) {
2+
const adjacencyList = new Array(n).fill(0).map(() => []);
3+
for (const [nodeA, nodeB] of edges) {
4+
adjacencyList[nodeA].push(nodeB);
5+
adjacencyList[nodeB].push(nodeA);
6+
}
7+
8+
const visitedNodes = new Array(n).fill(false);
9+
const bfsQueue = [source];
10+
visitedNodes[source] = true;
11+
12+
while (bfsQueue.length) {
13+
const currentNode = bfsQueue.shift();
14+
if (currentNode === destination) return true;
15+
16+
for (const adjacentNode of adjacencyList[currentNode]) {
17+
if (!visitedNodes[adjacentNode]) {
18+
visitedNodes[adjacentNode] = true;
19+
bfsQueue.push(adjacentNode);
20+
}
21+
}
22+
}
23+
24+
return false;
25+
};
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
var findJudge = function (n, trust) {
2+
if (n === 1) return 1;
3+
4+
const trustScore = new Array(n + 1).fill(0);
5+
6+
for (const [a, b] of trust) {
7+
trustScore[a]--;
8+
trustScore[b]++;
9+
}
10+
11+
for (let i = 1; i <= n; i++) {
12+
if (trustScore[i] === n - 1) return i;
13+
}
14+
15+
return -1;
16+
};
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
function solution(maps) {
2+
const n = maps.length;
3+
const m = maps[0].length;
4+
5+
const dx = [0, 0, 1, -1];
6+
const dy = [1, -1, 0, 0];
7+
8+
let queue = [];
9+
queue.push([0, 0]);
10+
11+
while (queue.length) {
12+
const [y, x] = queue.shift();
13+
14+
if (y === n - 1 && x === m - 1) {
15+
return maps[y][x];
16+
}
17+
18+
for (let i = 0; i < 4; i++) {
19+
const ny = y + dy[i];
20+
const nx = x + dx[i];
21+
22+
if (ny >= 0 && ny < n && nx >= 0 && nx < m && maps[ny][nx] === 1) {
23+
maps[ny][nx] = maps[y][x] + 1;
24+
queue.push([ny, nx]);
25+
}
26+
}
27+
}
28+
29+
return -1;
30+
}

JustDevRae/Graph/target_number.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
function solution(numbers, target) {
2+
let count = 0;
3+
4+
function DFS(index, currentSum) {
5+
if (index === numbers.length) {
6+
if (currentSum === target) count++;
7+
return;
8+
}
9+
10+
DFS(index + 1, currentSum + numbers[index]);
11+
DFS(index + 1, currentSum - numbers[index]);
12+
}
13+
14+
DFS(0, 0);
15+
16+
return count;
17+
}

0 commit comments

Comments
 (0)