首页 > 其他分享 >950. Reveal Cards In Increasing Order (Medium)

950. Reveal Cards In Increasing Order (Medium)

时间:2023-07-20 10:47:33浏览次数:50  
标签:11 Reveal 13 Medium 17 deck 950 dq reveal

Description

950. Reveal Cards In Increasing Order (Medium)

You are given an integer array deck. There is a deck of cards where every card has a unique integer. The integer on the ith card is deck[i].

You can order the deck in any order you want. Initially, all the cards start face down (unrevealed) in one deck.

You will 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 then 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.

Note that the first entry in the answer is considered to be the top of the deck.

 

Example 1:

Input: deck = [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 does not 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.

Example 2:

Input: deck = [1,1000]
Output: [1,1000]

 

Constraints:

  • 1 <= deck.length <= 1000
  • 1 <= deck[i] <= 106
  • All the values of deck are unique.

Solution

This problem can be solved through direct simulation according to the given instructions. The use of recursion is not necessary. Since the problem involves popping the first element, a double-ended queue (deque) is a suitable data structure to use. The deque will store the indices of elements, and we can proceed with the simulation accordingly.

Code

class Solution {
public:
    vector<int> deckRevealedIncreasing(vector<int>& deck) {
    	sort(deck.begin(), deck.end());
    	int n = deck.size();
    	vector<int> res(n);
    	deque<int> dq;
    	for (int i = 0; i < n; ++i) {
    		dq.push_back(i);
    	}
    	int cnt = 0;
    	for (int card : deck) {
    		res[dq.front()] = card;
    		dq.pop_front();
    		if (!dq.empty()) {
    			dq.push_back(dq.front());
    			dq.pop_front();
    		}
    	}
    	return res;
    }
};

标签:11,Reveal,13,Medium,17,deck,950,dq,reveal
From: https://www.cnblogs.com/zwyyy456/p/17567674.html

相关文章

  • 950. 按递增顺序显示卡牌 (Medium)
    问题描述950.按递增顺序显示卡牌(Medium)牌组中的每张卡牌都对应有一个唯一的整数。你可以按你想要的顺序对这套卡片进行排序。最初,这些卡牌在牌组里是正面朝下的(即,未显示状态)。现在,重复执行以下步骤,直到显示所有卡牌为止:从牌组顶部抽一张牌,显示它,然后将其从牌组中移出。......
  • [Typescript] 149 Medium - Triangular number
    GivenanumberN,findtheNthtriangularnumber,i.e. 1+2+3+...+N/*_____________YourCodeHere_____________*/exporttypeNumberToArray<Textendsnumber,Rextends1[]=[]>=R["length"]extendsT?R:NumberToArray&......
  • [Typescript Challenge] 148 Medium - CartesianProduct
    Given2sets(unions),returnitsCartesianproductinasetoftuples,e.g.CartesianProduct<1|2,'a'|'b'>//[1,'a']|[2,'a']|[1,'b']|[2,'b'] Solution:/*____________......
  • 安装NET3.5提示0x800f0950失败解决方法(不用重装系统)
    1、windows搜索框搜索“系统信息”,找到系统的版本号;2、下载系统对应的iso镜像。windows系统历史版本下载地址:NEXT,ITELLYOU3、下载好iso文件之后右键--打开方式--WinRAR压缩文件管理器--打开,找到子目录中的sources里面的sxs文件夹,拷贝到C:\Windows\System32下;3、windows搜索......
  • iOS开发笔记 - 界面调试神器Reveal
    Reveal是iOS开发工具中的神器之一,它能够在应用程序运行过程中调试应用程序界面。通过Reveal我们可以连接到应用程序,并允许开发者编辑各种用户界面参数,结果会马上在用户界面上呈现。就像Web开发人员用浏览器提供的开发人员工具调试页面一样,Reveal允许开发者在不修改代码、不重新构......
  • 2712. Minimum Cost to Make All Characters Equal (Medium)
    Description2712.MinimumCosttoMakeAllCharactersEqual(Medium)Youaregivena0-indexedbinarystringsoflengthnonwhichyoucanapplytwotypesofoperations:Chooseanindexiandinvertallcharactersfromindex0toindexi(bothinclusive......
  • 1020.飞地的数量 (Medium)
    问题描述1020.飞地的数量(Medium)给你一个大小为mxn的二进制矩阵grid,其中0表示一个海洋单元格、1表示一个陆地单元格。一次移动是指从一个陆地单元格走到另一个相邻(上、下、左、右)的陆地单元格或跨过grid的边界。返回网格中无法在任意次数的移动中离开网格......
  • 442.数组中重复的数据 (Medium)
    问题描述442.数组中重复的数据(Medium)给你一个长度为n的整数数组nums,其中nums的所有整数都在范围[1,n]内,且每个整数出现一次或两次。请你找出所有出现两次的整数,并以数组形式返回。你必须设计并实现一个时间复杂度为O(n)且仅使用常量额外空间的算法解决此......
  • 面试题 17.05. 字母与数字 (Medium)
    问题描述面试题17.05.字母与数字(Medium)给定一个放有字母和数字的数组,找到最长的子数组,且包含的字母和数字的个数相同。返回该子数组,若存在多个最长子数组,返回左端点下标值最小的子数组。若不存在这样的数组,返回一个空数组。示例1:输入:["A","1","B","C","D","2","3",......
  • 1631.最小体力消耗路径 (Medium)
    问题描述1631.最小体力消耗路径(Medium)你准备参加一场远足活动。给你一个二维rowsxcolumns的地图heights,其中heights[row][col]表示格子(row,col)的高度。一开始你在最左上角的格子(0,0),且你希望去最右下角的格子(rows-1,columns-1)(注意下标从0开始编号)。......