按照取完这个字符串后剩余0或1较多的逆序排列 第2种是对第一种情况的补充例如一个字符串011111111111,0只有一个1很多个 第一种情况对应Array = {“10”, “0001”, “111001”, “1”, “0”}, m = 5, n = 3 第二种情况对应Array = {“01111”, “001”, “100”}, m = 4, n = 4
classSolution:deffindMaxForm(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]
s1 =sorted(counter,key=lambda x:min(x[0],x[1]))
s2 =sorted(counter,key=lambda x:min(m-x[0],n-x[1]),reverse=True)returnmax(self._findMaxForm(s1, m, n),self._findMaxForm(s2, m, n))def_findMaxForm(self, s, m, n):
count =0for k in s:if m >= k[0]and n >= k[1]:
count +=1
m -= k[0]
n -= k[1]return count