diff --git a/Jiho/Day27/Leetcode_40. Combination Sum II.js b/Jiho/Day27/Leetcode_40. Combination Sum II.js new file mode 100644 index 0000000..fcddb0c --- /dev/null +++ b/Jiho/Day27/Leetcode_40. Combination Sum II.js @@ -0,0 +1,109 @@ +var combinationSum2 = function (candidates, target) { + const len = candidates.length; + const totalSum = getSum(candidates); + let answer = []; + const DFS = (index, arr, sum, leftOver) => { + if (sum === target) { + // 같은거 포함 여부 확인 + const targetMap = getMap(arr); + let sameExist = false; + answer.forEach((ans) => { + if (isIdentical(getMap(ans), targetMap)) { + sameExist = true; + } + }); + !sameExist ? answer.push(arr) : undefined; + return; + } else if (index >= len || sum > target || sum + leftOver < target) { + return; + } + // 포함X + DFS(index + 1, [...arr], sum, leftOver - candidates[index]); + // 포함O + arr.push(candidates[index]); + DFS( + index + 1, + [...arr], + sum + candidates[index], + leftOver - candidates[index] + ); + }; + + DFS(0, [], 0, totalSum); + return answer; +}; + +function isIdentical(iMap, tMap) { + if (iMap.size !== tMap.size) { + return false; + } + for (let [key, value] of iMap) { + if (tMap.has(key)) { + if (value !== tMap.get(key)) { + return false; + } + } else { + return false; + } + } + + return true; +} + +function getMap(array) { + const map = new Map(); + for (const elm of array) { + map.set(elm, (map.get(elm) | 0) + 1); + } + return map; +} +function getSum(array) { + let sum = 0; + for (let i = 0; i < array.length; i++) sum += array[i]; + return sum; +} + +var combinationSumAns = function (candidates, target) { + candidates.sort((a, b) => a - b); + const res = []; + + function dfs(target, start, comb) { + if (target < 0) { + return; + } + + if (target === 0) { + res.push(comb.slice()); + return; + } + + for (let i = start; i < candidates.length; i++) { + if (i > start && candidates[i] === candidates[i - 1]) { + continue; + } + + if (candidates[i] > target) { + break; + } + + dfs(target - candidates[i], i + 1, comb.concat(candidates[i])); + } + } + + dfs(target, 0, []); + return res; +}; + +console.log( + combinationSumAns( + [ + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + ], + 30 + ) +); +// console.log(combinationSum2([10, 1, 2, 7, 6, 1, 5], 8)); +// console.log(combinationSum2([2, 5, 2, 1, 2], 5)); diff --git "a/Jiho/Day27/\353\260\261\354\244\200_1388_\353\260\224\353\213\245 \354\236\245\354\213\235.js" "b/Jiho/Day27/\353\260\261\354\244\200_1388_\353\260\224\353\213\245 \354\236\245\354\213\235.js" new file mode 100644 index 0000000..1d7c0de --- /dev/null +++ "b/Jiho/Day27/\353\260\261\354\244\200_1388_\353\260\224\353\213\245 \354\236\245\354\213\235.js" @@ -0,0 +1,62 @@ +const fs = require("fs"); +const input = fs + .readFileSync(process.platform === "linux" ? "/dev/stdin" : "../input.txt") + .toString() + .trim() + .split("\n"); + +// 입력 받기 +const [height, width] = input.shift()?.split(" ").map(Number); +const arr = Array.from(Array(height), () => new Array(width)); + +for (let row = 0; row < height; row++) { + const rowInput = input[row]; + for (let col = 0; col < width; col++) { + arr[row][col] = rowInput[col]; + } +} + +const BFS = (startRow, startCol, visited) => { + const queue = [[startRow, startCol]]; + while (queue.length) { + const [row, col] = queue.shift(); + + const val = arr[row][col]; + // + if (val === "-") { + if ( + col + 1 < width && + arr[row][col + 1] === "-" && + !visited[row][col + 1] + ) { + visited[row][col + 1] = true; + queue.push([row, col + 1]); + } + } + // + else if (val === "|") { + if ( + row + 1 < height && + arr[row + 1][col] === "|" && + !visited[row + 1][col] + ) { + visited[row + 1][col] = true; + queue.push([row + 1, col]); + } + } + } +}; + +let answer = 0; +let visited = Array.from(Array(height), () => new Array(width).fill(false)); +for (let i = 0; i < height; i++) { + for (let j = 0; j < width; j++) { + if (!visited[i][j]) { + answer++; + visited[i][j] = true; + BFS(i, j, visited); + } + } +} + +console.log(answer); diff --git a/Jiho/Day28/Leetcode_70. Climbing Stairs.js b/Jiho/Day28/Leetcode_70. Climbing Stairs.js new file mode 100644 index 0000000..f72967c --- /dev/null +++ b/Jiho/Day28/Leetcode_70. Climbing Stairs.js @@ -0,0 +1,11 @@ +var climbStairs = function (n) { + let arr = new Array(n + 1); + arr[1] = 1; + arr[2] = 2; + for (let i = 3; i <= n; i++) { + arr[i] = arr[i - 1] + arr[i - 2]; + } + return arr[n]; +}; + +console.log(climbStairs(10)); diff --git a/Jiho/Day28/Leetcode_746. Min Cost Climbing Stairs.js b/Jiho/Day28/Leetcode_746. Min Cost Climbing Stairs.js new file mode 100644 index 0000000..3492434 --- /dev/null +++ b/Jiho/Day28/Leetcode_746. Min Cost Climbing Stairs.js @@ -0,0 +1,7 @@ +var minCostClimbingStairs = function (cost) { + for (let i = cost.length - 3; i >= 0; i--) { + cost[i] += Math.min(cost[i + 1], cost[i + 2]); + } + + return Math.min(cost[0], cost[1]); +};