기억은 짧고 기록은 길다
[프로그래머스/Programmers] 위장 - Python 본문
Link
코딩테스트 연습 - 위장
programmers.co.kr
Solution
해당 문제는 종류 별로 개수를 파악한 후 (개수 + 1) 값을 곱해주는 것만 파악하면 쉽게 해결할 수 있는 문제였다. 마지막에 -1은 아무것도 착용하지 않은 경우를 빼주는 역할을 한다.
def solution(clothes):
answer = 1
d = {}
for i in range(len(clothes)):
if clothes[i][1] in list(d.keys()):
continue
d[clothes[i][1]] = 1
for j in range(i + 1, len(clothes)):
if clothes[i][1] == clothes[j][1]:
d[clothes[i][1]] += 1
v = list(d.values())
for i in range(len(v)):
answer *= (v[i] + 1)
return answer - 1
🔑 key point: Dictionary, answer *= (v[i] + 1)
Other Solution
해당 풀이를 보고 정말 감탄을 금치 못했다. Counter()와 reduce()의 사용법만 안다면 풀이를 쉽게 이해할 수 있으므로 참고에 Counter()와 reduce()에 대한 자세한 설명이 되어있는 글을 첨부하도록 하겠다.
from collections import Counter
from functools import reduce
def solution(clothes):
cnt = Counter([kind for name, kind in clothes])
answer = reduce(lambda x, y: x * (y + 1), cnt.values(), 1) - 1
return answer
🔑 key point: Counter(), reduce()
첨부
https://excelsior-cjh.tistory.com/94
https://codingpractices.tistory.com/67?category=1026198
'CodingTest > programmers' 카테고리의 다른 글
[프로그래머스/Programmers] 이진 변환 반복하기 - Python (0) | 2021.09.21 |
---|---|
[프로그래머스/Programmers] 전화번호 목록 - Python (0) | 2021.09.21 |
[프로그래머스/Programmers] 예상 대진표 - Python (0) | 2021.09.17 |
[프로그래머스/Programmers] 숫자의 표현 - Python (0) | 2021.09.17 |
[프로그래머스/Programmers] 방문 길이 - Python (0) | 2021.09.17 |
Comments