File tree Expand file tree Collapse file tree 1 file changed +25
-0
lines changed Expand file tree Collapse file tree 1 file changed +25
-0
lines changed Original file line number Diff line number Diff line change @@ -191,6 +191,31 @@ class Solution {
191
191
192
192
Python:
193
193
194
+ ``` python3
195
+ class Solution :
196
+ def countBinary (self , s : str ) -> (int , int ):
197
+ zeros, ones = 0 , 0
198
+ for c in s:
199
+ if c == ' 0' :
200
+ zeros += 1
201
+ else :
202
+ ones += 1
203
+ return zeros, ones
204
+
205
+ def findMaxForm (self , strs : List[str ], m : int , n : int ) -> int :
206
+ dp = [[0 ]* (n + 1 ) for _ in range (m + 1 )] # 默认初始化0
207
+ # 遍历物品
208
+ for i in range (len (strs)):
209
+ # num_zeros = strs[i].count('0')
210
+ # num_ones = strs[i].count('1')
211
+ num_zeros, num_ones = self .countBinary(strs[i])
212
+ # 遍历背包容量且从后向前遍历!
213
+ for j in range (m, num_zeros - 1 , - 1 ):
214
+ for i in range (n, num_ones - 1 , - 1 ):
215
+ dp[j][i] = max (dp[j - num_zeros][i - num_ones] + 1 , dp[j][i])
216
+ return dp[m][n]
217
+ ```
218
+
194
219
195
220
Go:
196
221
You can’t perform that action at this time.
0 commit comments