-
Notifications
You must be signed in to change notification settings - Fork 892
/
problem_298.py
59 lines (41 loc) · 1.44 KB
/
problem_298.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
from typing import List, Dict
from copy import deepcopy
class AppleSet:
def __init__(self):
self.apple_types = dict()
def __repr__(self):
return str(self.apple_types)
def add_apple(self, atype: int):
if atype not in self.apple_types:
self.apple_types[atype] = 0
self.apple_types[atype] += 1
def remove_apple(self, atype: int):
self.apple_types[atype] -= 1
if self.apple_types[atype] == 0:
del self.apple_types[atype]
def size(self):
return len(self.apple_types)
def total(self):
return sum(x for x in self.apple_types.values())
def get_min_set(apple_set: AppleSet, apples: List[int]):
if apple_set.size() == 2:
return apple_set.total()
if not apples:
return 0
first, last = apples[0], apples[-1]
apple_set_1 = deepcopy(apple_set)
apple_set_1.remove_apple(first)
alt_1 = get_min_set(apple_set_1, apples[1:])
apple_set_2 = deepcopy(apple_set)
apple_set_2.remove_apple(last)
alt_2 = get_min_set(apple_set_2, apples[:-1])
return max(alt_1, alt_2)
def get_longest_portion(apples: List[int]):
apple_set = AppleSet()
for atype in apples:
apple_set.add_apple(atype)
return get_min_set(apple_set, apples)
# Tests
assert get_longest_portion([2, 1, 2, 3, 3, 1, 3, 5]) == 4
assert get_longest_portion([2, 1, 2, 2, 2, 1, 2, 1]) == 8
assert get_longest_portion([1, 2, 3, 4]) == 2