티스토리 뷰

문제

https://school.programmers.co.kr/learn/courses/30/lessons/42578

풀이

  • 조합으로 해결가능하다
  • 옷 종류에 따른 개수 + 해당 옷을 선택하지 않는 경우로 계산하면 된다
  • 최소 하나의 옷을 골라야하기 때문에 전체 경우의 수 - 1(아무것도 선택하지 않는 경우)로 계산한다

전체코드

import Foundation

func solution(_ clothes:[[String]]) -> Int {
    var dics: [String:[String]] = [:]

    for clothe in clothes { //종류별 정리
        if dics[clothe[1]] == nil {
            dics[clothe[1]] = []
        }
        dics[clothe[1]]!.append(clothe[0])
    }
    var result: Int = 1
    for dic in dics {
        result *= (dic.value.count+1)
    }
    return result-1
}
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2026/01   »
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
글 보관함