|
| 1 | +package array; |
| 2 | + |
| 3 | +import java.util.ArrayDeque; |
| 4 | +import java.util.Arrays; |
| 5 | + |
| 6 | +/** |
| 7 | + * Created by gouthamvidyapradhan on 12/08/2019 In a deck of cards, every card has a unique integer. |
| 8 | + * You can order the deck in any order you want. |
| 9 | + * |
| 10 | + * <p>Initially, all the cards start face down (unrevealed) in one deck. |
| 11 | + * |
| 12 | + * <p>Now, you do the following steps repeatedly, until all cards are revealed: |
| 13 | + * |
| 14 | + * <p>Take the top card of the deck, reveal it, and take it out of the deck. If there are still |
| 15 | + * cards in the deck, put the next top card of the deck at the bottom of the deck. If there are |
| 16 | + * still unrevealed cards, go back to step 1. Otherwise, stop. Return an ordering of the deck that |
| 17 | + * would reveal the cards in increasing order. |
| 18 | + * |
| 19 | + * <p>The first entry in the answer is considered to be the top of the deck. |
| 20 | + * |
| 21 | + * <p>Example 1: |
| 22 | + * |
| 23 | + * <p>Input: [17,13,11,2,3,5,7] Output: [2,13,3,11,5,17,7] Explanation: We get the deck in the order |
| 24 | + * [17,13,11,2,3,5,7] (this order doesn't matter), and reorder it. After reordering, the deck starts |
| 25 | + * as [2,13,3,11,5,17,7], where 2 is the top of the deck. We reveal 2, and move 13 to the bottom. |
| 26 | + * The deck is now [3,11,5,17,7,13]. We reveal 3, and move 11 to the bottom. The deck is now |
| 27 | + * [5,17,7,13,11]. We reveal 5, and move 17 to the bottom. The deck is now [7,13,11,17]. We reveal |
| 28 | + * 7, and move 13 to the bottom. The deck is now [11,17,13]. We reveal 11, and move 17 to the |
| 29 | + * bottom. The deck is now [13,17]. We reveal 13, and move 17 to the bottom. The deck is now [17]. |
| 30 | + * We reveal 17. Since all the cards revealed are in increasing order, the answer is correct. |
| 31 | + * |
| 32 | + * <p>Note: |
| 33 | + * |
| 34 | + * <p>1 <= A.length <= 1000 1 <= A[i] <= 10^6 A[i] != A[j] for all i != j |
| 35 | + * |
| 36 | + * <p>Solution: O(N) General idea is to start from the last element and build the array of element |
| 37 | + * in the backwards order. Use a doubly-ended queue which allows you to poll from either end of a |
| 38 | + * queue. |
| 39 | + */ |
| 40 | +public class RevealCardsInIncreasingOrder { |
| 41 | + public static void main(String[] args) { |
| 42 | + int[] A = {17, 13, 11, 2, 3, 5, 7}; |
| 43 | + int[] R = new RevealCardsInIncreasingOrder().deckRevealedIncreasing(A); |
| 44 | + } |
| 45 | + |
| 46 | + public int[] deckRevealedIncreasing(int[] deck) { |
| 47 | + Arrays.sort(deck); |
| 48 | + ArrayDeque<Integer> queue = new ArrayDeque<>(); |
| 49 | + for (int i = deck.length - 1; i >= 0; i--) { |
| 50 | + queue.offer(deck[i]); |
| 51 | + if (i == 0) break; |
| 52 | + int temp = queue.pollFirst(); |
| 53 | + queue.offer(temp); |
| 54 | + } |
| 55 | + int[] answer = new int[deck.length]; |
| 56 | + int i = 0; |
| 57 | + while (!queue.isEmpty()) { |
| 58 | + answer[i++] = queue.pollLast(); |
| 59 | + } |
| 60 | + return answer; |
| 61 | + } |
| 62 | +} |
0 commit comments