Queue-Array: April POTD
Day 14/75:
Question: Reveal Cards In Increasing Order -(Medium)
You are given an integer array deck. There is a deck of cards where every card has a unique integer. The integer on the ith card is deck[i].
You can order the deck in any order you want. Initially, all the cards start face down (unrevealed) in one deck.
You will do the following steps repeatedly until all cards are revealed:
Return an ordering of the deck that would reveal the cards in increasing order.
Note that the first entry in the answer is considered to be the top of the deck.
Input: deck = [17,13,11,2,3,5,7]
Output: [2,13,3,11,5,17,7]
Intuition :
Given in question: Resultant order should reveal the card in Increasing Order
So, I considered result order and followed steps, to check if it is actually giving increasing order: Output: [2,13,3,11,5,17,7]
Steps: Reveal 1st and put next top at the back and continue till all cards are revealed.
Approach
领英推荐
Code :
Complexity :
Approach2:
Here, We won't be using skip flag to indicate position to place value in res.
Code :
Time Complexity : O(nlogn)
Space Complexity: O(n): As Queue is used to store indices.
My Leetocode Solution: https://leetcode.com/problems/reveal-cards-in-increasing-order/solutions/5008457/100-easy-explanation-with-solution-java/
Love this !!