diff --git a/Level 1/Week 06/Queue/950. Reveal Cards In Increasing Order/code.cpp b/Level 1/Week 06/Queue/950. Reveal Cards In Increasing Order/code.cpp index 9c60729..214fac3 100644 --- a/Level 1/Week 06/Queue/950. Reveal Cards In Increasing Order/code.cpp +++ b/Level 1/Week 06/Queue/950. Reveal Cards In Increasing Order/code.cpp @@ -3,7 +3,77 @@ #include using namespace std; -// 🧠 Approach: + +/* +Approach 1: +-------- + +Sort the Deck +Since the goal is to reveal the cards in increasing order, the first step is to sort the input deck. +For example, if the deck is [17,13,11,2,3,5,7], the sorted version would be [2,3,5,7,11,13,17]. + +The Reverse Simulation Method +Trying to simulate the card-revealing process forwards to find the correct initial order is difficult. It is much easier to work backward from the final, sorted state. +The steps for this reverse process are: +Start with an empty list or deque. +Take the largest card from your sorted list and place it into your empty list. +Now, for every subsequent card (working from largest to smallest), first take the last element currently in your list and move it to the front. Then, add the new card to the front. + + +Final Result +After you have placed all the cards from the sorted list back into the new list using this method, the resulting order is the correct initial deck arrangement that satisfies the problem's conditions. + +Example +Let's use the sorted deck: [2,3,5,7,11,13,17]. We will work backward from 17 down to 2. +Start with an empty list: [] +Insert 17: [17] +Move the last element (17) to the front, then insert 13 at the front: [13,17] +Move the last element (17) to the front, then insert 11 at the front: [11,17,13] +Move the last element (13) to the front, then insert 7 at the front: [7,13,11,17] +Move the last element (17) to the front, then insert 5 at the front: [5,17,7,13,11] +Move the last element (11) to the front, then insert 3 at the front: [3,11,5,17,7,13] +Move the last element (13) to the front, then insert 2 at the front: [2,13,3,11,5,17,7] +The final answer is [2,13,3,11,5,17,7]. + +Complexity Analysis +Sorting: The initial sort of the deck takes O(nlogn) time. +Reverse Simulation: We iterate through the n cards once, and each operation (adding and moving elements in a deque) takes, on average, constant time. This part of the process takes O(n) time. +Total: The overall time complexity is dominated by the sorting step, resulting in O(nlogn). + + +*/ + + + +class Solution { +public: + vector deckRevealedIncreasing(vector& deck) { + sort(deck.begin(), deck.end()); + deque dq; + + // reverse simulation + for (int i = deck.size() - 1; i >= 0; i--) { + if (!dq.empty()) { + int back_ele = dq.back(); + dq.pop_back(); + dq.push_front(back_ele); + } + + dq.push_front(deck[i]); + } + + // convert deque to array + vector result(dq.begin(), dq.end()); + + return result; + } +}; + + + + +// Previous Approch +// 🧠 Approach 2: // This problem requires working backwards from the desired result. // 1. Sort the deck to get the desired increasing order. // 2. Use a queue to simulate the reverse process of the card revealing. @@ -16,7 +86,7 @@ using namespace std; // ✅ Time Complexity: O(n log n), for sorting // ✅ Space Complexity: O(n), for the queue and result array -class Solution { +class Solution2 { public: vector deckRevealedIncreasing(vector& deck) { int n = deck.size(); @@ -46,4 +116,4 @@ class Solution { return result; } -}; \ No newline at end of file +};