문제
내 풀이
function solution(lottos, win_nums) {
const highestRank = [];
const lowestRank = [];
const result = [];
lottos.sort((a, b) => a - b);
win_nums.sort((a, b) => a - b);
for (let i = 0; i < lottos.length; i++) {
if (lottos[i] === 0) {
highestRank.push(lottos[i]);
continue;
}
for (let j = 0; j < win_nums.length; j++) {
if (lottos[i] === win_nums[j]) {
highestRank.push(lottos[i]);
lowestRank.push(lottos[i]);
}
}
}
if (lowestRank.length !== 0) {
result.push(7 - highestRank.length, 7 - lowestRank.length);
} else if (highestRank.length === 0 && lowestRank.length === 0) {
result.push(6, 6);
} else {
result.push(7 - highestRank.length, 6);
}
return result;
}
다른 사람 풀이
- look-up table을 사용하여 순위를 배열에 담아둔다.
const ranks = [6, 6, 5, 4, 3, 2, 1];
const minCount = lottos.filter((el) => win_nums.includes(el)).length;
const zeroCount = lottos.filter((el) => el === 0).length;
let maxCount = minCount + zeroCount;
return [ranks[maxCount], ranks[minCount]];