파비의 매일매일 공부기록

2023.05.13 Today's Challenge 본문

Problem Solving/LeetCode

2023.05.13 Today's Challenge

fabichoi 2023. 5. 13. 23:45

https://leetcode.com/problems/count-ways-to-build-good-strings/

 

Count Ways To Build Good Strings - LeetCode

Can you solve this real interview question? Count Ways To Build Good Strings - 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 char

leetcode.com

오늘도 DP.
보이는 것에 비해 꽤나 간단한 코드로 끝이 나는 문제.

class Solution:
    def countGoodStrings(self, low: int, high: int, zero: int, one: int) -> int:
        dp = [1] + [0] * (high)
        mod = 10 ** 9 + 7

        for end in range(1, high + 1):
            if end >= zero:
                dp[end] += dp[end - zero]
            if end >= one:
                dp[end] += dp[end - one]
            dp[end] %= mod
        
        return sum(dp[low : high+1]) % mod
반응형

'Problem Solving > LeetCode' 카테고리의 다른 글

2023.05.15 Today's Challenge  (1) 2023.05.15
2023.05.14 Today's Challenge  (0) 2023.05.14
2023.05.12 Today's Challenge  (0) 2023.05.12
2023.05.11 Today's Challenge  (0) 2023.05.11
2023.05.10 Today's Challenge  (0) 2023.05.10
Comments