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 题目描述 给定一个树,按中序遍历重新排列树,使树中最左边的结点现在是树的根,并且每个结点没有左子结点,只有 ...
随机推荐
- 为什么你需要少看垃圾博客以及如何在Python里精确地四舍五入
今天又有一个Python初学者被中文技术博客中的垃圾文章给误导了. 这位初学者的问题是: 在Python中,如何精确地进行浮点数的四舍五入,保留两位小数? 如果你在Google或者百度上搜索,你会发现 ...
- NodeJs 使用 multer 实现文件上传
Multer 是一个 node.js 中间件,用于处理 multipart/form-data 类型的表单数据,它主要用于上传文件 注意: Multer 不会处理任何非 multipart/form- ...
- springboot security
Authority 权限Credential 证书Grant 授予 Authentication 身份验证 以下,我们将通过四步,逐步实现spring-security的userna ...
- mysql--构造数据、导入导出
一.mysql造数据脚本 DELIMITER $$ DROP PROCEDURE IF EXISTS `test3`$$ CREATE /*[DEFINER = { user | CU ...
- XproerIM2-更新-2017-6-28
资源下载:源代码,开发文档,客户端,openfire-3.9.3.exe,openfire-4.1.4.exe, 开源库:cximage600-full,boost-1.55.0,pugixml-1. ...
- 微信小程序 app.json文件配置
https://developers.weixin.qq.com/miniprogram/dev/index.html 起步 https://developers.weixin.qq.com/min ...
- destructuring
解构(结构化赋值): 解构过程中,具备赋值和变量声明两个功能 目的在于把等号左右长的相似的两个东西内部的值取出来. 对象数组都可以参与解构: let obj = {name: ‘duyi’,age: ...
- mongodb安装使用简单命令
.window+x,A,管理员进入cmd.cd C:\Program Files\MongoDB\Server\3.4\bin.安装:mongod --dbpath "D:\work\DB\ ...
- 使用其他分支替换master分支
在提交混乱的时候, 导致master分支和远程仓库完全一致的时候,这时候解决这种问题可以创建一个新的分支, 再合并到master分支, 像这样: git checkout seotweaks git ...
- Alfred 3 如何设置默认搜索引擎(以百度搜索为例)
前提条件: Alfred 3 已经安装在MAC中 首先要有破解的Alfred 3 安装在MAC PRO中,然后进入到以下栏目:Features-->web Search-->Add Cus ...