티스토리 뷰
from itertools import combinations
def solution(orders, course):
answer = []
for i in range(len(orders)):
orders[i] = sorted(orders[i])
for i in course:
hash_map = {}
for j in orders:
for k in list(combinations(j,i)):
if k in hash_map.keys():
hash_map[k] += 1
else:
hash_map[k] = 1
max_ = 0
for key, value in hash_map.items():
if len(key) == i:
max_ = max(value, max_)
for key, value in hash_map.items():
if value == max_ and value >= 2:
a = ""
for i in key:
a = a + i
answer.append(a)
return sorted(answer)
흠.. for문 너무 많아... 힘들어...
좋은 방법이 필요해
<내가 생각하는 BEST 풀이>
import collections
import itertools
def solution(orders, course):
result = []
for course_size in course:
order_combinations = []
for order in orders:
order_combinations += itertools.combinations(sorted(order), course_size)
most_ordered = collections.Counter(order_combinations).most_common()
result += [ k for k, v in most_ordered if v > 1 and v == most_ordered[0][1] ]
return [ ''.join(v) for v in sorted(result) ]
오우 모르던 것들 많다.
collections 를 import하여 list 안에서 나타난 원소의 개수를 세서 dict로 만들 수 있다.
collections.Counter(order_combinations) 으로 원소들의 개수를 세서 dict로 만든다.
그리고, most_common()은 가장 많이 등장한 요소만을 리턴해주는 것인데, most_common(n) 을 하면 가장 많이 등장한 n개를 리턴해준다. 그러나 아무것도 안쓰면 전부 리턴해줌.
'코딩테스트 대비' 카테고리의 다른 글
[프로그래머스] k진수에서 소수 개수 구하기 (0) | 2022.02.08 |
---|---|
[프로그래머스] 최댓값과 최솟값 (0) | 2022.02.08 |
[프로그래머스] 124나라 (0) | 2022.02.07 |
[프로그래머스] 3진법 뒤집기 (0) | 2022.02.07 |
[프로그래머스] 로또의 최고 순위와 최저 순위 (0) | 2022.02.07 |
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- 백트래킹
- 최소신장트리
- torchscript
- LGSVL
- version
- 이것이코딩테스트다
- 프로그래머스
- BFS
- 동적프로그래밍
- 설치
- 설치하기
- 코딩테스트
- tensorflow
- 백준
- CUDA
- matplotlib
- notfound
- 다익스트라
- 파이썬
- PIP
- pytorch
- numpy
- n과m
- docker
- error
- torch
- 카카오
- Python
- dfs
- shellscript
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
글 보관함