[LeetCode]1422. Maximum Score After Splitting a String (Easy) Python-2023.13.22
Posted on 2023-12-22 16:35 Uriel 閱讀(47) 評論(0) 編輯 收藏 引用 所屬分類: 閑來無事重切Leet Code 、大水題將一個01字符串切割成兩個非空子串,最大化第一個字符串的0和第二個字符串的1,先統(tǒng)計1的總個數(shù),再從左到右掃一遍找最佳切割位置
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
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