Reveal Cards In Increasing Order LT950
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 alli != j
Idea 1. 官方的妙法,直接由答案数组来模拟游戏,有点意思
Time complexity: O(n)
Space complexity: O(n)
class Solution {
public int[] deckRevealedIncreasing(int[] deck) {
int n = deck.length;
int[] cards = new int[n];
Deque<Integer> deq = new ArrayDeque<>();
for(int i = 0; i < n; ++i) {
deq.addLast(i);
} Arrays.sort(deck); for(int j = 0; j < n; ++j){
int i = deq.removeFirst();
cards[i] = deck[j];
if(!deq.isEmpty()) {
deq.addLast(deq.removeFirst());
}
} return cards;
}
}
Reveal Cards In Increasing Order LT950的更多相关文章
- [Swift]LeetCode950. 按递增顺序显示卡牌 | Reveal Cards In Increasing Order
In a deck of cards, every card has a unique integer. You can order the deck in any order you want. ...
- [Solution] 950. Reveal Cards In Increasing Order
Difficulty: Medium Problem In a deck of cards, every card has a unique integer. You can order the de ...
- 113th LeetCode Weekly Contest Reveal Cards In Increasing Order
In a deck of cards, every card has a unique integer. You can order the deck in any order you want. ...
- 【leedcode】950. Reveal Cards In Increasing Order
题目如下: In a deck of cards, every card has a unique integer. You can order the deck in any order you ...
- 【LeetCode】950. Reveal Cards In Increasing Order 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 模拟 日期 题目地址:https://leetcod ...
- 揭示牌面使之升序 Reveal Cards In Increasing Order
2019-03-27 14:10:37 问题描述: 问题求解: 模拟题.考虑角度是从结果来进行反推. input - [2,3,5,7,11,13,17] (just sort the input t ...
- Leetcode950. Reveal Cards In Increasing Order按递增顺序显示卡牌
牌组中的每张卡牌都对应有一个唯一的整数.你可以按你想要的顺序对这套卡片进行排序. 最初,这些卡牌在牌组里是正面朝下的(即,未显示状态). 现在,重复执行以下步骤,直到显示所有卡牌为止: 从牌组顶部抽一 ...
- 【Leetcode_easy】897. Increasing Order Search Tree
problem 897. Increasing Order Search Tree 参考 1. Leetcode_easy_897. Increasing Order Search Tree; 完
- LeetCode 897. 递增顺序查找树(Increasing Order Search Tree)
897. 递增顺序查找树 897. Increasing Order Search Tree 题目描述 给定一个树,按中序遍历重新排列树,使树中最左边的结点现在是树的根,并且每个结点没有左子结点,只有 ...
随机推荐
- web 安全:
XSSXSS 全称“跨站脚本”,是注入攻击的一种. 其特点是不对服务器端造成任何伤害,而是通过一些正常的站内交互途径,例如发布评论,提交含有 JavaScript 的内容文本. 这时服务器端如果没有过 ...
- 慕课网c#开发轻松入门6-8最终项目
下面是一些同学的姓名和对应的考试分数,请输出他们的平均分和高于平均分的同学姓名. 运行效果如下: using System; using System.Collections.Generic; usi ...
- Java8 Base64
转自:https://www.runoob.com/java/java8-base64.html 在Java 8中,Base64编码已经成为Java类库的标准. Java 8 内置了 Base64 编 ...
- 深度学习原理与框架- tf.nn.conv2d_transpose(反卷积操作) tf.nn.conv2d_transpose(进行反卷积操作) 对于stride的理解存在问题?
反卷积操作: 首先对需要进行维度扩张的feature_map 进行补零操作,然后使用3*3的卷积核,进行卷积操作,使得其维度进行扩张,图中可以看出,2*2的feature经过卷积变成了4*4. ...
- 根据设备width(375)动态设置font-size
根据html的font-size使用rem来优化移动端页面 (function () { var w = window, d = document.documentElement, t; var re ...
- splash介绍及安装_mac
一.splash介绍 Splash是一个Javascript渲染服务.它是一个实现了HTTP API的轻量级浏览器,基于Python3和Twisted引擎,可以异步处理任务,并发性能好. 二.spla ...
- cxgrid主从表的从表数据小于主表总数的问题
当从表的数据移动需要和数据源记录同步时,会发生一现象:从表中设定的keyfield记录不连续,显示就会中断. 这样要设置从表的排序,按照主表的关联键在内存表里进行排序 indexFieldNames设 ...
- WPF 学习笔记
依赖属性(Dependency Property) 相比较于普通属性,依赖属性有以下特点: 变化通知(change notification) 节省内存 使用某种带优先级决定策略(resolution ...
- 布局inline-block问题
当在一行中需要展示多个拥有块级属性的标签元素时,通常选择display:inline-block; 优点:不用设置浮动或定位,浮动脱离文档流还需要清除浮动,定位降低扩展性. 问题: 1.标签元素之间会 ...
- docker搭建gitlab服务器(Centos7)
系统环境:CentOS Linux release 7.6.1810 (Core) git版本:gitlab/gitlab-ce 一.安装和启动docker 见HTTPRUNNERMANAGER安装部 ...