1834. Factorial Digits

Medium3.5 points estimatedMathsArraySimulation3 hints

In the factorial number system, the digit at position i, counting 1 from the right, is multiplied by i! and may be anything from 0 to i. Every non-negative integer can be written this way in exactly one way with no leading zeros, so n equals d_k * k! + d_(k-1) * (k-1)! + ... + d_1 * 1!, where each d_i is between 0 and i and the leading digit d_k is not 0.

Given the integer n, return the digits d_k, d_(k-1), ..., d_1 as an array, most significant first. For n = 0 return [0]. For example, 463 is [3, 4, 1, 0, 1] because 3*120 + 4*24 + 1*6 + 0*2 + 1*1 = 463. Aim for O(k) time, where k is the number of digits in the answer.

Example 1

Input:
n = 463
Output:
[3,4,1,0,1]
Explanation:

3*120 + 4*24 + 1*6 + 0*2 + 1*1 = 463.

Example 2

Input:
n = 24
Output:
[1,0,0,0]
Explanation:

24 is 1*24 followed by zeros for 6, 2 and 1.

Example 3

Input:
n = 0
Output:
[0]
Explanation:

Zero is a single 0.

Constraints

0 ≤ n ≤ 1015

How this problem is judged

Answers
Your answer must match exactly. Numbers compare by value, so 2 and 2.0 are equal.

Expected complexity

Time
O(k)
Space
O(k)

What the author was aiming for. Your own solution is not measured against it.

Asked in an interview

Were you asked this in an interview? Say where, anonymously.

Code
Loading the editor…