资讯专栏INFORMATION COLUMN

leetcode406. Queue Reconstruction by Height

morgan / 2341人阅读

摘要:题目要求假设有一组人站成一堆,每个人都记录下了自己的高度,以及在自己前面有多少个不比自己矮的人。现在请按照这个信息将这组人放在队列中正确的位置上并返回。但是这样解决其实复杂化了问题。即越大,则该同等高度的人一定在另一个同等高度的人后面。

题目要求
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]]

假设有一组人站成一堆,每个人都记录下了自己的高度,以及在自己前面有多少个不比自己矮的人。现在请按照这个信息将这组人放在队列中正确的位置上并返回。

思路和代码

刚开始我试图用分治法来解决,因为每一个小队列中,高度最矮且站在自己前面的高个最少的人一定位于k位置上。但是这样解决其实复杂化了问题。

可以从另一个角度来想,首先我们知道,同等高度的人,其相对位置一定是根据k的值从小到大排列的。即k越大,则该同等高度的人一定在另一个同等高度的人后面。如果我们从高到低将人们插入队列中,可以知道,k的值就该人在当前队列中的下标,如:

[[7,0], [4,4], [7,1], [5,0], [6,1], [5,2]]
首先将其按照h和k排序,得出结果:
[[7,0],[7,1],[6,1],[5,1],[5,0],[5,2],[4,4]]

当前结果队列为[]
将[7,0]插入下标为0的位置上 结果队列[[7,0]]
将[7,1]插入下标为1的位置上 结果队列[[7,0],[7,1]]
将[6,1]插入下标为1的位置上 结果队列[[7,0],[6,1],[7,1]]
按照这种规则,依次按照顺序和k的值将数据插入结果队列中

代码如下:

    public int[][] reconstructQueue(int[][] people) {
        int[][] result = new int[people.length][2];
        Arrays.sort(people, new Comparator() {

            @Override
            public int compare(int[] o1, int[] o2) {
                return o1[0] == o2[0] ? o1[1] - o2[1] : o2[0] - o1[0];
            }
            
        });
        for(int i = 0 ; i pos; j--) {
                result[j] = result[j - 1];
            }    
            result[pos] = people[i];
        }
        return result;
    }

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/73653.html

相关文章

  • Queue Reconstruction by Height

    摘要:题目链接的题感觉思路好难写啊。一直重复到结束。第一遍第二遍第三遍每次最前面的减的是最多的,所以考虑倒过来做这样减的时候就是碰到之后先减,碰到了,之后是减。又由于减到的时候要倒过来到结果里面,实际上就是把小的查到前面对应位置的过程。 Queue Reconstruction by Height 题目链接:https://leetcode.com/problems... greedy的题感...

    melody_lql 评论0 收藏0
  • leetcode102. Binary Tree Level Order Traversal

    摘要:题目要求对于一棵树进行序遍历。水平遍历即遍历结束当前行以后再遍历下一行,并将每行的结果按行填入到数组中返回。利用水平遍历的话,我们只需要知道当前元素在树中的高度就可以知道应当插入到那个数组中。 题目要求 Given a binary tree, return the level order traversal of its nodes values. (ie, from left to...

    Coding01 评论0 收藏0
  • [LeetCode] 490. The Maze (BFS/DFS)

    Problem There is a ball in a maze with empty spaces and walls. The ball can go through empty spaces by rolling up, down, left or right, but it wont stop rolling until hitting a wall. When the ball sto...

    smartlion 评论0 收藏0
  • [LeetCode] Reconstruct Itinerary

    摘要:来自大神的解答,只能膜拜。题目确定了至少有一条的行程不存在分支情况,一定有相同的最终目的地,而且对于多条的行程,要选取字母顺序较小的一条。 Problem Given a list of airline tickets represented by pairs of departure and arrival airports [from, to], reconstruct the i...

    jubincn 评论0 收藏0
  • [Leetcode] The Skyline Problem 天际线问题

    摘要:遍历时,通过一个堆来得知当前图形的最高位置。堆顶是所有顶点中最高的点,只要这个点没被移出堆,说明这个最高的矩形还没结束。对于左顶点,我们将其加入堆中。 The Skyline Problem A citys skyline is the outer contour of the silhouette formed by all the buildings in that city w...

    hidogs 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<