😎풀이

  1. 모든 카드가 같은 종류라면 Flush
  2. ranks 순회
    2-1. 각 카드의 숫자 빈도 기록
  3. 가장 많이 나온 숫자의 빈도가 3회 이상이라면, Three of a Kind
  4. 가장 많이 나온 숫자의 빈도가 2회 이상이라면, Pair
  5. 그 외는 모두 High Card를 반환
function bestHand(ranks: number[], suits: string[]): string {
    const suitsSet = new Set(suits)
    if(suitsSet.size === 1) return "Flush"
    const frequent = new Map()
    for(const rank of ranks) {
        frequent.set(rank, (frequent.get(rank) ?? 0) + 1)
    }
    let maxFreq = 0
    for(const [key, value] of frequent) {
        maxFreq = Math.max(maxFreq, value)
    }
    if(maxFreq >= 3) return "Three of a Kind"
    if(maxFreq === 2) return "Pair"
    return "High Card"
};
profile
내 지식을 공유할 수 있는 대담함

0개의 댓글