1. public class Solution {
  2. public int[,] ReconstructQueue(int[,] people) {
  3. if (people == null || people.Length == )
  4. {
  5. return new int[,] { };
  6. }
  7.  
  8. var row = people.GetLength();//二元组个数
  9. var col = people.GetLength();//
  10.  
  11. var dic = new Dictionary<int, List<int>>();
  12.  
  13. var ary = new int[row, col];
  14.  
  15. //将前面为0的“队头”确定
  16. for (int i = ; i < row; i++)
  17. {
  18. var height = people[i, ];
  19. var position = people[i, ];
  20.  
  21. if (!dic.ContainsKey(position))
  22. {
  23. var po = new List<int>();
  24. po.Add(height);
  25. dic.Add(position, po);
  26. }
  27. else
  28. {
  29. dic[position].Add(height);
  30. }
  31. }
  32.  
  33. //先确定队头
  34. var headlist = dic[].OrderBy(x => x).ToList();
  35. for (int i = ; i < headlist.Count; i++)
  36. {
  37. ary[i, ] = headlist[i];
  38. ary[i, ] = ;
  39. }
  40. //按照positon进行插入排序
  41. var plist = dic.Keys.OrderBy(x => x).ToList();
  42.  
  43. var dtcount = dic[].Count;//队头的二元组数量
  44.  
  45. foreach (var p in plist)
  46. {
  47. if (p == )
  48. {
  49. continue;
  50. }
  51. var addlist = dic[p].OrderBy(x => x).ToList();
  52.  
  53. for (int i = ; i < addlist.Count; i++)//循环剩余的列表
  54. {
  55. var curheight = addlist[i];
  56. var curposition = p;
  57.  
  58. var cf = ;//队头中满足条件的数量
  59. var inserted = false;//是否已经插入
  60. for (int j = ; j < dtcount; j++)//循环队头,找到第一个不满足的位置
  61. {
  62. if (curheight <= ary[j, ])
  63. {
  64. cf++;//发现一个,比当前元素相等或更高的元素
  65. if (cf > p)
  66. {
  67. //找到了不满足的情况,当前的j为插入的位置
  68.  
  69. //j以及j之后的元素都向后移动
  70. for (int k = dtcount - ; k >= j; k--)
  71. {
  72. ary[k + , ] = ary[k, ];
  73. ary[k + , ] = ary[k, ];
  74. }
  75. ary[j, ] = curheight;
  76. ary[j, ] = curposition;
  77.  
  78. inserted = true;
  79. dtcount++;
  80. break;
  81. }
  82. }
  83. }
  84.  
  85. if (!inserted)//没有遇到冲突的情况,插入末尾
  86. {
  87. ary[dtcount, ] = curheight;
  88. ary[dtcount, ] = curposition;
  89. dtcount++;
  90. }
  91.  
  92. }
  93.  
  94. }
  95. return ary;
  96. }
  97. }

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

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

  1. class Solution:
  2. def reconstructQueue(self, people):
  3. res = []
  4. for i in sorted(people, key = lambda x: (-x[0],x[1])):
  5. res.insert(i[1], i)
  6. 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. ubuntu 升级 python3.5到 python3.6

    首先是在Ubuntu中安装python3.6 sudo apt-get install software-properties-common sudo add-apt-repository ppa:j ...

  2. 由一次报错引发的对于Spring创建对象的理解

    org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'ent ...

  3. jQuery-2.DOM---创建节点及节点属性

    DOM创建节点及节点属性 通过JavaScript可以很方便的获取DOM节点,从而进行一系列的DOM操作.但实际上一般开发者都习惯性的先定义好HTML结构,但这样就非常不灵活了. 试想下这样的情况:如 ...

  4. 《Java编程思想》读书笔记-对象导论

    计算机是头脑延伸的工具,是一种不同类型的表达媒体.本文以背景性的和补充性的材料,介绍包括开发方法概述在内的面向对象程序设计(Object-oriented Programming,OOP)的基本概念. ...

  5. jsonify

    在flask中通过响应,将json数据显示在网页中,并且将Content-Type该为application/json格式 1,第一种方法: from flask import jsonify @ap ...

  6. BZOJ1800:fly 飞行棋 (双指针 组合数)

    pro: 给出圆周上的若干个点,已知点与点之间的弧长,其值均为正整数,并依圆周顺序排列. 请找出这些点中有没有可以围成矩形的,并希望在最短时间内找出所有不重复矩形. N<20; sol:很可能被 ...

  7. 《从Lucene到Elasticsearch:全文检索实战》学习笔记一

    今天,我主要给大家讲一下信息检索概念. 信息检索: 互联网时代的飞速发展使人们进入了信息爆炸时代,据统计全球的互联网用户已达到30亿,在各个网站及移动app在每个分钟 产生的数据量是巨大的,从而导致数 ...

  8. 一台服务器绑定多个ip地址

    一台服务器绑定多个ip地址. 方法一: 使用标准的网络配置工具(比如ifconfig和route命令)添加lP别名: 在eth0网卡再绑定一个ip:192.168.101.103 /sbin/ifco ...

  9. best practices for designing web api

    restful why: meaningful This will be improve efficiency , less documents , just read the code auto g ...

  10. windows server 2008 R2之取消多余的安全配置

    一:取消IE浏览器的安全配置(使IE浏览器可以正常上网) 管理员禁用即可 二.取消关机时强制输入关机备注 运行gpedit.msc,选择计算机配置->管理模板->系统->提示“关机时 ...