BudiBadu Logo

Card Sorter Queue Balance

Sorting Easy 1 views
Like21

The city treasury relies on an automated card sorter to organize payment vouchers into ascending order. At the start of each batch, the sorter receives a list of voucher IDs and repeatedly performs one of two actions selected from a predefined schedule: remove the card at the front and place it at the back, or remove the card at the front and place it into a growing output pile. Once the schedule ends, the machine outputs every card currently in the output pile followed by whatever remains in the queue. Technicians want to simulate the final list so they can verify that no voucher is misplaced before nightly reconciliation.

You will receive the initial sequence cards representing the queue from front to back and a list operations of the same length, where each entry is either "rotate" or "deposit". For every "rotate" operation, move the first card to the end of the queue. For every "deposit" operation, remove the first card and append it to the output list. After processing the entire schedule, append the remaining queue cards to the output list in their current order. Return this final list.

Assume operations has the same length as the number of steps the machine will run. Every "deposit" operation occurs only when the queue is non-empty. Focus on producing the resulting order, not on optimizing time complexity.

Example 1:

Input: cards = [5,2,9,7], operations = ["deposit","rotate","deposit","deposit"]
Output: [5,9,7,2]
Explanation: The machine deposits 5, rotates to send 2 to the back, deposits 9, then deposits 7; the remaining queue holds only 2.

Example 2:

Input: cards = [4,1,3], operations = ["rotate","deposit","deposit"]
Output: [1,3,4]
Explanation: After rotating, the queue becomes [1,3,4]; depositing twice produces [1,3], leaving [4] in the queue.

Example 3:

Input: cards = [8,6], operations = ["deposit","deposit"]
Output: [8,6]
Explanation: Both cards move directly to the output pile.

Algorithm Flow

Recommendation Algorithm Flow for Card Sorter Queue Balance - Budibadu
Recommendation Algorithm Flow for Card Sorter Queue Balance - Budibadu

Best Answers

java
import java.util.*;
class Solution {
    public Object card_sorter_queue_balance(Object input) {
        Object[] args = (Object[]) input;
        int[] cards = (int[]) args[0];
        String[] ops = (String[]) args[1];
        
        LinkedList<Integer> q = new LinkedList<>();
        for (int c : cards) q.add(c);
        ArrayList<Integer> out = new ArrayList<>();
        
        for (String op : ops) {
            if (q.isEmpty()) break;
            if (op.equals("deposit")) {
                out.add(q.removeFirst());
            } else if (op.equals("rotate")) {
                q.add(q.removeFirst());
            }
        }
        while (!q.isEmpty()) out.add(q.removeFirst());
        
        int[] res = new int[out.size()];
        for (int i = 0; i < out.size(); i++) res[i] = out.get(i);
        return res;
    }
}