Talentd
FleetCode

2719. Count of Integers

Hard38.9% acceptance2 companies

You are given two numeric strings num1 and num2 and two integers max_sum and min_sum. We denote an integer x to be good if:

  • num1 <= x <= num2
  • min_sum <= digit_sum(x) <= max_sum.

Return the number of good integers. Since the answer may be large, return it modulo 109 + 7.

Note that digit_sum(x) denotes the sum of the digits of x.

Example 1:

Input: num1 = "1", num2 = "12", min_sum = 1, max_sum = 8
Output: 11
Explanation: There are 11 integers whose sum of digits lies between 1 and 8 are 1,2,3,4,5,6,7,8,10,11, and 12. Thus, we return 11.

Example 2:

Input: num1 = "1", num2 = "5", min_sum = 1, max_sum = 5
Output: 5
Explanation: The 5 integers whose sum of digits lies between 1 and 5 are 1,2,3,4, and 5. Thus, we return 5.

Constraints:

  • 1 <= num1 <= num2 <= 1022
  • 1 <= min_sum <= max_sum <= 400

Asked at 2 companies

Hints open one at a time

Hint 1
Let f(n, l, r) denotes the number of integers from 1 to n with the sum of digits between l and r.
Hint 2
The answer is f(num2, min_sum, max_sum) - f(num-1, min_sum, max_sum).
Hint 3
You can calculate f(n, l, r) using digit dp.
34:06
Still stuck?Read 3 approaches, or watch a walkthrough.
Open solution
Loading editor…
1class Solution:
2 def count(self, num1: str, num2: str, min_sum: int, max_sum: int) -> int:
3
Starter codePython 3Spaces: 4
Examples
Input
"1"
"12"
1
8
Input
"1"
"5"
1
5
Your code is saved on this device.Solution