From 34c96e2d8e94dbc19ca3af07982475f44c268f81 Mon Sep 17 00:00:00 2001 From: chayan das <110921638+Chayandas07@users.noreply.github.com> Date: Wed, 10 Apr 2024 16:25:36 +0530 Subject: [PATCH] Create 950. Reveal Cards In Increasing Order --- 950. Reveal Cards In Increasing Order | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 950. Reveal Cards In Increasing Order diff --git a/950. Reveal Cards In Increasing Order b/950. Reveal Cards In Increasing Order new file mode 100644 index 0000000..86db039 --- /dev/null +++ b/950. Reveal Cards In Increasing Order @@ -0,0 +1,26 @@ +class Solution { +public: + vector deckRevealedIncreasing(vector& deck) { + sort(deck.begin(), deck.end()); // Sort the deck in increasing order + + int n = deck.size(); + vector result(n); + deque indices; + + for (int i = 0; i < n; i++) { + indices.push_back(i); // Initialize deque with indices 0, 1, 2, ..., n-1 + } + + for (int card : deck) { + int idx = indices.front(); // Get the next available index + indices.pop_front(); // Remove the index from the front + result[idx] = card; // Place the card in the result array + if (!indices.empty()) { + indices.push_back(indices.front()); // Move the used index to the end of deque + indices.pop_front(); // Remove the index from the front + } + } + + return result; + } +};