题目给我的提示,是贪心算法,我也用贪心写出来了,可惜会报错
//假设有打乱顺序的一群人站成一个队列。 每个人由一个整数对(h, k)表示,其中h是这个人的身高,k是排在这个人前面且身高大于或等于h的人数。 编写一个算法来//重建这个队列。//// 注意://总人数少于1100人。//// 示例//////输入://[[7,0], [4,4], [7,1], [5,0], [6,1], [5,2]]////输出://[[5,0], [7,0], [5,2], [6,1], [4,4], [7,1]]//// Related Topics 贪心算法// 👍 610 👎 0import java.util.Arrays;import java.util.Comparator;//leetcode submit region begin(Prohibit modification and deletion)class Solution {int[][] finalResult;public int[][] reconstructQueue(int[][] people) {if (people.length == 0) {return new int[0][0];}buildIndexI(0, people);return finalResult;}public void buildIndexI(int j, int[][] people) {boolean flag = false;for (int i = j; i < people.length; i++) {if(people[i][1]>j){continue;}int height = people[i][0];int count = 0;// 计算数量for (int m = 0; m <= j - 1; m++) {if (people[m][0] >= height) {count++;}}if (count == people[i][1]) {if (j == people.length - 1 && i == j) {finalResult = Arrays.copyOf(people, people.length);return;}swap(j, i, people);buildIndexI(j + 1, people);swap(i, j, people);flag = true;}}if (!flag) {return;}}public void swap(int i, int j, int[][] array) {int[] temp = array[i];array[i] = array[j];array[j] = temp;}}//leetcode submit region end(Prohibit modification and deletion)
