1422. Maximum Score After Splitting a String
https://leetcode.com/problems/maximum-score-after-splitting-a-string/
Given a string s
of zeros and ones, return the maximum score after splitting the string into two non-empty substrings (i.e. left substring and right substring).
The score after splitting a string is the number of zeros in the left substring plus the number of ones in the right substring.
Example 1:
Example 2:
Example 3:
Constraints:
2 <= s.length <= 500
The string
s
consists of characters '0' and '1' only.
由01组成的数组问左右划分后使得左边的count(l_0)和右边的count(l_1)的和最大(要求左右至少有一个元素),这个argmax(sum(count(l_0) + count(l_1)))是多少。直观的解法是前后各遍历一遍记录下以i点分界前面的0和后面的1的个数。实际只需要从头遍历一遍,以i为左边最后一个元素,记录当前遇到的0和1的个数,所求为argmax(count(l_0) + count(r_1)) = argmax(count(l_0) - count(l_1) + count(total_1)) ,因此遍历时再比较当前是否argmax(count(l_0) - count(l_1))。
Last updated
Was this helpful?