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

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

相关文章:

  • 【Day41 LeetCode】单调栈问题
  • 什么是中间件中间件有哪些
  • 可解释性:走向透明与可信的人工智能
  • 浅谈线程安全问题的原因和解决方案
  • langchain学习笔记之消息存储在内存中的实现方法
  • Day3 25/2/16 SUN
  • Linux:用 clang 编译带 sched_ext 功能内核
  • 与传统光伏相比 城电科技的光伏太阳花有什么优势?
  • 最新智能优化算法: 阿尔法进化(Alpha Evolution,AE)算法求解23个经典函数测试集,MATLAB代码
  • 利用亚马逊AI代码助手生成、构建和编译一个游戏应用(下)
  • auto关键字的作用
  • Deepseek高效使用指南
  • 每日一题——最长上升子序列与最长回文子串
  • 渗透测试方向的就业前景怎么样?
  • PHP基础部分
  • 人工智能学习(八)之注意力机制原理解析
  • 赖莎莎:创意总监的跨洋之旅
  • 【数据采集】基于Selenium爬取猫眼Top100电影信息
  • 如何搭建Wi-Fi CVE漏洞测试环境:详细步骤与设备配置
  • 第四章 Vue 中的 ajax
  • 长三角铁路今日预计发送旅客420万人次,有望创单日客发量新高
  • 澎湃读报丨解放日报8个版聚焦:牢记嘱托,砥砺奋进
  • 陕西省通报6起违反八项规定典型问题,省卫健委原主任刘宝琴违规收受礼品礼金
  • 美参议院通过新任美国驻华大使任命,外交部回应
  • 海尔智家一季度营收791亿元:净利润增长15%,海外市场收入增超12%
  • 游客曝九寨沟打网约车被出租车围堵,景区回应:当地无合规网约车