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. eclipse中创建DataBase Connections

    1.window --> show view --> other --> Data Management --> Data Exploerer --> ok: 2.右键单 ...

  2. bfs两种记录路径方法

    #include<cstdio> #include<queue> using namespace std; struct sss { int x,y; }ans[][]; ][ ...

  3. cut语法2

    linux每日一命令--cut--按文件大小排序 显示前100行 显示后五列 ll -Sh|head -n 100|cut -d ' ' -f 5- 一.基本语法cut是一个选取命令,以行为单位,用指 ...

  4. SpringBoot(五)Springmvc+Mybatis+mysql

    为了整合这个花了,好长时间,因为上几个连在一起,在pom文件中有没注释的配置,导致我找这个问题找了好久.下面开始 1,先new project 时选择spring initializr,新建好一个空项 ...

  5. 剑指Offer 40. 数组中只出现一次的数字 (数组)

    题目描述 一个整型数组里除了两个数字之外,其他的数字都出现了偶数次.请写程序找出这两个只出现一次的数字. 题目地址 https://www.nowcoder.com/practice/e02fdb54 ...

  6. 剑指Offer 48. 不用加减乘除做加法 (其他)

    题目描述 写一个函数,求两个整数之和,要求在函数体内不得使用+.-.*./四则运算符号. 题目地址 https://www.nowcoder.com/practice/59ac416b4b944300 ...

  7. Canvas名侦探柯南-canvas练习

    var canvas=document.getElementById("canvas"); var ctx=canvas.getContext("2d"); / ...

  8. 【leetcode】455. Assign Cookies

    problem 455. Assign Cookies solution1: But, you should give each child at most one cookie. 对小朋友的满意程度 ...

  9. Idea基于maven搭建多模块单体结构项目

    菜鸟教程 一.创建一个公共的父工程: 1.1 创建一个新的maven项目: 1.2 先创建公共的父工程,选择maven骨架: 1.3 配置maven: 1.4 填写groupId.artifactId ...

  10. varchar字数

    每行数据最多65000字节 长度是当前字符集的字符长度,而不是字节长度! 参考:https://www.cnblogs.com/billyxp/p/3548540.html 经常变化的字段用varch ...