-
-
Notifications
You must be signed in to change notification settings - Fork 298
/
Copy path474.py
106 lines (90 loc) · 3.76 KB
/
474.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
__________________________________________________________________________________________________
sample 48 ms submission
class Solution:
def findMaxForm(self, strs: List[str], m: int, n: int) -> int:
strs.sort(key=len)
#print(strs)
used_0=0
used_1=0
res=0
res_0=0
res_1=0
res_comb=0
for i in range(len(strs)):
tmp_0=strs[i].count("0")
#tmp_1=strs[i].count("1")
tmp_1=len(strs[i])-tmp_0
if m-used_0>=tmp_0 and n-used_1>=tmp_1:
res+=1
used_0+=tmp_0
used_1+=tmp_1
#return res
used_0=0
used_1=0
strs.sort(key=lambda k: k.count("0"))
for i in range(len(strs)):
tmp_0=strs[i].count("0")
#tmp_1=strs[i].count("1")
tmp_1=len(strs[i])-tmp_0
if m-used_0>=tmp_0 and n-used_1>=tmp_1:
res_0+=1
used_0+=tmp_0
used_1+=tmp_1
used_0=0
used_1=0
strs.sort(key=lambda k: k.count("1"))
for i in range(len(strs)):
tmp_0=strs[i].count("0")
#tmp_1=strs[i].count("1")
tmp_1=len(strs[i])-tmp_0
if m-used_0>=tmp_0 and n-used_1>=tmp_1:
res_1+=1
used_0+=tmp_0
used_1+=tmp_1
used_0=0
used_1=0
strs.sort(key=lambda k: (len(k),-k.count("0")*k.count("1")))
for i in range(len(strs)):
tmp_0=strs[i].count("0")
#tmp_1=strs[i].count("1")
tmp_1=len(strs[i])-tmp_0
if m-used_0>=tmp_0 and n-used_1>=tmp_1:
res_comb+=1
used_0+=tmp_0
used_1+=tmp_1
return max([res,res_0,res_1,res_comb])
__________________________________________________________________________________________________
sample 52 ms submission
class Solution:
def findMaxForm(self, strs: List[str], m: int, n: int) -> int:
results = self.keyComb(strs, m, n, len)
results_2 = self.keyComb(strs, m, n, lambda x: -1*len(x))
results_3 = self.keyComb(strs, m, n, lambda x: x.count('1'))
results_4 = self.keyComb(strs, m, n, lambda x: x.count('0'))
results_5 = self.keyComb(strs, m, n, lambda x: x.count('0'),
double=True, key_2=len)
results_6 = self.keyComb(strs, m, n, lambda x: x.count('0'),
double=True, key_2=lambda x: -1*len(x))
results_7 = self.keyComb(strs, m, n, lambda x: x.count('1'),
double=True, key_2=len)
results_8 = self.keyComb(strs, m, n, lambda x: x.count('1'),
double=True, key_2=lambda x: -1*len(x))
return max(len(results), len(results_2), len(results_3), len(results_4), len(results_5), len(results_6), len(results_7), len(results_8))
def keyComb(self, strs, m, n, key, double=False, key_2=None,):
# m's -> 0's, n's -> 1's
result = []
strs.sort(key=key)
if double:
strs.sort(key=key_2)
for str_i in strs:
ones = str_i.count('1')
zeros = str_i.count('0')
# print ('string {} ones {}, zeroes{}, m {}, n {} \n'.format(str_i, ones,zeros, m, n))
if zeros <= m and ones <= n:
m -= zeros
n -= ones
result.append(str_i)
if ones == 0 and zeros == 0:
break
return result
__________________________________________________________________________________________________