当前位置: 首页 > 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
http://www.dtcms.com/a/21630.html

相关文章:

  • 【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
  • 基于图像处理的裂缝检测与特征提取
  • easyCode代码模板配置
  • 【ESP32】ESP-IDF开发 | WiFi开发 | HTTPS服务器 + 搭建例程
  • Java 运算符
  • 【第11章:生成式AI与创意应用—11.1 文本生成与创意写作辅助的实现与优化】
  • 【ISO 14229-1:2023 UDS诊断全量测试用例清单系列:第三节】
  • 什么是 DeepSeek?
  • DeepSeek辅助测试测试一 -- DeepSeek加MaxKB知识库本地部署
  • 文件上传功能(四)——项目集成
  • 建筑兔零基础自学python记录22|实战人脸识别项目——视频人脸识别(下)11