Leetcode_406 Queue Reconstruction by Height

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/m0_37567543/article/details/78801751
闲来无事,又做了一道题,写一篇博客吧。

题目:
Suppose you have a random list of people standing in a queue. Each person is described by a pair of integers (h, k), where h is the height of the person and k is the number of people in front of this person who have a height greater than or equal to h. Write an algorithm to reconstruct the queue. Note: The number of people is less than 1,100. Example
Input:
[[7,0], [4,4], [7,1], [5,0], [6,1], [5,2]]

Output:
[[5,0], [7,0], [5,2], [6,1], [4,4], [7,1]]

这道题可以先根据数值对的第一个值进行降序排序,如果两个值相等的话,根据数值对的第二个值进行升序排序,然后再遍历一遍数组,根据数值对的第二个元素插入到返回的数组中即可。
代码:
运行时间截图:


查看原文:http://www.xuchenbjtu.cn/index.php/2017/12/14/leetcode_406-queue-reconstruction-by-height/
展开阅读全文

Graph Reconstruction

08-27

Let there be a simple graph with N vertices but we just know the degree of each vertex. Is it possible to reconstruct the graph only by these information?nnA simple graph is an undirected graph that has no loops (edges connected at both ends to the same vertex) and no more than one edge between any two different vertices. The degree of a vertex is the number of edges that connect to it.nnInputnnThere are multiple cases. Each case contains two lines. The first line contains one integer N (2 ≤ N ≤ 100), the number of vertices in the graph. The second line conrains N integers in which the ith item is the degree of ith vertex and each degree is between 0 and N-1(inclusive).nOutputnnIf the graph can be uniquely determined by the vertex degree information, output "UNIQUE" in the first line. Then output the graph.nnIf there are two or more different graphs can induce the same degree for all vertices, output "MULTIPLE" in the first line. Then output two different graphs in the following lines to proof.nnIf the vertex degree sequence cannot deduced any graph, just output "IMPOSSIBLE".nnThe output format of graph is as follows:nnN Enu1 u2 ... uEnv1 v2 ... vEnWhere N is the number of vertices and E is the number of edges, and ui,vi is the ith edge the the graph. The order of edges and the order of vertices in the edge representation is not important since we would use special judge to verify your answer. The number of each vertex is labeled from 1 to N. See sample output for more detail.nSample Inputnn1n0n6n5 5 5 4 4 3n6n5 4 4 4 4 3n6n3 4 3 1 2 0nSample OutputnnUNIQUEn1 0nnnUNIQUEn6 13n3 3 3 3 3 2 2 2 2 1 1 1 5n2 1 5 4 6 1 5 4 6 5 4 6 4nMULTIPLEn6 12n1 1 1 1 1 5 5 5 6 6 2 2n5 4 3 2 6 4 3 2 4 3 4 3n6 12n1 1 1 1 1 5 5 5 6 6 3 3n5 4 3 2 6 4 3 2 4 2 4 2nIMPOSSIBLE 问答

没有更多推荐了,返回首页