|
| 1 | +--- |
| 2 | +id: number-of-digit-one |
| 3 | +title: Number of Digit One |
| 4 | +sidebar_label: 233 Number of Digit One |
| 5 | +tags: |
| 6 | +- Dynamic Programming |
| 7 | +- Java |
| 8 | +- Recursion |
| 9 | +- Math |
| 10 | +description: "This document provides a solution where we count the total number of digit $1$ appearing in all non-negative integers less than or equal to n." |
| 11 | +--- |
| 12 | + |
| 13 | +## Problem |
| 14 | + |
| 15 | +Given an integer n, count the total number of $digit$ 1 appearing in all non-negative integers less than or equal to n. |
| 16 | + |
| 17 | +### Examples |
| 18 | + |
| 19 | +**Example 1:** |
| 20 | + |
| 21 | +``` |
| 22 | +Input: n = 13 |
| 23 | +
|
| 24 | +Output: 6 |
| 25 | +
|
| 26 | +``` |
| 27 | +**Example 2:** |
| 28 | +``` |
| 29 | +Input: n = 0 |
| 30 | +
|
| 31 | +Output: 0 |
| 32 | +
|
| 33 | +``` |
| 34 | +### Constraints |
| 35 | + |
| 36 | +- `0 <= n <= 10^9` |
| 37 | + |
| 38 | +--- |
| 39 | +## Approach |
| 40 | +There are four approaches discussed that helps to obtain the solution: |
| 41 | + |
| 42 | +1. **Dynamic Programming Table**: |
| 43 | + |
| 44 | + - Initialize **'count'** to $0$, which will store the total count of $1's$. |
| 45 | + |
| 46 | + - Use **'factor'** to isolate each digit position, starting from the units place and moving to higher places (tens, hundreds, etc.). |
| 47 | + |
| 48 | +3. **Iterative Analysis**: |
| 49 | + |
| 50 | + - Loop through each digit position using **'factor'**, which starts from $1$ and increases by a factor of $10$ in each iteration. |
| 51 | + |
| 52 | + - For each position defined by **'factor'**, determine: |
| 53 | + |
| 54 | + - **'lowerNumbers'**: Numbers to the right of the current digit. |
| 55 | + |
| 56 | + - **'currentDigit'**: The digit at the current position. |
| 57 | + |
| 58 | + - **'higherNumbers'**: Numbers to the left of the current digit. |
| 59 | + |
| 60 | +3. **Count Calculation**: |
| 61 | + |
| 62 | + - If **'currentDigit'** is $0$, then the count of $1's$ contributed by the current digit position comes solely from higher numbers. |
| 63 | + |
| 64 | + - If **'currentDigit'** is $1$, it includes all $1's$ contributed by higher numbers, plus the $1's$ in the lower numbers up to **'lowerNumbers + 1'**. |
| 65 | + |
| 66 | + - If **'currentDigit'** is greater than $1$, it includes all $1's$ contributed by higher numbers and the full set of lower numbers for that digit position. |
| 67 | + |
| 68 | +4. **Result**: |
| 69 | + |
| 70 | + - Return the accumulated **'count'** after processing all digit positions. |
| 71 | + |
| 72 | +## Solution for Number of Digit One |
| 73 | + |
| 74 | +This problem can be solved using dynamic programming. The problem requires to count the total number of digit $1$ appearing in all non-negative integers less than or equal to n. |
| 75 | + |
| 76 | +#### Code in Java |
| 77 | + |
| 78 | + ```java |
| 79 | +class Solution { |
| 80 | + public int countDigitOne(int n) { |
| 81 | + if (n <= 0) return 0; |
| 82 | + |
| 83 | + int count = 0; |
| 84 | + for (long factor = 1; factor <= n; factor *= 10) { |
| 85 | + long lowerNumbers = n - (n / factor) * factor; |
| 86 | + long currentDigit = (n / factor) % 10; |
| 87 | + long higherNumbers = n / (factor * 10); |
| 88 | + |
| 89 | + if (currentDigit == 0) { |
| 90 | + count += higherNumbers * factor; |
| 91 | + } else if (currentDigit == 1) { |
| 92 | + count += higherNumbers * factor + lowerNumbers + 1; |
| 93 | + } else { |
| 94 | + count += (higherNumbers + 1) * factor; |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + return count; |
| 99 | + } |
| 100 | + |
| 101 | + public static void main(String[] args) { |
| 102 | + Solution sol = new Solution(); |
| 103 | + |
| 104 | + // Test cases |
| 105 | + System.out.println(sol.countDigitOne(13)); |
| 106 | + System.out.println(sol.countDigitOne(0)); |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +``` |
| 111 | + |
| 112 | +### Complexity Analysis |
| 113 | + |
| 114 | +#### Time Complexity: $O(log_{10} n)$ |
| 115 | + |
| 116 | +> **Reason**: The time complexity is $O(log_{10} n)$, because we are processing each digit position from the least significant to the most significant, and the number of digit positions is logarithmic relative to the input size. |
| 117 | +
|
| 118 | +#### Space Complexity: $O(1)$ |
| 119 | + |
| 120 | +> **Reason**: The space complexity is $O(1)$, because we only use a constant amount of extra space regardless of the input size. |
| 121 | +
|
| 122 | +# References |
| 123 | + |
| 124 | +- **LeetCode Problem:** [Number of Digit One](https://leetcode.com/problems/number-of-digit-one/description/) |
| 125 | +- **Solution Link:** [Number of Digit One Solution on LeetCode](https://leetcode.com/problems/number-of-digit-one/solutions/) |
| 126 | +- **Authors LeetCode Profile:** [Vivek Vardhan](https://leetcode.com/u/vivekvardhan43862/) |
0 commit comments