题目给我的提示,是贪心算法,我也用贪心写出来了,可惜会报错

    1. //假设有打乱顺序的一群人站成一个队列。 每个人由一个整数对(h, k)表示,其中h是这个人的身高,k是排在这个人前面且身高大于或等于h的人数。 编写一个算法来
    2. //重建这个队列。
    3. //
    4. // 注意:
    5. //总人数少于1100人。
    6. //
    7. // 示例
    8. //
    9. //
    10. //输入:
    11. //[[7,0], [4,4], [7,1], [5,0], [6,1], [5,2]]
    12. //
    13. //输出:
    14. //[[5,0], [7,0], [5,2], [6,1], [4,4], [7,1]]
    15. //
    16. // Related Topics 贪心算法
    17. // 👍 610 👎 0
    18. import java.util.Arrays;
    19. import java.util.Comparator;
    20. //leetcode submit region begin(Prohibit modification and deletion)
    21. class Solution {
    22. int[][] finalResult;
    23. public int[][] reconstructQueue(int[][] people) {
    24. if (people.length == 0) {
    25. return new int[0][0];
    26. }
    27. buildIndexI(0, people);
    28. return finalResult;
    29. }
    30. public void buildIndexI(int j, int[][] people) {
    31. boolean flag = false;
    32. for (int i = j; i < people.length; i++) {
    33. if(people[i][1]>j){
    34. continue;
    35. }
    36. int height = people[i][0];
    37. int count = 0;
    38. // 计算数量
    39. for (int m = 0; m <= j - 1; m++) {
    40. if (people[m][0] >= height) {
    41. count++;
    42. }
    43. }
    44. if (count == people[i][1]) {
    45. if (j == people.length - 1 && i == j) {
    46. finalResult = Arrays.copyOf(people, people.length);
    47. return;
    48. }
    49. swap(j, i, people);
    50. buildIndexI(j + 1, people);
    51. swap(i, j, people);
    52. flag = true;
    53. }
    54. }
    55. if (!flag) {
    56. return;
    57. }
    58. }
    59. public void swap(int i, int j, int[][] array) {
    60. int[] temp = array[i];
    61. array[i] = array[j];
    62. array[j] = temp;
    63. }
    64. }
    65. //leetcode submit region end(Prohibit modification and deletion)