当前位置: 首页 > article >正文

Leetcode 2466. Count Ways To Build Good Strings

Problem

Given the integers zero, one, low, and high, we can construct a string by starting with an empty string, and then at each step perform either of the following:

  • Append the character ‘0’ zero times.
  • Append the character ‘1’ one times.

This can be performed any number of times.

A good string is a string constructed by the above process having a length between low and high (inclusive).

Return the number of different good strings that can be constructed satisfying these properties. Since the answer can be large, return it modulo 1 0 9 + 7 10^9 + 7 109+7.

Algorithm

Dynamic Programming (DP). Define dp[i] as the number of valid strings of length i that can be formed using the given rules.
d p [ i ] = d p [ i − zero ] + d p [ i − one ] dp[i] = dp[i - \text{zero}] + dp[i - \text{one}] dp[i]=dp[izero]+dp[ione]

This is valid if i - zero >= 0 and i - one >= 0.

Code

class Solution:
    def countGoodStrings(self, low: int, high: int, zero: int, one: int) -> int:
        dp = [0] * (high + 1)
        dp[0] = 1
        for i in range(high + 1):
            if i >= zero:
                dp[i] = (dp[i] + dp[i - zero]) % 1000000007
            if i >= one:
                dp[i] = (dp[i] + dp[i - one]) % 1000000007
        
        ans = 0
        for i in range(low, high+1):
            ans = (ans + dp[i]) % 1000000007
        return ans

http://www.kler.cn/a/549011.html

相关文章:

  • 《RCooper: 一个真实世界的大规模道路边协同感知数据集》学习笔记
  • Docker高级篇
  • 09 解决方案 - 开源机器人+具身智能+AI
  • Hot100 堆
  • 机器视觉3D工业相机机器视觉检测的优缺点
  • 游戏引擎学习第106天
  • 算法日记19:SC71多元最短路(Floyd)
  • 【从0做项目】Java搜索引擎(4)——性能优化~烧脑~~~
  • MyBatis:动态SQL高级标签使用方法指南
  • 数据库基本概念及基本使用
  • 【一文读懂】HTTP与Websocket协议
  • Python 2 和 Python 3 在字符串编码上的差异
  • 人工智障的软件开发-自动流水线CI/CD篇-docker+jenkins部署之道
  • FPGA开发进阶指南:从基础到精通的技术跃迁
  • AI大模型的文本流如何持续吐到前端,实时通信的技术 SSE(Server-Sent Events) 认知
  • 【计算机网络】数据链路层数据帧(Frame)格式
  • 对项目交接的一些思考
  • 【c++】【Linux】【进程】线程终止/崩溃 会导致进程终止/崩溃 吗?
  • matlab汽车动力学半车垂向振动模型
  • 服务器部署DeepSeek,通过Ollama+open-webui部署