leetcode406
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的更多相关文章
- [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 ...
- 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 ...
- leetcode406 ,131,1091 python
LeetCode 406. Queue Reconstruction by Height 解题报告题目描述Suppose you have a random list of people standi ...
- LeetCode406 queue-reconstruction-by-height详解
题目详情 假设有打乱顺序的一群人站成一个队列. 每个人由一个整数对(h, k)表示,其中h是这个人的身高,k是排在这个人前面且身高大于或等于h的人数. 编写一个算法来重建这个队列. 注意: 总人数少于 ...
- LeetCode Weekly Contest 6
leetcode现在每周末举办比赛,这是今天上午参加的比赛的题解.题目难度不算大,两个easy,一个medium,一个hard.hard题之前接触过,所以做得比较顺利. 1. Sum of Left ...
随机推荐
- 关于maven环境变量的配置问题
开始使用“MAVEN_HOME”配置完环境变量后,在cmd中输入mvn -v提示不是内部命令,后直接在PATH 路径里面添加maven所在的位置+\bin,比如,maven的路径为E:\maven\a ...
- disjoint set
MAKE-SET.x/ creates a new set whose only member (and thus representative) is x. Since the sets are d ...
- 将100道计算题输出至txt文件,再读取文件至控制台,在控制台中输入答案并评判对错
我在课堂上基本完成了输出100道题和创建文档,但是因为对输入输出流不熟悉,所以并没有实现将输出的计算题导出到文档里,在课下我又请教了宿舍的大佬,基本完成如下: 源代码: import java.io. ...
- Beta版——爱阅APP功能说明书
爱阅APP功能说明书 一.引言 通过Alpha发布和一些用户的反馈信息,了解到我们APP存在的问题.针对这些问题我们做了一些修改.以下内容是Beta版的功能说明书. 二.工具 安卓手机 爱阅APP安装 ...
- OpenCV中的图像插值示例
本文地址:http://www.cnblogs.com/QingHuan/p/7384433.html,转载请注明出处 ======================================== ...
- [LeetCode&Python] Problem 744. Find Smallest Letter Greater Than Target
Given a list of sorted characters letters containing only lowercase letters, and given a target lett ...
- IDEA将web项目打成war包
非Maven项目打包方式 按ctrl+滚动鼠标滚轮就能放大浏览器看图片了.或者将图片拖到新窗口打开. 然后点击OK,然后build一下.然后去classes文件夹下面找就能找到打成war包的项目 ...
- https加载非https资源时不出现问题
老规矩,国服第一博客copy王,原文链接:https://blog.csdn.net/zhengbingmei/article/details/81325325将系统变成了https访问之后,发现部分 ...
- 产生10个随机数5-9之间 统计一个int类型的一维数组中有多少个在[min,max]之间的数
* 产生10个随机数5-9之间 统计一个int类型的一维数组中有多少个在[min,max]之间的数 */ import java.util.*; public class Demo{ public s ...
- IIS 负载均衡
在大型Web应用系统中,由于请求的数据量过大以及并发的因素,导致Web系统会出现宕机的现象,解决这一类问题的方法我个人觉得主要在以下几个方面: 1.IIS 负载均衡. 2.数据库 负载均衡. 3.系统 ...