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 ...
随机推荐
- ubuntu 升级 python3.5到 python3.6
首先是在Ubuntu中安装python3.6 sudo apt-get install software-properties-common sudo add-apt-repository ppa:j ...
- 由一次报错引发的对于Spring创建对象的理解
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'ent ...
- jQuery-2.DOM---创建节点及节点属性
DOM创建节点及节点属性 通过JavaScript可以很方便的获取DOM节点,从而进行一系列的DOM操作.但实际上一般开发者都习惯性的先定义好HTML结构,但这样就非常不灵活了. 试想下这样的情况:如 ...
- 《Java编程思想》读书笔记-对象导论
计算机是头脑延伸的工具,是一种不同类型的表达媒体.本文以背景性的和补充性的材料,介绍包括开发方法概述在内的面向对象程序设计(Object-oriented Programming,OOP)的基本概念. ...
- jsonify
在flask中通过响应,将json数据显示在网页中,并且将Content-Type该为application/json格式 1,第一种方法: from flask import jsonify @ap ...
- BZOJ1800:fly 飞行棋 (双指针 组合数)
pro: 给出圆周上的若干个点,已知点与点之间的弧长,其值均为正整数,并依圆周顺序排列. 请找出这些点中有没有可以围成矩形的,并希望在最短时间内找出所有不重复矩形. N<20; sol:很可能被 ...
- 《从Lucene到Elasticsearch:全文检索实战》学习笔记一
今天,我主要给大家讲一下信息检索概念. 信息检索: 互联网时代的飞速发展使人们进入了信息爆炸时代,据统计全球的互联网用户已达到30亿,在各个网站及移动app在每个分钟 产生的数据量是巨大的,从而导致数 ...
- 一台服务器绑定多个ip地址
一台服务器绑定多个ip地址. 方法一: 使用标准的网络配置工具(比如ifconfig和route命令)添加lP别名: 在eth0网卡再绑定一个ip:192.168.101.103 /sbin/ifco ...
- best practices for designing web api
restful why: meaningful This will be improve efficiency , less documents , just read the code auto g ...
- windows server 2008 R2之取消多余的安全配置
一:取消IE浏览器的安全配置(使IE浏览器可以正常上网) 管理员禁用即可 二.取消关机时强制输入关机备注 运行gpedit.msc,选择计算机配置->管理模板->系统->提示“关机时 ...