1234. Count Digit One — Numbers from 1 to N

HardDynamic ProgrammingDigit DP

Given an integer n, count the total number of times the digit 1 appears across all integers from 1 to n inclusive. The input is JSON {n}.

Input: JSON {n}.

Output: Integer — the total count of the digit 1.

Examples

Example 1
Input: {"n":13}
Output: 6
Explanation: 1, 10, 11 (twice), 12, 13.
Example 2
Input: {"n":0}
Output: 0
Explanation: No numbers.
Example 3
Input: {"n":1}
Output: 1
Explanation: Just the number 1.

Constraints

Asked by

AmazonGoogleMicrosoftMetaAdobe
Solve this problem in the editor →