https://programmers.co.kr/learn/courses/30/lessons/72411
Code
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
from itertools import combinations
from collections import defaultdict
def solution(orders, course):
answer = []
### 1. Remove
min_len = course[0]
invalids = []
for idx_order, order in enumerate(orders):
if len(order) < min_len:
invalids.append(idx_order)
orders = [order for idx_order, order in enumerate(orders) if idx_order not in invalids]
### 2. Results
results = []
for one_course in course:
counters = defaultdict(int)
for order in orders:
order = ''.join(sorted(order))
for comb in combinations(order, one_course):
counters[comb] += 1
sorted_counters = sorted(counters.items(), key=lambda x: x[1], reverse=True)
max_val = sorted_counters[0][1]
max_counters = [''.join(counter[0]) for counter in sorted_counters if counter[1] == max_val and (max_val > 1)]
results += max_counters
return list(sorted(results))
Complexity
$O(mn)$
- $n$:
|orders|
- $m$:
|course|
PREVIOUSEtc