forked from ngiengkianyew/daily-coding-problem
-
Notifications
You must be signed in to change notification settings - Fork 1
/
problem_297.py
43 lines (33 loc) · 1.19 KB
/
problem_297.py
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
32
33
34
35
36
37
38
39
40
41
42
43
from typing import Dict, List
def minimize_drinks(drinks, remaining_drinks, remaining_customers, cust_by_drink):
min_option = drinks
if not remaining_customers:
return drinks - remaining_drinks
for drink in remaining_drinks:
option = minimize_drinks(
drinks, remaining_drinks - {drink},
remaining_customers - cust_by_drink[drink], cust_by_drink)
if len(option) < len(min_option):
min_option = option
return min_option
def get_min_drinks(preferences: Dict[int, List[int]]):
cust_by_drink = dict()
for cust in preferences:
for drink in preferences[cust]:
if drink not in cust_by_drink:
cust_by_drink[drink] = set()
cust_by_drink[drink].add(cust)
remaining_drinks = set(cust_by_drink.keys())
remaining_customers = set(preferences.keys())
min_drinks = minimize_drinks(set(cust_by_drink.keys()), remaining_drinks,
remaining_customers, cust_by_drink)
return min_drinks
# Tests
preferences = {
0: [0, 1, 3, 6],
1: [1, 4, 7],
2: [2, 4, 7, 5],
3: [3, 2, 5],
4: [5, 8]
}
assert get_min_drinks(preferences) == {1, 5}