Leetcode406

给了每个人的身高和前面>=他身高的人的个数。输出他们是怎么排的。


按照身高降序,人数升序排。

可以立即得到他们的排列。。。。


package com.leet;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.PriorityQueue;

/**
 * Created by dezhonger on 2016/10/20.
 */
//http://www.cnblogs.com/reboot329/p/5908130.html
public class Solution {

    public class People {
        public int h;
        public int k;
        public int i;   //index of this people in people[][]

        public People(int h, int k, int i) {
            this.h = h;
            this.k = k;
            this.i = i;
        }
    }
    public class Compare implements Comparator<People>
    {
        public int compare(People p1, People p2)
        {
            if(p1.h!=p2.h) return p2.h-p1.h;
            else return p1.k-p2.k;
        }
    }

    public int[][] reconstructQueue(int[][] people) {
        if (people.length == 0) return new int[0][0];

        PriorityQueue<People> pq1 = new PriorityQueue(new Compare());
        for (int i = 0; i < people.length; i++) {
            pq1.add(new People(people[i][0], people[i][1], i));
        }

        List<Integer> list = new ArrayList<Integer>();

        while (!pq1.isEmpty()) {
            People p = pq1.poll();
            list.add(p.k, p.i); //p.k表示前面有几个人比它高,所以要插入在第k个位置

        }
        int[][] res = new int[people.length][2];
        for (int i = 0; i < list.size(); i++) {
            res[i][0] = people[list.get(i)][0];
            res[i][1] = people[list.get(i)][1];
        }

        return res;

    }

    public static void main(String[] args) {
        int [][] a= new int[6][2];
        a[0] = new int[]{7, 0};
        a[1] = new int[]{4, 4};
        a[2] = new int[]{7, 1};
        a[3] = new int[]{5, 0};
        a[4] = new int[]{6, 1};
        a[5] = new int[]{5, 2};
        new Solution().reconstructQueue(a);
    }

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值