Given an integer, A. Find and Return first positive A integers in ascending order containing only digits 1, 2, and 3.
NOTE: All the A integers will fit in 32-bit integers.
Problem Constraints
1 <= A <= 29500
Input Format
The only argument given is integer A.
Output Format
Return an integer array denoting the first positive A integers in ascending order containing only digits 1, 2 and 3.
Example Input
Input 1:
A = 3
Output 1:
[1, 2, 3]
Explanation 1:
Output denotes the first 3 integers that contains only digits 1, 2 and 3.
Input 2:
A = 7
Output 2:
[1, 2, 3, 11, 12, 13, 21]
Explanation 2:
Output denotes the first 7 integers that contains only digits 1, 2 and 3.
let our result be stored in a result
array
What are the numbers that should go into the result?
Any number that contains 1 2 or 3
the length of result should not exceed A
What is the formula for a valid number?
Therefore current x 10 + 1
or current x 10 + 2
or current x 10 + 3
Loop till we get the desired length A
solve : function(A){
let queue = [1,2,3]
let result = []
while(result.length < A) {
current = queue.shift()
result.push(current)
queue.push(current * 10 +1)
queue.push(current * 10 +2)
queue.push(current * 10 +3)
}
return result
}