The team at an Amazon warehouse is given a task to optimize the packing of a set of boxes with different IDs.
Each box is labeled with an ID, and these boxes are currently arranged in a single row from left to right, where the ID of the i-th box is represented by the string boxIds consisting of digits from 0 to 9 inclusive.
To make the packing more efficient, the team can perform the following operation any (possibly zero) number of times:
- Choose an index
iand remove the digitboxIds[i]. - Then insert the box with ID
min(boxIds[i] + 1, 9)on any position (at the beginning, at the end, or in between any two adjacent boxes) in the row.
Given a string boxIds, find the lexicographically minimal string of boxes using these operations.
Note: A string X is lexicographically smaller than a string Y of the same length if and only if: In the first position where X and Y differ, the string X has a smaller digit than the corresponding digit in Y.
boxIds = "26547" return = "24677"
1 <= |boxIds| <= 2 * 10^5boxIds consists of only digits from 0 to 9. Note that boxIds is just a string consisting of digits, so leading zeros are allowed.- Count Promotional PeriodsOA · Seen Jun 2026
- Find Maximum Total Amount (SDE I, Fungible :)Seen Jun 2026
- Get Minimum AmountOA · Seen Jun 2026
- Find Minimum CostOA · Seen Jun 2026
- Get Smallest Base SegmentOA · Seen Jun 2026
- Maximum Non-Adjacent House ValueONSITE INTERVIEW · Seen Jun 2026
- Running Delivery Time MediansONSITE INTERVIEW · Seen Jun 2026
- Select Least Resource TasksOA · Seen Jun 2026
public String optimizeBoxIds(String boxIds) {
// write your code here
}