-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path10apr.txt
28 lines (28 loc) · 1.08 KB
/
10apr.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
class Solution {
public:
vector<int> deckRevealedIncreasing(vector<int>& deck) {
// Sort the deck in descending order
sort(rbegin(deck), rend(deck)); // Initialize a deque to simulate the card revealing process
deque<int> dq; // Number of cards in the deck
int n = deck.size(); // Initialize deque with the largest card
dq.push_front(deck[0]); // Simulate the revealing process
for(int i = 1; i < n; i++) {
// Take the top card from the back of the deque
int x = dq.back();
// Remove this card from the deque
dq.pop_back();
// Place this revealed card at the front of the deque
dq.push_front(x);
// Add the current card from the sorted deck to the front of the deque
dq.push_front(deck[i]);
}
// Retrieve the revealed cards in increasing order
vector<int> ans;
while(!dq.empty()) {
ans.push_back(dq.front());
dq.pop_front();
}
// Return the ordered deck
return ans;
}
};