Skip to content
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
24 changes: 24 additions & 0 deletions Jiho/Day23/백준_10816.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
const fs = require("fs");
const input = fs
.readFileSync(process.platform === "linux" ? "/dev/stdin" : "../input.txt")
.toString()
.trim()
.split("\n");

const N = parseInt(input[0]);
const carr = input[1].trim().split(" ").map(Number);
const M = parseInt(input[2]);
const nums = input[3].trim().split(" ").map(Number);

// Map 만들기
let map = new Map();
for (const val of carr) {
map.set(val, (map.get(val) | 0) + 1);
}

// 결과 만들기
const result = [];
for (const num of nums) {
result.push(map.get(num) | 0);
}
console.log(result.join(" "));
63 changes: 63 additions & 0 deletions Jiho/Day23/백준_4963.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
const fs = require("fs");
const input = fs
.readFileSync(process.platform === "linux" ? "/dev/stdin" : "../input.txt")
.toString()
.trim()
.split("\n");

// 저장
let answers = [];
while (true) {
const [w, h] = input.shift()?.split(" ").map(Number);

if (w === 0 && h === 0) {
break;
}
let map = Array.from(new Array(h), () => new Array(w));
let visited = Array.from(new Array(h), () => new Array(w).fill(false));

for (let i = 0; i < h; i++) {
const arr = input.shift()?.split(" ").map(Number);
for (let j = 0; j < w; j++) {
map[i][j] = arr[j];
}
}

const BFS_FindConnected = (startRow, startCol, map, visited) => {
const dr = [0, 1, 1, 1, 0, -1, -1, -1];
const dc = [1, 1, 0, -1, -1, -1, 0, 1];
visited[startRow][startCol] = true;
let queue = [[startRow, startCol]];
while (queue.length) {
const [row, col] = queue.shift();

for (let i = 0; i < 8; i++) {
const [nrow, ncol] = [row + dr[i], col + dc[i]];
if (
nrow >= 0 &&
nrow < h &&
ncol >= 0 &&
ncol < w &&
!visited[nrow][ncol] &&
map[nrow][ncol] === 1
) {
visited[nrow][ncol] = true;
queue.push([nrow, ncol]);
}
}
}
};

// Execute BFS
let answer = 0;
for (let row = 0; row < h; row++) {
for (let col = 0; col < w; col++) {
if (!visited[row][col] && map[row][col] === 1) {
answer++;
BFS_FindConnected(row, col, map, visited);
}
}
}
answers.push(answer);
}
console.log(answers.join("\n"));
30 changes: 30 additions & 0 deletions Jiho/Day24/Leetcode_39. Combination Sum.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
var combinationSum = function (candidates, target) {
const len = candidates.length;
let answer = [];
let accumulate = [];
function backtracking(remain, arr, index) {
if (remain === 0) {
answer.push(arr);
return;
}
if (index < 0) {
return;
}
const val = candidates[index];
const q = Math.floor(remain / val); // q번 만큼 backtracking 해야함

for (let i = q; i >= 0; i--) {
const new_arr = [...arr];
for (let j = 0; j < i; j++) {
new_arr.push(val);
}

backtracking(remain - i * val, new_arr, index - 1);
}
}

backtracking(target, accumulate, len - 1);
return answer;
};

console.log(combinationSum([2], 1));
20 changes: 20 additions & 0 deletions Jiho/Day24/Leetcode_78. Subsets.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
var subsets = function (nums) {
let accumulate = [];
let answer = [];
function backtracking(arr, index) {
// 마지막
if (index === nums.length) {
answer.push(arr);
return;
}
const cpy = [...arr];
backtracking(cpy, index + 1);
arr.push(nums[index]);
backtracking(arr, index + 1);
}
backtracking(accumulate, 0);

return answer;
};

console.log(subsets([1, 2, 3]));