-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0827_make_predix_sum.py
More file actions
52 lines (44 loc) · 1.21 KB
/
0827_make_predix_sum.py
File metadata and controls
52 lines (44 loc) · 1.21 KB
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
from collections import deque
def check(x,y):
if sum(x) != sum(y):
return False
queue = deque()
l_y = len(y)
l_x = len(x)
for i in range(l_x):
for j in range(l_y):
if x[i]==y[j]:
x[i] = 0
y[j] = 0
n_x = [num for num in x if num]
n_y = [num for num in y if num]
l_y = len(n_y)
l_x = len(n_x)
y_check = [False for _ in range(l_y)]
for i in range(l_x):
queue.append([1<<i,n_x[i]])
while queue:
check_x, sum_n = queue.popleft()
for i in range(l_x):
if not check_x & (1<<i):
next_num = n_x[i]
tmp = sum_n + next_num
for j in range(l_y):
if not y_check[j] and tmp == n_y[j]:
y_check[j] = True
break
else:
check_x |= (1<<i)
queue.append([check_x,tmp])
if all(y_check):
return True
else:
return False
def solution(p, q):
n = len(p)
answer = []
for case in range(n):
answer.append(check(p[case],q[case]))
print()
return answer
print(solution([[5,3,2,2,1]],[[7,2,4]]))