Order ID Removal Priority Based on Neighbors
You are given a list of unique integers representing order IDs. Determine their priority by repeatedly removing one order ID according to the values of its current neighbors.
Priority Rules
- An order ID is eligible if its value is greater than every neighbor it currently has.
- An order ID in the middle of the list must be greater than both its left and right neighbors.
- The first and last order IDs have only one neighbor and must be greater than that neighbor to be eligible.
- If the list contains only one order ID, that order ID is eligible.
- Among all eligible order IDs, remove the one with the smallest value.
- After removing an order ID, determine eligibility again using the updated neighboring order IDs.
Continue until the list is empty. Return the order IDs in the order in which they were removed.
Method Signature
List<Integer> prioritizeOrders(List<Integer> orderIds)
Parameters
orderIds: The order IDs in their initial sequence.
Returns
Return a List<Integer> containing the order IDs in their removal order.
Constraints
1 ≤ orderIds.size() ≤ 100,000
1 ≤ orderIds.get(i) ≤ 1,000,000,000
- All values in
orderIds are unique.
Examples
Example 1
prioritizeOrders(orderIds = List.of(8, 3, 6, 1, 5))
Output: [5, 6, 8, 3, 1]
Initially, 8, 6, and 5 are eligible. The smallest eligible value is 5, so it is removed first. The same rules are applied repeatedly to the updated list.
Example 2
prioritizeOrders(orderIds = List.of(2, 7, 4, 9, 3, 6))
Output: [6, 7, 9, 4, 3, 2]
Initially, 7, 9, and 6 are eligible. Since 6 is the smallest eligible value, it is removed first.
Example 3
prioritizeOrders(orderIds = List.of(1, 2, 3, 4))
Output: [4, 3, 2, 1]
Only the last order ID is eligible during each step.