public class Solution {
public int[,] ReconstructQueue(int[,] people) {
if (people == null || people.Length == )
{
return new int[,] { };
} var row = people.GetLength();//二元组个数
var col = people.GetLength();// var dic = new Dictionary<int, List<int>>(); var ary = new int[row, col]; //将前面为0的“队头”确定
for (int i = ; i < row; i++)
{
var height = people[i, ];
var position = people[i, ]; if (!dic.ContainsKey(position))
{
var po = new List<int>();
po.Add(height);
dic.Add(position, po);
}
else
{
dic[position].Add(height);
}
} //先确定队头
var headlist = dic[].OrderBy(x => x).ToList();
for (int i = ; i < headlist.Count; i++)
{
ary[i, ] = headlist[i];
ary[i, ] = ;
}
//按照positon进行插入排序
var plist = dic.Keys.OrderBy(x => x).ToList(); var dtcount = dic[].Count;//队头的二元组数量 foreach (var p in plist)
{
if (p == )
{
continue;
}
var addlist = dic[p].OrderBy(x => x).ToList(); for (int i = ; i < addlist.Count; i++)//循环剩余的列表
{
var curheight = addlist[i];
var curposition = p; var cf = ;//队头中满足条件的数量
var inserted = false;//是否已经插入
for (int j = ; j < dtcount; j++)//循环队头,找到第一个不满足的位置
{
if (curheight <= ary[j, ])
{
cf++;//发现一个,比当前元素相等或更高的元素
if (cf > p)
{
//找到了不满足的情况,当前的j为插入的位置 //j以及j之后的元素都向后移动
for (int k = dtcount - ; k >= j; k--)
{
ary[k + , ] = ary[k, ];
ary[k + , ] = ary[k, ];
}
ary[j, ] = curheight;
ary[j, ] = curposition; inserted = true;
dtcount++;
break;
}
}
} if (!inserted)//没有遇到冲突的情况,插入末尾
{
ary[dtcount, ] = curheight;
ary[dtcount, ] = curposition;
dtcount++;
} } }
return ary;
}
}

https://leetcode.com/problems/queue-reconstruction-by-height/#/description

上面这个写的够长的了,用python,4行就可以实现:

 class Solution:
def reconstructQueue(self, people):
res = []
for i in sorted(people, key = lambda x: (-x[0],x[1])):
res.insert(i[1], i)
return res

先按照第一个元素倒序排,再按照第二个元素正序排,然后用insert方法,在指定的index上插入。

leetcode406的更多相关文章

  1. [Swift]LeetCode406. 根据身高重建队列 | Queue Reconstruction by Height

    Suppose you have a random list of people standing in a queue. Each person is described by a pair of ...

  2. LeetCode406. Queue Reconstruction by Height Add to List

    Description Suppose you have a random list of people standing in a queue. Each person is described b ...

  3. leetcode406 ,131,1091 python

    LeetCode 406. Queue Reconstruction by Height 解题报告题目描述Suppose you have a random list of people standi ...

  4. LeetCode406 queue-reconstruction-by-height详解

    题目详情 假设有打乱顺序的一群人站成一个队列. 每个人由一个整数对(h, k)表示,其中h是这个人的身高,k是排在这个人前面且身高大于或等于h的人数. 编写一个算法来重建这个队列. 注意: 总人数少于 ...

  5. LeetCode Weekly Contest 6

    leetcode现在每周末举办比赛,这是今天上午参加的比赛的题解.题目难度不算大,两个easy,一个medium,一个hard.hard题之前接触过,所以做得比较顺利. 1.  Sum of Left ...

随机推荐

  1. NOIP2012提高组 Day 2 Problem 2 借教室

    原题 题目描述 在大学期间,经常需要租借教室.大到院系举办活动,小到学习小组自习讨论,都需要向学校申请借教室.教室的大小功能不同,借教室人的身份不同,借教室的手续也不一样. 面对海量租借教室的信息,我 ...

  2. 蘑菇街支付架构 PDF 下载

    蘑菇街支付架构 PDF 下载 下载地址:链接:https://pan.baidu.com/s/1ZffetaUhVMOzb9j2PSQJIQ 密码:iays http://www.java1234.c ...

  3. python中calendar模块的常用方法

    >>> import calendar >>> calendar.isleap(2000) #判断是否是闰年 True >>> calendar. ...

  4. PTA——洗牌

    PTA 7-43 Shuffling Machine #include<stdio.h> int main() { int i,n,*result; scanf("%d" ...

  5. BIOS(Basic Input/Output System)是基本输入输出系统的简称

    BIOS(Basic Input/Output System)是基本输入输出系统的简称 介绍 操作系统老师说,平时面试学生或者毕业答辩的时候他都会问这个问题,可见这个问题对于计算机专业的学生来说是如此 ...

  6. UEFI+GPT双硬盘安装Win10+Ubuntu16.04双系统

    转载请注明出处:http://www.cnblogs.com/willnote/p/6725594.html 安装环境 SSD+HDD双盘,Win10安装在SSD里,HDD分出来60G安装Ubuntu ...

  7. mvc ajax访问后台时session过期无法跳转到Login页面问题解决

    public class BaseController : Controller { protected User UserInfo { set { Session["UserInfo&qu ...

  8. 用a标签实现submit提交按钮的效果

    今天做了一个小项目练手,要求点击a标签后实现post提交的效果,看到这个的时候心理还是有一丝丝懵逼的,不过在朕的十秒钟思考之后有了头绪... 首先表单 <form action="te ...

  9. [转].NET 性能测试工具 -- 事件跟踪器(ETW)

    .NET 性能测试工具 -- 事件跟踪器(ETW) 内容预告: Windows内置工具(性能计数器) 事件跟踪器(WPT,PerfMoniter,PerfView,自定义ETW) 时间分析 内存分配分 ...

  10. Python输入语句

    什么是输入 咱们在银行ATM机器前取钱时,肯定需要输入密码,对不? 那么怎样才能让程序知道咱们刚刚输入的是什么呢?? 大家应该知道了,如果要完成ATM机取钱这件事情,需要先从键盘中输入一个数据,然后用 ...