-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path474 Ones and Zeroes.py
179 lines (149 loc) · 5.1 KB
/
474 Ones and Zeroes.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#!/usr/bin/python3
"""
In the computer world, use restricted resource you have to generate maximum
benefit is what we always want to pursue.
For now, suppose you are a dominator of m 0s and n 1s respectively. On the other
hand, there is an array with strings consisting of only 0s and 1s.
Now your task is to find the maximum number of strings that you can form with
given m 0s and n 1s. Each 0 and 1 can be used at most once.
Note:
The given numbers of 0s and 1s will both not exceed 100
The size of given string array won't exceed 600.
Example 1:
Input: Array = {"10", "0001", "111001", "1", "0"}, m = 5, n = 3
Output: 4
Explanation: This are totally 4 strings can be formed by the using of 5 0s and
3 1s, which are “10,”0001”,”1”,”0”
Example 2:
Input: Array = {"10", "0", "1"}, m = 1, n = 1
Output: 2
Explanation: You could form "10", but then you'd have nothing left. Better form
"0" and "1".
Author: Rajeev Ranjan
"""
from collections import Counter
class Solution:
def findMaxForm(self, strs, m, n):
"""
0-1 knapsack
let F[p][q][i] be the max end at A[i], with p 0's and q 1's remaining
F[p][q][i] = max(F[p'][q'][i-1] + 1, F[p][q][i-1])
To save space, drop i
:type strs: List[str]
:type m: int
:type n: int
:rtype: int
"""
if not strs:
return 0
F = [[0 for _ in range(n + 1)] for _ in range(m + 1)]
z, o = self.count(strs[0])
for i in range(m+1):
for j in range(n+1):
if i + z<= m and j + o <= n:
F[i][j] = 1
for e in range(1, len(strs)):
z, o = self.count(strs[e])
for i in range(m+1):
for j in range(n+1):
if i + z <= m and j + o <= n:
F[i][j] = max(
F[i][j],
F[i + z][j + o] + 1
)
ret = max(
F[i][j]
for i in range(m + 1)
for j in range(n + 1)
)
return ret
def count(self, s):
z, o = 0, 0
for e in s:
if e == "0":
z += 1
else:
o += 1
return z, o
def findMaxForm_TLE(self, strs, m, n):
"""
0-1 knapsack
let F[p][q][i] be the max end at A[i], with p 0's and q 1's
F[p][q][i] = max(F[p'][q'][i-1] + 1, F[p][q][i-1])
:type strs: List[str]
:type m: int
:type n: int
:rtype: int
"""
if not strs:
return 0
F = [[[0 for _ in range(len(strs))] for _ in range(n + 1)] for _ in range(m + 1)]
count = Counter(strs[0])
for i in range(m+1):
for j in range(n+1):
if i + count["0"] <= m and j + count["1"] <= n:
F[i][j][0] = 1
for e in range(1, len(strs)):
count = Counter(strs[e])
for i in range(m+1):
for j in range(n+1):
if i + count["0"] <= m and j + count["1"] <= n:
F[i][j][e] = F[i + count["0"]][j + count["1"]][e-1] + 1
F[i][j][e] = max(F[i][j][e], F[i][j][e-1])
ret = max(
F[i][j][-1]
for i in range(m + 1)
for j in range(n + 1)
)
return ret
def findMaxForm_error(self, strs, m, n):
"""
0-1 knapsack
let F[p][q][i] be the max end at A[i], with p 0's and q 1's
F[p][q][i] = max(F[p'][q'][i-1] + 1, F[p][q][i-1])
:type strs: List[str]
:type m: int
:type n: int
:rtype: int
"""
if not strs:
return 0
F = [[[0 for _ in range(len(strs))] for _ in range(n + 1)] for _ in range(m + 1)]
count = Counter(strs[0])
if count["0"] <= m and count["1"] <= n:
F[m - count["0"]][n - count["1"]][0] += 1
for e in range(1, len(strs)):
count = Counter(strs[e])
for i in range(m+1):
for j in range(n+1):
if count["0"] <= i and count["1"] <= j:
F[i - count["0"]][j - count["1"]][e] = F[i][j][e-1] + 1
else:
F[i][j][e] = F[i][j][e-1]
ret = max(
F[i][j][-1]
for i in range(m + 1)
for j in range(n + 1)
)
return ret
def findMaxForm_error(self, strs, m, n):
"""
reward is 1 regarless of length, then greedy - error
:type strs: List[str]
:type m: int
:type n: int
:rtype: int
"""
strs.sort(key=len)
ret = 0
for a in strs:
count = Counter(a)
if count["0"] <= m and count["1"] <= n:
ret += 1
m -= count["0"]
n -= count["1"]
return ret
if __name__ == "__main__":
assert Solution().findMaxForm(["10", "0001", "111001", "1", "0"], 5, 3) == 4
assert Solution().findMaxForm(["10", "0", "1"], 1, 1) == 2
assert Solution().findMaxForm(["111", "1000", "1000", "1000"], 9, 3) == 3