-
Notifications
You must be signed in to change notification settings - Fork 481
/
0474.py
30 lines (25 loc) · 823 Bytes
/
0474.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
class Solution:
def findMaxForm(self, strs, m, n):
"""
:type strs: List[str]
:type m: int
:type n: int
:rtype: int
"""
counter = [[i.count('0'), i.count('1')] for i in strs]
arr1 = sorted(counter, key=lambda s: -min(m - s[0], n - s[1]))
arr2 = sorted(counter, key=lambda s: min(s[0], s[1]))
return max(self._findMaxForm(arr1, m, n), self._findMaxForm(arr2, m, n))
def _findMaxForm(self, arr, m, n):
result = 0
for per in arr:
if m >= per[0] and n >= per[1]:
result += 1
m -= per[0]
n -= per[1]
return result
if __name__ == '__main__':
strs = ["10", "0001", "111001", "1", "0"]
m = 5
n = 3
print(Solution().findMaxForm(strs, m, n))