【leetcode】1422. 分割字符串的最大得分

文章目录

题目

1422. 分割字符串的最大得分

给你一个由若干 0 和 1 组成的字符串 s ,请你计算并返回将该字符串分割成两个 非空 子字符串(即 左 子字符串和 右 子字符串)所能获得的最大得分。

「分割字符串的得分」为 左 子字符串中 0 的数量加上 右 子字符串中 1 的数量。

示例 1:

输入:s = "011101"

输出:5

解释:

将字符串 s 划分为两个非空子字符串的可行方案有:

左子字符串 = "0" 且 右子字符串 = "11101",得分 = 1 + 4 = 5

左子字符串 = "01" 且 右子字符串 = "1101",得分 = 1 + 3 = 4

左子字符串 = "011" 且 右子字符串 = "101",得分 = 1 + 2 = 3

左子字符串 = "0111" 且 右子字符串 = "01",得分 = 1 + 1 = 2

左子字符串 = "01110" 且 右子字符串 = "1",得分 = 2 + 1 = 3

示例 2:

输入:s = "00111"

输出:5

解释:当 左子字符串 = "00" 且 右子字符串 = "111" 时,我们得到最大得分 = 2 + 3 = 5

示例 3:

输入:s = "1111"

输出:3

题解

python 复制代码
class Solution(object):
    def maxScore(self, s):
        """
        :type s: str
        :rtype: int
        """
        ans = 0
        
        for i in range(1, len(s)):
            score = 0
            for s1 in s[:i]:
                if s1 == '0':
                    score += 1
            for s2 in s[i:]:
                if s2 == '1':
                    score += 1
            ans = max(ans, score)
        return ans
        
相关推荐
云心雨禅31 分钟前
Ubuntu GRUB菜单密码重置教程
linux·运维·ubuntu
温暖的苹果1 小时前
【linux V0.11】init/main.c
linux·c语言
iCxhust2 小时前
一个用于在 Ubuntu 22.04.3 LTS 上显示文件系统超级块信息的 C 程序
linux·c语言·ubuntu
Bella的成长园地2 小时前
linux 系统依赖包查询命令汇总
linux·运维·服务器
Arthurmoo3 小时前
Linux系统集群部署模块之Keepalived双机热备
linux·git·github
hweiyu003 小时前
Linux 命令:uname
linux·运维·服务器
大时代11053 小时前
Linux 基础 IO
linux
RIVOTEK_OPENVELA3 小时前
OpenVela之 Arch Timer 驱动框架使用指南
linux·开源软件·iot
longerxin20204 小时前
在 CentOS 8 上彻底卸载 Kubernetes(k8s)
linux·kubernetes·centos
阿竹.4 小时前
Linux运维新手的修炼手扎之第19天
linux·运维·服务器