Problem
A number is considered perfect if its digits sum up to exactly 10.
Given a positive integer n
, return the n
-th perfect number.
Examples
Example 1:
|
|
Example 2:
|
|
Solution
Method 1 - Brute Force
Keep on generating the numbers with sum of digits as 10, and when count is n, return the number.
Code
|
|
Complexity
- ⏰ Time complexity:
O(n * log_10(n))
- 🧺 Space complexity:
O(1)
Method 2 - Slightly more efficient
Lets look at some numbers in series - 19, 28, 37, 46… and so on. It becomes apparent that they differ by multiple of 9. But, not every number in this sequence adds up to 10—the number 100 being a case in point. Therefore, rather than evaluating each number individually, we begin the sequence at 19 and increase in steps of 9.
Code
|
|
Complexity
- ⏰ Time complexity:
O(n * log_10(n))
- 🧺 Space complexity:
O(1)