-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathm0090.py
87 lines (70 loc) · 1.8 KB
/
m0090.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
"""Subsets II
TAG: array, back-tracking
Given a collection of integers that might contain duplicates, nums, return all
possible subsets (the power set).
Note: The solution set must not contain duplicate subsets.
Example:
Input: [1,2,2]
Output:
[
[2],
[1],
[1,2,2],
[2,2],
[1,2],
[]
]
"""
from __future__ import annotations
from dataclasses import dataclass
from enum import IntEnum, unique
from typing import Sequence, Tuple
@unique
class State(IntEnum):
DOING = 0
DONE = 1
@dataclass(frozen=True)
class FSM:
seq: Tuple[int, ...]
remaining: Tuple[int, ...]
@property
def state(self):
if not self.remaining:
return State.DONE
return State.DOING
def explode(self) -> Tuple[FSM, FSM]:
assert self.state == State.DOING
value = self.remaining[0]
fsm_new = (type(self))((*self.seq, value), self.remaining[1:])
fsm_old = (type(self))(self.seq, self.remaining[1:])
return fsm_old, fsm_new
class Subset2:
@classmethod
def looper(
cls,
inputs: Tuple[FSM, ...],
outputs: Tuple[Tuple[int, ...], ...],
) -> Tuple[Tuple[int, ...], ...]:
if not inputs:
return outputs
fsm = inputs[0]
if fsm.state == State.DONE:
return cls.looper(inputs[1:], (*outputs, fsm.seq))
return cls.looper((*fsm.explode(), *inputs[1:]), outputs)
def solution(self, seq: Sequence[int]):
fsm = FSM((), seq)
raw_seq = self.looper((fsm, ), ())
unique_seq = set(raw_seq)
return tuple(sorted(unique_seq))
if __name__ == '__main__':
ipt_1 = [1, 2, 2]
exp_1 = (
(),
(1, ),
(1, 2),
(1, 2, 2),
(2, ),
(2, 2),
)
subset = Subset2()
assert subset.solution(ipt_1) == exp_1