Advertisement

Number of Digit One - LeetCode 233 Solution

Number of Digit One - Complete Solution Guide

Number of Digit One is LeetCode problem 233, a Hard level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

Given an integer n , count the total number of digit 1 appearing in all non-negative integers less than or equal to n . Example 1: Input: n = 13 Output: 6 Example 2: Input: n = 0 Output: 0 Constraints: 0 <= n <= 10 9

Detailed Explanation

The problem asks us to count the number of times the digit '1' appears in all non-negative integers from 0 up to a given integer 'n'. For example, if n = 13, we need to count the '1's in 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13. There are six 1s: one in '1', one in '10', two in '11', one in '12', and one in '13'. The input 'n' is constrained to be between 0 and 10^9.

Solution Approach

The solution iterates through each digit place (starting from the ones place) and calculates how many times the digit '1' appears in that specific place for all numbers from 0 to 'n'. It does this by considering the 'high' part (digits to the left of the current digit), the 'current' digit, and the 'low' part (digits to the right of the current digit). Based on the value of the 'current' digit, we use different formulas to determine the number of '1's in that place. For example, if the current digit is 0, the number of 1s is solely determined by the 'high' part. If the current digit is 1, the number of 1s is influenced by both the 'high' and 'low' parts. If the current digit is greater than 1, the number of 1s is determined by the 'high' part increased by 1.

Step-by-Step Algorithm

  1. Step 1: Initialize a counter 'count' to 0. This variable will store the total number of 1s.
  2. Step 2: Initialize a variable 'i' to 1. This variable represents the current digit place (1, 10, 100, 1000, etc.).
  3. Step 3: Iterate while 'i' is less than or equal to 'n'. In each iteration, we process one digit place.
  4. Step 4: Calculate 'high': The integer formed by digits to the left of the current digit. high = n / (i * 10)
  5. Step 5: Calculate 'curr': The digit in the current place. curr = (n / i) % 10
  6. Step 6: Calculate 'low': The integer formed by digits to the right of the current digit. low = n % i
  7. Step 7: If 'curr' is 0, the number of 1s in this place is 'high * i'. Add this to 'count'.
  8. Step 8: If 'curr' is 1, the number of 1s in this place is 'high * i + low + 1'. Add this to 'count'.
  9. Step 9: If 'curr' is greater than 1, the number of 1s in this place is '(high + 1) * i'. Add this to 'count'.
  10. Step 10: Multiply 'i' by 10 to move to the next digit place (ones -> tens -> hundreds, etc.).
  11. Step 11: After the loop finishes, return the 'count'.

Key Insights

  • Insight 1: We can iterate through each digit place (ones, tens, hundreds, etc.) and count the number of 1s appearing in that place.
  • Insight 2: For each digit place, we can determine the number of 1s based on the digits to the left (higher digits) and the digit in the current place.
  • Insight 3: We can divide the problem into three parts: the higher digits, the current digit, and the lower digits.

Complexity Analysis

Time Complexity: O(log(n))

Space Complexity: O(1)

Topics

This problem involves: Math, Dynamic Programming, Recursion.

Companies

Asked at: Atlassian, Google.