AmazonOA #5
MERN E-commerce: Optimize Box IDs
Estimated MediumJudge-ready
Topics (AI-labelled)GreedyIn-place Arrays
Amazon MERN #5 is an interleaved algorithm problem that is already included in the main algorithm library; it is the same problem as Amazon #34 "Get Minimum String (Box Lex-Smallest)". The Amazon #13 that the #5 page points to is actually a different interval-counting problem, so this entry is mapped to #34 by its title and problem meaning.
One operation takes out any digit d, changes it to min(d+1,9), and inserts it at any position. Any number of operations (including 0) may be performed; find the lexicographically smallest final string, keeping leading zeros.
This site's standard I/O, examples, and judging reuse the verified Amazon #34 and do not redefine the operation.
Input
A single line containing a digit string of length 1..200000.
Output
Output the lexicographically smallest digit string, keeping leading zeros.
Example 1
Input:
26547
Output:
24677
Example 2
Input:
0
Output:
0
Example 3
Input:
21
Output:
13
Example notes
This problem is the same as Amazon #34 and reuses its verified examples, reference implementation, and tests; the original #5 link to Amazon #13 is unrelated to this problem.
Editorial
Loading solution…