Description:
In a deck of cards, every card has a unique integer. You can order the deck in any order you want.
Initially, all the cards start face down (unrevealed) in one deck.
Now, you do the following steps repeatedly, until all cards are revealed:
- Take the top card of the deck, reveal it, and take it out of the deck.
- If there are still cards in the deck, put the next top card of the deck at the bottom of the deck.
- If there are still unrevealed cards, go back to step 1. Otherwise, stop.
Return an ordering of the deck that would reveal the cards in increasing order.
The first entry in the answer is considered to be the top of the deck.
Example 1:
Input: [17,13,11,2,3,5,7]
Output: [2,13,3,11,5,17,7]
Explanation:
We get the deck in the order [17,13,11,2,3,5,7] (this order doesn't matter), and reorder it.
After reordering, the deck starts 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. The deck is now [3,11,5,17,7,13].
We reveal 3, and move 11 to the bottom. The deck is now [5,17,7,13,11].
We reveal 5, and move 17 to the bottom. The deck is now [7,13,11,17].
We reveal 7, and move 13 to the bottom. The deck is now [11,17,13].
We reveal 11, and move 17 to the bottom. The deck is now [13,17].
We reveal 13, and move 17 to the bottom. The deck is now [17].
We reveal 17.
Since all the cards revealed are in increasing order, the answer is correct.
Note:
- 1 <= A.length <= 1000
- 1 <= A[i] <= 10^6
- A[i] != A[j] for all i != j
题意:给定一个牌序列,要求实现了下面的操作之后(一开始扑克牌牌面是朝下的,表示未揭面),移出的牌面大小的升序的;
- 将最上面的一张牌揭面并移出;
- 如果第1步后,下面还有牌,将最上方的牌放置在最下面
- 如果还有未揭面的牌,继续执行1,2步,否则,结束
解法:我们首先将这些牌按照牌面的大小进行排序后得到我们最后应该移出的顺序;因为每次将最上面的牌揭面后移出后,如果下方还有牌,要将下方的牌放置在最下方,因此,对于排序后的牌来说,我们的访问应当是跨过两个未访问过的元素;
e.g
我们处理序列deck=[17,13,11,2,3,5,7],对其进行排序得到deck=[2,3,5,7,11,13,17]
我们定义标记数组visited和最终的结果result;记此时访问result的下标为index=0(即顺序填充deck排序元素的下标位置);我们第一次访问的必定是第一个元素如下:
index | 0 | 1 | 2 | 3 | 4 | 5 | 6 |
---|---|---|---|---|---|---|---|
visited[index] | true | false | false | false | false | false | false |
result[index] | 2 | 0 | 0 | 0 | 0 | 0 | 0 |
deck[index] | 2 | 3 | 5 | 7 | 11 | 13 | 17 |
第二次访问时,我们跨过两个未访问过的元素到达index=2,此时状态为:
index | 0 | 1 | 2 | 3 | 4 | 5 | 6 |
---|---|---|---|---|---|---|---|
visited[index] | true | false | true | false | false | false | false |
result[index] | 2 | 0 | 3 | 0 | 0 | 0 | 0 |
deck[index] | 2 | 3 | 5 | 7 | 11 | 13 | 17 |
第三次访问时,同样方式我们到达index=4,此时状态为:
index | 0 | 1 | 2 | 3 | 4 | 5 | 6 |
---|---|---|---|---|---|---|---|
visited[index] | true | false | true | false | true | false | false |
result[index] | 2 | 0 | 3 | 0 | 5 | 0 | 0 |
deck[index] | 2 | 3 | 5 | 7 | 11 | 13 | 17 |
第四次访问时,index=6,此时状态为:
index | 0 | 1 | 2 | 3 | 4 | 5 | 6 |
---|---|---|---|---|---|---|---|
visited[index] | true | false | true | false | true | false | true |
result[index] | 2 | 0 | 3 | 0 | 5 | 0 | 7 |
deck[index] | 2 | 3 | 5 | 7 | 11 | 13 | 17 |
第五次访问时,遇到末尾时重新回到开到,同样跨过两个未访问过的元素,因为此时index=0和index=2都访问过了,因此,index=3,此时状态为:
index | 0 | 1 | 2 | 3 | 4 | 5 | 6 |
---|---|---|---|---|---|---|---|
visited[index] | true | false | true | true | true | false | true |
result[index] | 2 | 0 | 3 | 11 | 5 | 0 | 7 |
deck[index] | 2 | 3 | 5 | 7 | 11 | 13 | 17 |
第六次访问时,同样的因为index=4,index=6,index=0都访问过了,因此,index=1,此时状态为:
index | 0 | 1 | 2 | 3 | 4 | 5 | 6 |
---|---|---|---|---|---|---|---|
visited[index] | true | true | true | true | true | false | true |
result[index] | 2 | 13 | 3 | 11 | 5 | 0 | 7 |
deck[index] | 2 | 3 | 5 | 7 | 11 | 13 | 17 |
第七次访问时,index=2,3,4,6,0,1,2,3,4都访问过了,因此,index=5,此时状态为:
index | 0 | 1 | 2 | 3 | 4 | 5 | 6 |
---|---|---|---|---|---|---|---|
visited[index] | true | true | true | true | true | true | true |
result[index] | 2 | 13 | 3 | 11 | 5 | 17 | 7 |
deck[index] | 2 | 3 | 5 | 7 | 11 | 13 | 17 |
所以最后得到的result=[2,13,3,11,5,17,7];这里需要说明的一定是,我们每次是需要在result中找到一个可以填充的位置后,在deck中按照次序提供填充的元素(比如第一次填充是提供2,第二次为3,第三次为5…最后一次为17);
Java
class Solution {
public int[] deckRevealedIncreasing(int[] deck) {
int[] result = new int[deck.length];
boolean[] visited = new boolean[deck.length];
Arrays.sort(deck);
int index = 0;
result[0] = deck[0];
visited[0] = true;
for (int i = 1; i < deck.length; i++) {
while (visited[index]) index = (index + 1) % result.length;
index = (index + 1) % result.length;
while (visited[index]) index = (index + 1) % result.length;
result[index] = deck[i];
visited[index] = true;
}
return result;
}
}