[LeetCode] 1128. Number of Equivalent Domino Pairs

Chobby·2025년 7월 8일
1

LeetCode

목록 보기
465/481

😎풀이

  1. 작은 값,큰 값 형태의 문자열 구조로 빈도 확인
  2. 확인 된 빈도 수 체크
    2-1. 2보다 작다면 쌍을 이룰 수 없음
    2-2. 2보다 크다면 nC2 의 형태로 가능한 쌍의 수를 누산함
  3. 누산된 값을 반환
function numEquivDominoPairs(dominoes: number[][]): number {
    const history = new Map<string, number>()
    let pairCount = 0
    for(const [a, b] of dominoes) {
        const format = a <= b ? `${a},${b}` : `${b},${a}` 
        history.set(format, (history.get(format) ?? 0) + 1)
    }
    for(const [key, value] of history) {
        if(value < 2) continue
        pairCount += value * (value - 1) / 2
    }
    return pairCount
};
profile
내 지식을 공유할 수 있는 대담함

0개의 댓글