Description Link to heading

950. Reveal Cards In Increasing Order (Medium)

Solution Link to heading

This problem can be solved through direct simulation according to the given instructions. The use of recursion is not necessary. Since the problem involves popping the first element, a double-ended queue (deque) is a suitable data structure to use. The deque will store the indices of elements, and we can proceed with the simulation accordingly.

Code Link to heading

class Solution {
public:
    vector<int> deckRevealedIncreasing(vector<int>& deck) {
    	sort(deck.begin(), deck.end());
    	int n = deck.size();
    	vector<int> res(n);
    	deque<int> dq;
    	for (int i = 0; i < n; ++i) {
    		dq.push_back(i);
    	}
    	int cnt = 0;
    	for (int card : deck) {
    		res[dq.front()] = card;
    		dq.pop_front();
    		if (!dq.empty()) {
    			dq.push_back(dq.front());
    			dq.pop_front();
    		}
    	}
    	return res;
    }
};