将一个01字符串切割成两个非空子串,最大化第一个字符串的0和第二个字符串的1,先统计1的总个数,再从左到右扫一遍找最佳切割位置
1 #1422
2 #Runtime: 13 ms (Beats 90.28%)
3 #Memory: 13.3 MB (Beats 56.94%)
4
5 class Solution(object):
6 def maxScore(self, s):
7 """
8 :type s: str
9 :rtype: int
10 """
11 ones = s[1:].count('1')
12 if s[0] == '0':
13 zeros = 1
14 else:
15 zeros = 0
16 ans = ones + zeros
17 for i in xrange(1, len(s) - 1):
18 if s[i] == '0':
19 zeros += 1
20 else:
21 ones -= 1
22 ans = max(ans, ones + zeros)
23 return ans