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. c++中关于预编译头的设置问题

    在运行代码时会遇到缺少预编译pch.c 或者stadfx.h之类的, 这个时候,先查看有没有包含, 然后看一下预编译头设置中, 是否正确设置.

  2. 一分钟学会ConstraintLayout(转载)

    原文地址:https://www.v2ex.com/t/287863 最近更新了Android Studio,突然发现xml中的布局已经变成了ConstraintLayout,于是搜了一篇文章看一下 ...

  3. 开始一个django项目的流程

    1.明确开发站点的主题,(即此站点的作用), 确定站点的各种功能,需求. 2.优先设计数据库. 数据库的设计要合理,不能想当然的设计,最好能够以表格的形式展现出来,避免以后遗忘,也避免内容的重复. 3 ...

  4. 剑指Offer 47. 求1+2+3+...+n (其他)

    题目描述 求1+2+3+...+n,要求不能使用乘除法.for.while.if.else.switch.case等关键字及条件判断语句(A?B:C). 题目地址 https://www.nowcod ...

  5. Quartz动态修改数据库cronExpression(无须重启服务器即可更改定时时间)

    quartz通过动态设置配置文件确实可以实现与数据库的同步,但现实开发上线后我们基本是不会对配置文件等进行变动,因为重启一次服务器所需的成本太多. 这时,就需要我们仅仅修改数据库就能实现动态的更新定时 ...

  6. php之异常处理

    <?php declare(strict_types = 1); function demo(int $v):int{ return 1; } try{ demo("1"); ...

  7. C语言如何在可变参数函数中使用printf?

    我要将printf外面再包上一层:void fun(const char* fmt,...) {........printf(fmt,...);........}这种函数怎么写? 对比下printf与 ...

  8. Cocos2dx利用intersectsRect函数检测碰撞

    if (sp1->boundingBox().intersectsRect(sp2->boundingBox())) { pLabel->setString(“碰撞飞机爆炸”); } ...

  9. SQLI DUMB SERIES-7

    (1)查看PHP源代码 可以看见输入的id被一对单引号和两对圆括号包围 (2)根据提示:Dump into Outfile可知,此次攻击需要导出文件 (3)在第一关获取 @@datadir 读取数据库 ...

  10. 【java多线程】队列系统之说说队列Queue

    转载:http://benjaminwhx.com/2018/05/05/%E8%AF%B4%E8%AF%B4%E9%98%9F%E5%88%97Queue/ 1.简介 Queue(队列):一种特殊的 ...