|
4 | 4 | importjava.util.Arrays;
|
5 | 5 | importjava.util.Deque;
|
6 | 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 | 7 | publicclass_950 {
|
42 |
| -publicstaticclassSolution1 { |
43 |
| -publicint[]deckRevealedIncreasing(int[]deck) { |
44 |
| -Arrays.sort(deck); |
45 |
| -Deque<Integer>deque =newArrayDeque<>(); |
46 |
| -for (inti =deck.length -1;i >=0;i--) { |
47 |
| -if (i !=deck.length -1) { |
48 |
| -deque.addFirst(deque.pollLast()); |
| 8 | +publicstaticclassSolution1 { |
| 9 | +publicint[]deckRevealedIncreasing(int[]deck) { |
| 10 | +Arrays.sort(deck); |
| 11 | +Deque<Integer>deque =newArrayDeque<>(); |
| 12 | +for (inti =deck.length -1;i >=0;i--) { |
| 13 | +if (i !=deck.length -1) { |
| 14 | +deque.addFirst(deque.pollLast()); |
| 15 | + } |
| 16 | +deque.addFirst(deck[i]); |
| 17 | + } |
| 18 | +int[]result =newint[deck.length]; |
| 19 | +inti =0; |
| 20 | +while (!deque.isEmpty()) { |
| 21 | +result[i++] =deque.pollFirst(); |
| 22 | + } |
| 23 | +returnresult; |
49 | 24 | }
|
50 |
| -deque.addFirst(deck[i]); |
51 |
| - } |
52 |
| -int[]result =newint[deck.length]; |
53 |
| -inti =0; |
54 |
| -while (!deque.isEmpty()) { |
55 |
| -result[i++] =deque.pollFirst(); |
56 |
| - } |
57 |
| -returnresult; |
58 | 25 | }
|
59 |
| - } |
60 | 26 | }
|