-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbitmask_task.py
38 lines (29 loc) · 921 Bytes
/
bitmask_task.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
"""
Subset Sum
You are given N numbers. Check if there is a subset
of them, with the sum equal to target value S.
N <= 20
Own edit: return the found subset.
Own edit2: return all possible subsets.
"""
# O(2^n * n)
def solution(nums: list[int], s: int) -> list[int]:
size = len(nums)
subset_all = []
for mask in range(1 << size): # AKA 2 ** size
subset = []
subset_sum = 0
for i in range(size):
if mask & (1 << i):
subset.append(nums[i])
subset_sum += nums[i]
if subset_sum == s:
subset_all.append(subset)
return subset_all
if __name__ == '__main__':
print(solution([20], 20))
print(solution([1, 15, 3, 4], 20))
print(solution([1, 15, 3, 5], 20))
print(solution([1, 15, 3, 6], 20))
print(solution([1, 15, 3, 4, 5], 20))
# офигеть вау меня это впечатлило