
😎풀이
a
부터 e
까지 오름차 순으로 정렬된 5개의 조합을 찾기 위해 5중 반복한다.
- 각 조합에서
q
의 모든 조건과 그에 맞는 ans
의 수가 같은지 비교한다.
2-1. 모든 조건이 일치할 경우 정답을 카운트한다.
2-2. 하나라도 조건이 불일치 할 경우 다음 조합을 확인한다.
- 확인된 결과를 반환한다.
function solution(n, q, ans) {
let count = 0;
for (let a = 1; a <= n - 4; a++) {
for (let b = a + 1; b <= n - 3; b++) {
for (let c = b + 1; c <= n - 2; c++) {
for (let d = c + 1; d <= n - 1; d++) {
for (let e = d + 1; e <= n; e++) {
const candidate = [a, b, c, d, e];
let valid = true;
for (let i = 0; i < q.length; i++) {
const query = q[i];
const expectedMatch = ans[i];
let matchCount = 0;
for (let num of candidate) {
if(!query.includes(num)) continue
matchCount++;
}
if (matchCount !== expectedMatch) {
valid = false;
break;
}
}
if (valid) count++;
}
}
}
}
}
return count;
}