Skip to content

Commit 3bfce03

Browse files
authored
[moonjonghoo]25.02.20 (#48)
* String_Reversal / 기초 * control_z / 기초 * I_don't_like_English /기초 * String_Calculation / 기초 * Crane_Doll_Picking_Game / 중급 * 순서쌍의_개수 / 기초 * 점의_위치_구하기 / 기초 * 로그인_성공? / 기초 * 특이한_정렬 / 기초 * 프로세스 / 고급 * 프로세스 /고급 수정 * 모스부호(1) / 기초 * A로 B만들기 /기초 * 진료순서/ 기초 * 등수매기기 /기초 * 완주하지 못한 선수 /중급 * maximum_depth_of_binary_tree / 기초 * binary_tree_inorder_traversal /기초 * same_tree /기초 * Invert_Binary_tree/기초 * 중복된_문자_제거 /기초 * 한_번만_등장한_문자 /기초 * 소인수분해 /중급 * 무작위로_K개의_수_뽑기 / 기초 * contains_duplicate /기초 * intersection_of_two_array /기초 * count_the_number_of_consistent_strings /기초 * design_hashset/기초 * repeated_dna_sequences /중급 * longest_consecutive_sequence /중급 * DFS와_BFS /기초 * 전력망을_둘로_나누기 /심화 * 게임_맵_최단거리 /중급 * 타겟_넘버 /중급
1 parent bbf780e commit 3bfce03

12 files changed

+297
-0
lines changed

.vscode/launch.json

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
// IntelliSense를 사용하여 가능한 특성에 대해 알아보세요.
3+
// 기존 특성에 대한 설명을 보려면 가리킵니다.
4+
// 자세한 내용을 보려면 https://go.microsoft.com/fwlink/?linkid=830387을(를) 방문하세요.
5+
"version": "0.2.0",
6+
"configurations": [
7+
{
8+
"type": "chrome",
9+
"request": "launch",
10+
"name": "localhost에 대해 Chrome 시작",
11+
"url": "http://localhost:8080",
12+
"webRoot": "${workspaceFolder}"
13+
}
14+
]
15+
}

