|
| 1 | +var combinationSum2 = function (candidates, target) { |
| 2 | + const len = candidates.length; |
| 3 | + const totalSum = getSum(candidates); |
| 4 | + let answer = []; |
| 5 | + const DFS = (index, arr, sum, leftOver) => { |
| 6 | + if (sum === target) { |
| 7 | + // 같은거 포함 여부 확인 |
| 8 | + const targetMap = getMap(arr); |
| 9 | + let sameExist = false; |
| 10 | + answer.forEach((ans) => { |
| 11 | + if (isIdentical(getMap(ans), targetMap)) { |
| 12 | + sameExist = true; |
| 13 | + } |
| 14 | + }); |
| 15 | + !sameExist ? answer.push(arr) : undefined; |
| 16 | + return; |
| 17 | + } else if (index >= len || sum > target || sum + leftOver < target) { |
| 18 | + return; |
| 19 | + } |
| 20 | + // 포함X |
| 21 | + DFS(index + 1, [...arr], sum, leftOver - candidates[index]); |
| 22 | + // 포함O |
| 23 | + arr.push(candidates[index]); |
| 24 | + DFS( |
| 25 | + index + 1, |
| 26 | + [...arr], |
| 27 | + sum + candidates[index], |
| 28 | + leftOver - candidates[index] |
| 29 | + ); |
| 30 | + }; |
| 31 | + |
| 32 | + DFS(0, [], 0, totalSum); |
| 33 | + return answer; |
| 34 | +}; |
| 35 | + |
| 36 | +function isIdentical(iMap, tMap) { |
| 37 | + if (iMap.size !== tMap.size) { |
| 38 | + return false; |
| 39 | + } |
| 40 | + for (let [key, value] of iMap) { |
| 41 | + if (tMap.has(key)) { |
| 42 | + if (value !== tMap.get(key)) { |
| 43 | + return false; |
| 44 | + } |
| 45 | + } else { |
| 46 | + return false; |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + return true; |
| 51 | +} |
| 52 | + |
| 53 | +function getMap(array) { |
| 54 | + const map = new Map(); |
| 55 | + for (const elm of array) { |
| 56 | + map.set(elm, (map.get(elm) | 0) + 1); |
| 57 | + } |
| 58 | + return map; |
| 59 | +} |
| 60 | +function getSum(array) { |
| 61 | + let sum = 0; |
| 62 | + for (let i = 0; i < array.length; i++) sum += array[i]; |
| 63 | + return sum; |
| 64 | +} |
| 65 | + |
| 66 | +var combinationSumAns = function (candidates, target) { |
| 67 | + candidates.sort((a, b) => a - b); |
| 68 | + const res = []; |
| 69 | + |
| 70 | + function dfs(target, start, comb) { |
| 71 | + if (target < 0) { |
| 72 | + return; |
| 73 | + } |
| 74 | + |
| 75 | + if (target === 0) { |
| 76 | + res.push(comb.slice()); |
| 77 | + return; |
| 78 | + } |
| 79 | + |
| 80 | + for (let i = start; i < candidates.length; i++) { |
| 81 | + if (i > start && candidates[i] === candidates[i - 1]) { |
| 82 | + continue; |
| 83 | + } |
| 84 | + |
| 85 | + if (candidates[i] > target) { |
| 86 | + break; |
| 87 | + } |
| 88 | + |
| 89 | + dfs(target - candidates[i], i + 1, comb.concat(candidates[i])); |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + dfs(target, 0, []); |
| 94 | + return res; |
| 95 | +}; |
| 96 | + |
| 97 | +console.log( |
| 98 | + combinationSumAns( |
| 99 | + [ |
| 100 | + 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, |
| 101 | + 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, |
| 102 | + 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, |
| 103 | + 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, |
| 104 | + ], |
| 105 | + 30 |
| 106 | + ) |
| 107 | +); |
| 108 | +// console.log(combinationSum2([10, 1, 2, 7, 6, 1, 5], 8)); |
| 109 | +// console.log(combinationSum2([2, 5, 2, 1, 2], 5)); |
0 commit comments