ye0nn
영차영차
ye0nn
전체 방문자
오늘
어제
  • 분류 전체보기 (61)
    • CS (0)
      • 운영체제 (0)
      • 네트워크 (0)
      • 알고리즘 & 자료구조 (0)
    • 코딩테스트 (48)
      • 프로그래머스 (40)
      • 백준 (8)
    • 프로그래밍 (11)
      • 프론트엔드 (3)
      • 자바스크립트 (0)
      • 스위프트 (7)
      • 파이썬 (1)
    • 취준기록 (1)

인기 글

블로그 메뉴

  • 홈
  • 태그
  • 방명록

티스토리

hELLO · Designed By 정상우.
ye0nn

영차영차

[프로그래머스] Level 2 - 위장 (Python)
코딩테스트/프로그래머스

[프로그래머스] Level 2 - 위장 (Python)

2022. 6. 9. 14:06

 

 

 

 

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

 

코딩테스트 연습 - 위장

 

programmers.co.kr

 

 

 

 

✅ Solution

  • defaultdict을 이용하여 옷을 종류별로 구분해준다.
  • 종류별로 0개 혹은 1개를 선택할 수 있으므로 각 종류별 길이 + 1을 한 뒤 곱한다.
    • headgear : yellow_hat, green_turban / eyewear: blue_sunglasses -> (2+1) * (1 + 1) = 6
  • 근데, 문제에서 최소 1개의 의상을 입는다고 했으니 모두 0개를 고르는 한개의 경우를 빼주고 리턴한다. 

 

 

 

 

✅  Code

from collections import defaultdict

def solution(clothes):
    answer = 1
    allClothes = defaultdict(list)

    for cloth in clothes:
        name, kind = cloth
        allClothes[kind].append(name)

    for key in allClothes.keys():
        answer *= (len(allClothes[key]) + 1)

    return answer - 1

 

 

 

 

저작자표시 (새창열림)

'코딩테스트 > 프로그래머스' 카테고리의 다른 글

[프로그래머스] Level 2 - 2개 이하로 다른 비트 (Python)  (0) 2022.06.23
[프로그래머스] Level 2 - 다리를 지나는 트럭 (Python)  (0) 2022.06.22
[프로그래머스] Level 2 - 게임 맵 최단거리 (Python)  (0) 2022.06.09
[프로그래머스] Level 2 - 배달 (Python)  (0) 2022.06.09
[프로그래머스] Level 2 - 괄호 회전하기 (Python)  (0) 2022.06.09
    '코딩테스트/프로그래머스' 카테고리의 다른 글
    • [프로그래머스] Level 2 - 2개 이하로 다른 비트 (Python)
    • [프로그래머스] Level 2 - 다리를 지나는 트럭 (Python)
    • [프로그래머스] Level 2 - 게임 맵 최단거리 (Python)
    • [프로그래머스] Level 2 - 배달 (Python)
    ye0nn
    ye0nn
    프론트엔드 개발자의 개발기록

    티스토리툴바