|
| 1 | +package com.fishercoder.solutions; |
| 2 | + |
| 3 | +import java.util.ArrayDeque; |
| 4 | +import java.util.Arrays; |
| 5 | +import java.util.Deque; |
| 6 | + |
| 7 | +/** |
| 8 | + * 950. Reveal Cards In Increasing Order |
| 9 | + * |
| 10 | + * In a deck of cards, every card has a unique integer. You can order the deck in any order you want. |
| 11 | + * Initially, all the cards start face down (unrevealed) in one deck. |
| 12 | + * Now, you do the following steps repeatedly, until all cards are revealed: |
| 13 | + * Take the top card of the deck, reveal it, and take it out of the deck. |
| 14 | + * If there are still cards in the deck, put the next top card of the deck at the bottom of the deck. |
| 15 | + * If there are still unrevealed cards, go back to step 1. Otherwise, stop. |
| 16 | + * Return an ordering of the deck that would reveal the cards in increasing order. |
| 17 | + * |
| 18 | + * The first entry in the answer is considered to be the top of the deck. |
| 19 | + * |
| 20 | + * Example 1: |
| 21 | + * Input: [17,13,11,2,3,5,7] |
| 22 | + * Output: [2,13,3,11,5,17,7] |
| 23 | + * |
| 24 | + * Explanation: |
| 25 | + * We get the deck in the order [17,13,11,2,3,5,7] (this order doesn't matter), and reorder it. |
| 26 | + * After reordering, the deck starts as [2,13,3,11,5,17,7], where 2 is the top of the deck. |
| 27 | + * We reveal 2, and move 13 to the bottom. The deck is now [3,11,5,17,7,13]. |
| 28 | + * We reveal 3, and move 11 to the bottom. The deck is now [5,17,7,13,11]. |
| 29 | + * We reveal 5, and move 17 to the bottom. The deck is now [7,13,11,17]. |
| 30 | + * We reveal 7, and move 13 to the bottom. The deck is now [11,17,13]. |
| 31 | + * We reveal 11, and move 17 to the bottom. The deck is now [13,17]. |
| 32 | + * We reveal 13, and move 17 to the bottom. The deck is now [17]. |
| 33 | + * We reveal 17. |
| 34 | + * Since all the cards revealed are in increasing order, the answer is correct. |
| 35 | + * |
| 36 | + * Note: |
| 37 | + * 1 <= A.length <= 1000 |
| 38 | + * 1 <= A[i] <= 10^6 |
| 39 | + * A[i] != A[j] for all i != j |
| 40 | + */ |
| 41 | +public class _950 { |
| 42 | + public static class Solution1 { |
| 43 | + public int[] deckRevealedIncreasing(int[] deck) { |
| 44 | + Arrays.sort(deck); |
| 45 | + Deque<Integer> deque = new ArrayDeque<>(); |
| 46 | + for (int i = deck.length - 1; i >= 0; i--) { |
| 47 | + if (i != deck.length - 1) { |
| 48 | + deque.addFirst(deque.pollLast()); |
| 49 | + } |
| 50 | + deque.addFirst(deck[i]); |
| 51 | + } |
| 52 | + int[] result = new int[deck.length]; |
| 53 | + int i = 0; |
| 54 | + while (!deque.isEmpty()) { |
| 55 | + result[i++] = deque.pollFirst(); |
| 56 | + } |
| 57 | + return result; |
| 58 | + } |
| 59 | + } |
| 60 | +} |
0 commit comments