from itertools import combinations
def solution(orders, course):
comb_dic = {long: {} for long in course}
selected = set()
for long in course:
best_num = 2
for guest in orders:
comb = list(map(sorted, combinations(guest, long)))
comb = ["".join(arr) for arr in comb if len(arr) > 0]
for e in comb:
if not e:
continue
key = "".join(sorted(e))
if key in comb_dic[long].keys():
comb_dic[long][key] += 1
best_num = max(best_num, comb_dic[long][key])
else:
comb_dic[long][key] = 1
for k, v in comb_dic[long].items():
if v >= best_num:
selected.add(k)
selected = sorted(list(selected))
return selected
기억할 표현식
cnt = Counter(list) 로 dictionary 형태임.
answer += ["".join(f) for f in cnt if cnt[f] == max(cnt.values())]
cnt.values() # dictionary의 값을 배열로 반환
=> 대체
selected += [
"".join(f) for f in comb_dic[long] if comb_dic[long][f] == max(comb_dic[long].values())
]
for k, v in comb_dic[long].items():
if v >= best_num:
selected.add(k)
'⚡️algorithm > accepted' 카테고리의 다른 글
[BFS, 그래프] 프로그래머스 lv3. 블록이동하기 (0) | 2022.11.02 |
---|---|
[플로이드 와샬] 백준 11404. 플로이드 (기본) (1) | 2022.10.19 |
[BFS, 파이썬] 2146. 다리만들기 (!!) (0) | 2022.10.03 |
[queue] 프로그래머스 lv2. 두큐합 같게 만들기 (feat. deque의 주요 메서드) (0) | 2022.09.08 |
[DFS, 기본] 백준 2667. 단지번호 붙이기 (0) | 2022.04.26 |