1771. Balanced Ticket Numbers
A transit company prints tickets whose serial number has exactly 2*h decimal digits, leading zeros allowed, so the serials run from 00...0 to 99...9. A serial is called balanced when the sum of its first h digits equals the sum of its last h digits.
Given h, return how many balanced serials exist, as a long. There is no tie-break to apply because the answer is a single count. Do not look at all 10^(2h) serials: enumerate the 10^h possible halves once, tally how many halves produce each digit sum, and then add up the square of every tally, since any two halves with the same sum form a balanced serial. This takes O(h * 10^h) time and O(h) extra space.
Example 1
- Input:
- h = 1
- Output:
- 10
- Explanation:
With one digit per half, each of the 10 digits matches only itself, so there are 10 balanced serials.
Example 2
- Input:
- h = 2
- Output:
- 670
- Explanation:
Two-digit halves have digit sums with tallies whose squares add up to 670.
Constraints
1 ≤ h ≤ 5
How this problem is judged
- Answers
- Your answer must match exactly. Numbers compare by value, so 2 and 2.0 are equal.
- Time per case
- Python 4,000 msC++ 1,000 msJava 2,000 msJavaScript 2,000 msTypeScript 2,000 ms
Expected complexity
- Time
- O(h * 10^h)
- Space
- O(h)