Moonjonghoo/graph/DFS와_BFS.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
const fs = require("fs");
2+
const input = fs.readFileSync("/dev/stdin").toString().trim().split("\n");
3+
4+
const [N, M, V] = input[0].split(" ").map(Number);
5+
const edges = input.slice(1).map((line) => line.split(" ").map(Number));
6+
7+
const graph = Array.from({ length: N + 1 }, () => []);
8+
edges.forEach(([a, b]) => {
9+
graph[a].push(b);
10+
graph[b].push(a);
11+
});
12+
graph.forEach((node) => node.sort((a, b) => a - b));
13+
14+
const dfs = (v, visited = []) => {
15+
if (visited.includes(v)) return;
16+
visited.push(v);
17+
graph[v].forEach((n) => dfs(n, visited));
18+
return visited;
19+
};
20+
21+
const bfs = (v) => {
22+
let queue = [v],
23+
visited = [];
24+
while (queue.length) {
25+
let node = queue.shift();
26+
if (!visited.includes(node)) {
27+
visited.push(node);
28+
queue.push(...graph[node].filter((n) => !visited.includes(n)));
29+
}
30+
}
31+
return visited;
32+
};
33+
34+
console.log(dfs(V, []).join(" "));
35+
console.log(bfs(V).join(" "));
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
function solution(maps) {
2+
let n = maps.length;
3+
let m = maps[0].length;
4+
let answer = 1;
5+
let visited = maps;
6+
const dx = [-1, 1, 0, 0];
7+
const dy = [0, 0, -1, 1];
8+
let queue = [];
9+
queue.push([0, 0]);
10+
visited[0][0] = 0;
11+
12+
while (queue.length > 0) {
13+
let size = queue.length;
14+
15+
for (let i = 0; i < size; i++) {
16+
let [x, y] = queue.shift();
17+
18+
for (let j = 0; j < 4; j++) {
19+
let nx = x + dx[j];
20+
let ny = y + dy[j];
21+
22+
if (nx >= 0 && nx < n && ny >= 0 && ny < m && visited[nx][ny] === 1) {
23+
if (nx == n - 1 && ny == m - 1) {
24+
return ++answer;
25+
}
26+
queue.push([nx, ny]);
27+
visited[nx][ny] = 0;
28+
}
29+
}
30+
}
31+
answer++;
32+
}
33+
return -1;
34+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
function solution(n, wires) {
2+
let answer = Infinity;
3+
4+
function dfs(graph, start, visited) {
5+
visited[start] = true;
6+
let count = 1;
7+
8+
for (let i = 0; i < graph[start].length; i++) {
9+
const neighbor = graph[start][i];
10+
if (!visited[neighbor]) {
11+
count += dfs(graph, neighbor, visited);
12+
}
13+
}
14+
15+
return count;
16+
}
17+
18+
for (let i = 0; i < n - 1; i++) {
19+
const graph = Array(n)
20+
.fill(null)
21+
.map(() => []);
22+
for (let j = 0; j < n - 1; j++) {
23+
if (i === j) continue;
24+
const [a, b] = wires[j];
25+
graph[a - 1].push(b - 1);
26+
graph[b - 1].push(a - 1);
27+
}
28+
29+
const visited = Array(n).fill(false);
30+
let count1 = 0;
31+
let count2 = 0;
32+
33+
for (let j = 0; j < n; j++) {
34+
if (!visited[j]) {
35+
if (count1 === 0) {
36+
count1 = dfs(graph, j, visited);
37+
} else {
38+
count2 = dfs(graph, j, visited);
39+
}
40+
}
41+
}
42+
43+
const diff = Math.abs(count1 - count2);
44+
answer = Math.min(answer, diff);
45+
}
46+
47+
return answer;
48+
}

Moonjonghoo/graph/타겟_넘버.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
function solution(numbers, target) {
2+
let n = numbers.length;
3+
let check = Array.from({ length: n }, () => false);
4+
let answer = 0;
5+
function DFS(v) {
6+
if (v === n) {
7+
let sum = 0;
8+
for (let i = 0; i < n; i++) {
9+
if (check[i] === true) {
10+
sum += numbers[i];
11+
} else if (check[i] === false) {
12+
sum -= numbers[i];
13+
}
14+
}
15+
if (sum === target) answer++;
16+
} else {
17+
check[v] = true;
18+
DFS(v + 1);
19+
check[v] = false;
20+
DFS(v + 1);
21+
}
22+
}
23+
DFS(0);
24+
return answer;
25+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {boolean}
4+
*/
5+
var containsDuplicate = function (nums) {
6+
let hash = new Set();
7+
for (let i = 0; i < nums.length; i++) {
8+
if (!hash.has(nums[i])) {
9+
hash.add(nums[i]);
10+
}
11+
}
12+
if (hash.size !== nums.length) return true;
13+
else {
14+
return false;
15+
}
16+
};
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/**
2+
* @param {number[]} nums1
3+
* @param {number[]} nums2
4+
* @return {number[]}
5+
*/
6+
var intersection = function (nums1, nums2) {
7+
let hash = new Set(nums2);
8+
const intersectionSet = new Set([...nums1].filter((x) => hash.has(x)));
9+
return [...intersectionSet];
10+
};
11+
12+
console.log(intersection([1, 2, 2, 1], [2, 2]));
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* @param {string} allowed
3+
* @param {string[]} words
4+
* @return {number}
5+
*/
6+
var countConsistentStrings = function (allowed, words) {
7+
let hash = new Set(allowed.split(""));
8+
let answer = words.length;
9+
console.log(hash);
10+
for (let i = 0; i < words.length; i++) {
11+
for (let j = 0; j < words[i].length; j++) {
12+
if (!hash.has(words[i][j])) {
13+
answer--;
14+
break;
15+
}
16+
}
17+
}
18+
return answer;
19+
};
20+
21+
console.log(countConsistentStrings("ab", ["ad", "bd", "aaab", "baa", "badab"]));

Moonjonghoo/set/design_hashset.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
var MyHashSet = function () {
2+
this.data = [];
3+
};
4+
5+
/**
6+
* @param {number} key
7+
* @return {void}
8+
*/
9+
MyHashSet.prototype.add = function (key) {
10+
if (!this.data.includes(key)) {
11+
this.data.push(key);
12+
}
13+
};
14+
15+
/**
16+
* @param {number} key
17+
* @return {void}
18+
*/
19+
MyHashSet.prototype.remove = function (key) {
20+
this.data = this.data.filter((x) => x !== key);
21+
};
22+
23+
/**
24+
* @param {number} key
25+
* @return {boolean}
26+
*/
27+
MyHashSet.prototype.contains = function (key) {
28+
if (this.data.includes(key)) {
29+
return true;
30+
} else {
31+
return false;
32+
}
33+
};
34+
35+
let hash = new MyHashSet();
36+
hash.add(2);
37+
console.log(hash);
38+
console.log(hash.contains(2));
39+
hash.remove(2);
40+
console.log(hash);
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {number}
4+
*/
5+
var longestConsecutive = function (nums) {
6+
nums.sort((a, b) => a - b);
7+
let count = 1;
8+
let answer = 0;
9+
let max = 0;
10+
console.log(nums);
11+
while (true) {
12+
if (max === nums.length) return answer;
13+
if (nums[max + 1] - 1 === nums[max]) {
14+
max++;
15+
count++;
16+
} else {
17+
max++;
18+
answer = Math.max(answer, count);
19+
}
20+
}
21+
};
22+
23+
console.log(longestConsecutive([9, 1, 4, 7, 3, -1, 0, 5, 8, -1, 6]));

0 commit comments

Comments
 (0)