Skip to content

Commit 69f1937

Browse files
authored
Merge pull request #91 from AlgorithmIsMyLife/jiho
Day27 2Q 지호
2 parents 5e6aeb3 + cc3c732 commit 69f1937

File tree

2 files changed

+171
-0
lines changed

2 files changed

+171
-0
lines changed
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
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));
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
const fs = require("fs");
2+
const input = fs
3+
.readFileSync(process.platform === "linux" ? "/dev/stdin" : "../input.txt")
4+
.toString()
5+
.trim()
6+
.split("\n");
7+
8+
// 입력 받기
9+
const [height, width] = input.shift()?.split(" ").map(Number);
10+
const arr = Array.from(Array(height), () => new Array(width));
11+
12+
for (let row = 0; row < height; row++) {
13+
const rowInput = input[row];
14+
for (let col = 0; col < width; col++) {
15+
arr[row][col] = rowInput[col];
16+
}
17+
}
18+
19+
const BFS = (startRow, startCol, visited) => {
20+
const queue = [[startRow, startCol]];
21+
while (queue.length) {
22+
const [row, col] = queue.shift();
23+
24+
const val = arr[row][col];
25+
//
26+
if (val === "-") {
27+
if (
28+
col + 1 < width &&
29+
arr[row][col + 1] === "-" &&
30+
!visited[row][col + 1]
31+
) {
32+
visited[row][col + 1] = true;
33+
queue.push([row, col + 1]);
34+
}
35+
}
36+
//
37+
else if (val === "|") {
38+
if (
39+
row + 1 < height &&
40+
arr[row + 1][col] === "|" &&
41+
!visited[row + 1][col]
42+
) {
43+
visited[row + 1][col] = true;
44+
queue.push([row + 1, col]);
45+
}
46+
}
47+
}
48+
};
49+
50+
let answer = 0;
51+
let visited = Array.from(Array(height), () => new Array(width).fill(false));
52+
for (let i = 0; i < height; i++) {
53+
for (let j = 0; j < width; j++) {
54+
if (!visited[i][j]) {
55+
answer++;
56+
visited[i][j] = true;
57+
BFS(i, j, visited);
58+
}
59+
}
60+
}
61+
62+
console.log(answer);

0 commit comments

Comments
 (0)