Skip to content

Commit ceca8a9

Browse files
authored
Create 950. Reveal Cards In Increasing Order (#453)
2 parents 748c87f + 34c96e2 commit ceca8a9

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

950. Reveal Cards In Increasing Order

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
class Solution {
2+
public:
3+
vector<int> deckRevealedIncreasing(vector<int>& deck) {
4+
sort(deck.begin(), deck.end()); // Sort the deck in increasing order
5+
6+
int n = deck.size();
7+
vector<int> result(n);
8+
deque<int> indices;
9+
10+
for (int i = 0; i < n; i++) {
11+
indices.push_back(i); // Initialize deque with indices 0, 1, 2, ..., n-1
12+
}
13+
14+
for (int card : deck) {
15+
int idx = indices.front(); // Get the next available index
16+
indices.pop_front(); // Remove the index from the front
17+
result[idx] = card; // Place the card in the result array
18+
if (!indices.empty()) {
19+
indices.push_back(indices.front()); // Move the used index to the end of deque
20+
indices.pop_front(); // Remove the index from the front
21+
}
22+
}
23+
24+
return result;
25+
}
26+
};

0 commit comments

Comments
 (0)