LeetCode-Reveal Cards In Increasing Order

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:

  1. Take the top card of the deck, reveal it, and take it out of the deck.
  2. If there are still cards in the deck, put the next top card of the deck at the bottom of the deck.
  3. 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. 将最上面的一张牌揭面并移出;
  2. 如果第1步后,下面还有牌,将最上方的牌放置在最下面
  3. 如果还有未揭面的牌,继续执行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排序元素的下标位置);我们第一次访问的必定是第一个元素如下:

index0123456
visited[index]truefalsefalsefalsefalsefalsefalse
result[index]2000000
deck[index]2357111317

第二次访问时,我们跨过两个未访问过的元素到达index=2,此时状态为:

index0123456
visited[index]truefalsetruefalsefalsefalsefalse
result[index]2030000
deck[index]2357111317

第三次访问时,同样方式我们到达index=4,此时状态为:

index0123456
visited[index]truefalsetruefalsetruefalsefalse
result[index]2030500
deck[index]2357111317

第四次访问时,index=6,此时状态为:

index0123456
visited[index]truefalsetruefalsetruefalsetrue
result[index]2030507
deck[index]2357111317

第五次访问时,遇到末尾时重新回到开到,同样跨过两个未访问过的元素,因为此时index=0和index=2都访问过了,因此,index=3,此时状态为:

index0123456
visited[index]truefalsetruetruetruefalsetrue
result[index]20311507
deck[index]2357111317

第六次访问时,同样的因为index=4,index=6,index=0都访问过了,因此,index=1,此时状态为:

index0123456
visited[index]truetruetruetruetruefalsetrue
result[index]213311507
deck[index]2357111317

第七次访问时,index=2,3,4,6,0,1,2,3,4都访问过了,因此,index=5,此时状态为:

index0123456
visited[index]truetruetruetruetruetruetrue
result[index]2133115177
deck[index]2357111317

所以最后得到的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;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值