29
1
A list of numbers is called monotonically increasing (or nondecreasing) is every element is greater than or equal to the element before it.
For example, 1, 1, 2, 4, 5, 5, 5, 8, 10, 11, 14, 14
is monotonically increasing.
Given a monotonically increasing list of positive integers that has an arbitrary number of empty spots denoted by ?
, fill in the empty spots with positive integers such that as many unique integers as possible are present in the list, yet it remains monotonically increasing.
There may be multiple ways to accomplish this. Any is valid.
Output the resulting list.
For example, if the input is
?, 1, ?, 1, 2, ?, 4, 5, 5, 5, ?, ?, ?, ?, 8, 10, 11, ?, 14, 14, ?, ?
it is guaranteed that without the empty spots the list will be monotonically increasing
1, 1, 2, 4, 5, 5, 5, 8, 10, 11, 14, 14
and your task is to assign positive integers to each
?
to maximize the number of distinct integers in the list while keeping it nondecreasing.One assignment that is not valid is
1, 1, 1, 1, 2, 3, 4, 5, 5, 5, 5, 5, 5, 5, 8, 10, 11, 14, 14, 14, 14, 14
because, while it is nondecreasing, it only has one more unique integer than the input, namely
3
.In this example it is possible to insert six unique positive integers and keep the list nondecreasing.
A couple possible ways are:1, 1, 1, 1, 2, 3, 4, 5, 5, 5, 6, 7, 8, 8, 8, 10, 11, 12, 14, 14, 15, 16 1, 1, 1, 1, 2, 3, 4, 5, 5, 5, 5, 6, 6, 7, 8, 10, 11, 13, 14, 14, 20, 200
Either of these (and many others) would be valid output.
All empty spots must be filled in.
There is no upper limit on integers that can be inserted. It's ok if very large integers are printed in scientific notation.
Zero is not a positive integer and should never be inserted.
In place of ?
you may use any consistent value that is not a positive integer, such as 0
, -1
, null
, False
, or ""
.
The shortest code in bytes wins.
More Examples
[input]
[one possible output] (a "*" means it is the only possible output)
2, 4, 10
2, 4, 10 *
1, ?, 3
1, 2, 3 *
1, ?, 4
1, 2, 4
{empty list}
{empty list} *
8
8 *
?
42
?, ?, ?
271, 828, 1729
?, 1
1, 1 *
?, 2
1, 2 *
?, 3
1, 3
45, ?
45, 314159265359
1, ?, ?, ?, 1
1, 1, 1, 1, 1 *
3, ?, ?, ?, ?, 30
3, 7, 10, 23, 29, 30
1, ?, 2, ?, 3, ?, 4
1, 1, 2, 3, 3, 3, 4
1, ?, 3, ?, 5, ?, 7
1, 2, 3, 4, 5, 6, 7 *
1, ?, 3, ?, 5, ?, ?, 7
1, 2, 3, 4, 5, 6, 7, 7
1, ?, ?, ?, ?, 2, ?, ?, ?, ?, 4, ?, 4, ?, ?, 6
1, 1, 1, 1, 1, 2, 3, 4, 4, 4, 4, 4, 4, 5, 6, 6
98, ?, ?, ?, 102, ?, 104
98, 99, 100, 101, 102, 103, 104 *
Probably a better way to phrase the problem that has a strict input,output pair for verification, would be "What is the highest possible number of distinct numbers in the sequence". That way all answers will output the same number and makes evaluating test cases much easier – Cruncher – 2017-03-16T15:59:31.557
@StewieGriffin You can assume the list values and length are below int maximums as usual. I only meant that it's ok to insert very large integers at the end if that's the way your algorithm works. – Calvin's Hobbies – 2017-03-16T20:24:20.813