-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday13.py
75 lines (65 loc) · 1.83 KB
/
day13.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
# def findMaxLength(nums):
# subarrs = allSubArrays(nums)
# # print(subarrs)
# evenArrs = []
# for i in range(len(subarrs)):
# if subarrs[i].count(0)==subarrs[i].count(1):
# evenArrs.append(subarrs[i])
# maxsumArr = []
# currentsumArr = evenArrs[0]
# for i in range(len(evenArrs)):
# if len(evenArrs[i]) > len(currentsumArr):
# maxsumArr = evenArrs[i]
# else:
# maxsumArr = currentsumArr
# print("printing even arrays")
# print(evenArrs)
# print(maxsumArr)
# return len(maxsumArr)
# def allSubArrays(L,L2=None):
# if L2==None:
# L2 = L[:-1]
# if L==[]:
# if L2==[]:
# return []
# return allSubArrays(L2,L2[:-1])
# return [L]+allSubArrays(L[1:],L2)
# print(findMaxLength([0,0,1,0,0,0,1,1]))
# def getAllWindows(L):
# for w in range(1, len(L)+1):
# for i in range(len(L)-w+1):
# yield L[i:i+w]
# def findMaxLength(nums):
# dict = {0:-1}
# subarr, count = 0,0
# for i in range(len(nums)):
# if nums[i] == 1:
# count+=1
# else:
# count-= 1
# if count == 0:
# subarr = i+1
# if count in dict:
# max(subarr, i-dict[count])
# else:
# dict[count] = i
# return subarr
def findMaxLength( nums):
c,d,m = 0,{0:-1},0
for key,value in enumerate(nums):
c += 2*value -1
if c in d:
m = max(m,key-d[c])
else:
d[c] = key
return m
# def findMaxLength(nums):
# c,d,m = 0,{0:0},0
# for i,v in enumerate(nums):
# c += 2*v -1
# if c in d:
# m = max(m,i+1-d[c])
# else:
# d[c] = i+1
# return m
print(findMaxLength([0,0,1,0,0,1,1]))