📑 문제

✏️ 풀이
재영
이건 조건에 맞춰서 설정만 해주면 간단한 듯 해요!
그리고 sort의 경우 0만 아니면 제대로 비교를 해주기 때문에
||
연산으로 더 깔끔하게 처리해줄 수 있다고 합니다 :) 참고하시면 좋을 거 같아유!!const solution = (weights, head2head) => { return head2head .map((scores, idx) => [scores, weights[idx], idx]) .sort((a, b) => { const firstSortStandard = firstSortCallback(b[0]) - firstSortCallback(a[0]); const secondSortStandard = secondSortCallback(b[0], b[1], weights, b[2]) - secondSortCallback(a[0], a[1], weights, a[2]); const thirdSortStandard = b[1] - a[1]; const fourthSortStandard = a[2] - b[2]; return firstSortStandard || secondSortStandard || thirdSortStandard || fourthSortStandard; }) .map(([head, weight, idx]) => idx + 1) } const firstSortCallback = (scores) => { const scoresArr = scores.split("") const winFightCount = scoresArr.filter(score => score === "W").length; const allFightCount = winFightCount + scoresArr.filter(score => score === "L").length; return allFightCount ? (winFightCount / allFightCount).toFixed(12) : 0; } const secondSortCallback = (head, weight, weights, idx) => { return head .split("") .filter((h, i) => i !== idx && h === "W" && weights[i] > weight) .length; }
은찬
function solution(weights, head2head) { const answer = []; const weightsSet = [...new Set(weights)]; const weightsMap = new Map(); const rating = []; const length = weights.length; let rank = 0; weightsSet.sort((a, b) => a - b); weightsSet.forEach((w) => weightsMap.set(w, rank++)); for(let i = 0; i < length; i++){ let rate = 0; // 승률 let total = length; // 복서가 경기를 뛴 횟수 let win = 0; // 복서가 다른 복서를 이긴 횟수 let heavier = 0; // 자신보다 무거운 복서를 이긴 횟수 let weightIndex = weightsMap.get(weights[i]); // 현재 복서의 몸무게 순위 for(let j = 0; j < length; j++){ if(head2head[i][j] === "W"){ if(weights[i] < weights[j]){ heavier++; } win++ } else if(head2head[i][j] === "N"){ total--; } } if(total === 0){ rate = 0; } else{ rate = win / total; } rating.push([rate, heavier, weightIndex, i]); } rating.sort((a, b) => { if(a[0] === b[0]){ if(a[1] === b[1]){ if(a[2] === b[2]){ return a[3] - b[3]; } return b[2] - a[2]; } return b[1] - a[1]; } else{ return b[0] - a[0]; } }); rating.forEach((rank) => answer.push(rank[3] + 1)); return answer; }