https://school.programmers.co.kr/learn/courses/30/lessons/1845
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
간단한 문제였다.
그런데 처음에 조합이랑 카운터를 사용했는데 시간초과가 났다.
보니까 set을 이용하면 반복문없이 길이 비교만 하면 됐다,,
시간초과 코드
from itertools import combinations
from collections import Counter
def solution(nums):
answer = 0
take = int(len(nums)/2)
for i in combinations(nums,take):
c = Counter(i)
answer = max(len(c.keys()), answer)
return answer
정답코드 set 이용
def solution(nums):
answer = 0
take = int(len(nums)/2)
s = set(nums)
if len(s)<=take:
answer = len(s)
else:
answer = take
return answer
'알고리즘 > 프로그래머스(Programmers)' 카테고리의 다른 글
[프로그래머스](Lv.3)길 찾기 게임[2019 KAKAO BLIND RECRUITMENT][파이썬/트리/전위순회/후위순회/Preorder/Postorder/Tree] (0) | 2022.11.02 |
---|---|
[프로그래머스](Lv.3) 외벽점검 [2020 KAKAO BLIND RECRUITMENT][파이썬/Python/슬라이딩윈도우/원형큐] (1) | 2022.10.08 |
[알고리즘/프로그래머스] 숫자 문자열과 영단어 [파이썬/구현/문자열] (0) | 2022.08.07 |
[알고리즘/프로그래머스/카카오코테2021] 신규아이디추천 [문자열/파이썬/python] (0) | 2022.07.21 |
[알고리즘/프로그래머스] 더 맵게 [힙/heap/파이썬/python] (0) | 2022.06.30 |