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 1/3] 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])); From 7afd44fc31d45a7591be1c9d728f88a683a56390 Mon Sep 17 00:00:00 2001 From: JiHo00 <110150963+jihostudy@users.noreply.github.com> Date: Sat, 27 Jul 2024 02:37:31 +0900 Subject: [PATCH 2/3] Jiho 3Q --- Jiho/Day26/Leetcode_46. Permutations.js | 26 ++++++++++++ Jiho/Day26/Leetcode_90. Subsets II.js | 56 +++++++++++++++++++++++++ Jiho/Day26/set.js | 16 +++++++ 3 files changed, 98 insertions(+) create mode 100644 Jiho/Day26/Leetcode_46. Permutations.js create mode 100644 Jiho/Day26/Leetcode_90. Subsets II.js create mode 100644 Jiho/Day26/set.js diff --git a/Jiho/Day26/Leetcode_46. Permutations.js b/Jiho/Day26/Leetcode_46. Permutations.js new file mode 100644 index 0000000..50c0b7c --- /dev/null +++ b/Jiho/Day26/Leetcode_46. Permutations.js @@ -0,0 +1,26 @@ +var permute = function (nums) { + const len = nums.length; + const check = new Array(len).fill(false); + let answer = []; + const backtracking = (row, accumulate) => { + // End of DFS + if (row === len) { + answer.push([...accumulate]); + return; + } + for (let i = 0; i < len; i++) { + if (!check[i]) { + check[i] = true; + accumulate.push(nums[i]); + backtracking(row + 1, accumulate); + check[i] = false; + accumulate.pop(); + } + } + }; + + backtracking(0, []); + return answer; +}; + +console.log(permute([1, 2, 3, 4])); diff --git a/Jiho/Day26/Leetcode_90. Subsets II.js b/Jiho/Day26/Leetcode_90. Subsets II.js new file mode 100644 index 0000000..8894f52 --- /dev/null +++ b/Jiho/Day26/Leetcode_90. Subsets II.js @@ -0,0 +1,56 @@ +var subsetsWithDup = function (nums) { + const len = nums.length; + + let answer = []; + const backtracking = (index, accumulate) => { + if (index === len) { + // #3. 존재 여부 확인 + const targetMap = getMap(accumulate); + let sameExist = false; + answer.forEach((ans) => { + if (isIdentical(getMap(ans), targetMap)) { + sameExist = true; + } + }); + sameExist === false ? answer.push([...accumulate]) : undefined; + + return; + } + // #1. 선택X + const arr1 = [...accumulate]; + // #2. 선택O + accumulate.push(nums[index]); + const arr2 = [...accumulate]; + + backtracking(index + 1, arr1); + backtracking(index + 1, arr2); + }; + backtracking(0, []); + 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; +} +console.log(subsetsWithDup([0])); diff --git a/Jiho/Day26/set.js b/Jiho/Day26/set.js new file mode 100644 index 0000000..67109c7 --- /dev/null +++ b/Jiho/Day26/set.js @@ -0,0 +1,16 @@ +console.log(...[1, 2, 3]); +console.log(new Map([1, 2, 3])); + +console.log(isIdentical(new Set([1, 2, 3]), new Set([3, 1, 2]))); + +function isIdentical(setA, setB) { + if (setA.size !== setB.size) { + return false; + } + for (let elem of setA) { + if (!setB.has(elem)) { + return false; + } + } + return true; +} From 41117ec2fc75e5796beb49a1e12bb3987ce0520d Mon Sep 17 00:00:00 2001 From: JiHo00 <110150963+jihostudy@users.noreply.github.com> Date: Sat, 27 Jul 2024 02:42:14 +0900 Subject: [PATCH 3/3] Delete unnecessary file --- Jiho/Day26/set.js | 16 ---------------- 1 file changed, 16 deletions(-) delete mode 100644 Jiho/Day26/set.js diff --git a/Jiho/Day26/set.js b/Jiho/Day26/set.js deleted file mode 100644 index 67109c7..0000000 --- a/Jiho/Day26/set.js +++ /dev/null @@ -1,16 +0,0 @@ -console.log(...[1, 2, 3]); -console.log(new Map([1, 2, 3])); - -console.log(isIdentical(new Set([1, 2, 3]), new Set([3, 1, 2]))); - -function isIdentical(setA, setB) { - if (setA.size !== setB.size) { - return false; - } - for (let elem of setA) { - if (!setB.has(elem)) { - return false; - } - } - return true; -}