From b60c08d6da9d00436b4849d4a06f8275d0c96044 Mon Sep 17 00:00:00 2001 From: JiHo00 <110150963+jihostudy@users.noreply.github.com> Date: Tue, 23 Jul 2024 16:19:07 +0900 Subject: [PATCH 1/2] Day23 2Q JIHO --- .../Day23/\353\260\261\354\244\200_10816.js" | 24 +++++++ "Jiho/Day23/\353\260\261\354\244\200_4963.js" | 63 +++++++++++++++++++ 2 files changed, 87 insertions(+) create mode 100644 "Jiho/Day23/\353\260\261\354\244\200_10816.js" create mode 100644 "Jiho/Day23/\353\260\261\354\244\200_4963.js" diff --git "a/Jiho/Day23/\353\260\261\354\244\200_10816.js" "b/Jiho/Day23/\353\260\261\354\244\200_10816.js" new file mode 100644 index 0000000..5b46295 --- /dev/null +++ "b/Jiho/Day23/\353\260\261\354\244\200_10816.js" @@ -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(" ")); diff --git "a/Jiho/Day23/\353\260\261\354\244\200_4963.js" "b/Jiho/Day23/\353\260\261\354\244\200_4963.js" new file mode 100644 index 0000000..e730060 --- /dev/null +++ "b/Jiho/Day23/\353\260\261\354\244\200_4963.js" @@ -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")); From 8f9c88f7be3ff233f8745db26012ed44d3c8d795 Mon Sep 17 00:00:00 2001 From: JiHo00 <110150963+jihostudy@users.noreply.github.com> Date: Wed, 24 Jul 2024 15:54:00 +0900 Subject: [PATCH 2/2] Day24 Jiho 3Q --- Jiho/Day24/Leetcode_39. Combination Sum.js | 30 ++++++++++++++++++++++ Jiho/Day24/Leetcode_78. Subsets.js | 20 +++++++++++++++ 2 files changed, 50 insertions(+) create mode 100644 Jiho/Day24/Leetcode_39. Combination Sum.js create mode 100644 Jiho/Day24/Leetcode_78. Subsets.js diff --git a/Jiho/Day24/Leetcode_39. Combination Sum.js b/Jiho/Day24/Leetcode_39. Combination Sum.js new file mode 100644 index 0000000..811fe38 --- /dev/null +++ b/Jiho/Day24/Leetcode_39. Combination Sum.js @@ -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)); diff --git a/Jiho/Day24/Leetcode_78. Subsets.js b/Jiho/Day24/Leetcode_78. Subsets.js new file mode 100644 index 0000000..ff90cf4 --- /dev/null +++ b/Jiho/Day24/Leetcode_78. Subsets.js @@ -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]));