每日刷题:406. 根据身高重建队列

381 阅读2分钟

难度: 中等

假设有打乱顺序的一群人站成一个队列,数组 people 表示队列中一些人的属性(不一定按顺序)。每个 people[i] = [hi, ki] 表示第 i 个人的身高为 hi ,前面 正好 有 ki 个身高大于或等于 hi 的人。

请你重新构造并返回输入数组 people 所表示的队列。返回的队列应该格式化为数组 queue ,其中 queue[j] = [hj, kj] 是队列中第 j 个人的属性(queue[0] 是排在队列前面的人)。

解题思路

有两种方法:

  • 方法一: 高的优先排,高的前面可以随便插入低的,不影响其属性, 因此可以随意往前插入,高的则往后挪动;
  • 方法二: 低的优先排,因此高的随便排,见空插入即可。越往后越无所谓。

题解

public int[][] reconstructQueueTallerFirst(int[][] people) {
    Arrays.sort(people, (o1, o2) -> o1[0] == o2[0] ? o1[1] - o2[1] : o2[0] - o1[0]);
    List<int[]> reconstructionList = new ArrayList<>(people.length);
    for (int[] p : people) {
        reconstructionList.add(p[1], p);
    }
    return reconstructionList.toArray(new int[people.length][]);
}

public int[][] reconstructQueueShorterFirst(int[][] people) {
    Arrays.sort(people, (o1, o2) -> o1[0] == o2[0] ? o2[1] - o1[1] : o1[0] - o2[0]);
    int length = people.length;
    int[][] reQueue = new int[length][];
    for (int[] p : people) {
        int spaces = p[1] + 1;
        for (int i = 0; i < length; i++) {
            if (reQueue[i] != null) continue;
            --spaces;
            if (spaces != 0) continue;
            reQueue[i] = p;
            break;

        }
    }
    return reQueue;
}

测试

QueueReconstructionByHeight queueReconstructionByHeight = new QueueReconstructionByHeight();

@Test
public void taller_first_test_case1() {
    int[][] people = new int[][]{
            {7, 0},
            {4, 4},
            {7, 1},
            {5, 0},
            {6, 1},
            {5, 2},
    };
    int[][] expected = new int[][]{
            {5, 0},
            {7, 0},
            {5, 2},
            {6, 1},
            {4, 4},
            {7, 1},
    };
    Assertions.assertArrayEquals(expected, queueReconstructionByHeight.reconstructQueueTallerFirst(people));
}

@Test
public void taller_first_test_case2() {
    int[][] people = new int[][]{
            {6, 0},
            {5, 0},
            {4, 0},
            {3, 2},
            {2, 2},
            {1, 4},
    };
    int[][] expected = new int[][]{
            {4, 0},
            {5, 0},
            {2, 2},
            {3, 2},
            {1, 4},
            {6, 0},
    };
    Assertions.assertArrayEquals(expected, queueReconstructionByHeight.reconstructQueueTallerFirst(people));
}

@Test
public void shorter_first_test_case1() {
    int[][] people = new int[][]{
            {7, 0},
            {4, 4},
            {7, 1},
            {5, 0},
            {6, 1},
            {5, 2},
    };
    int[][] expected = new int[][]{
            {5, 0},
            {7, 0},
            {5, 2},
            {6, 1},
            {4, 4},
            {7, 1},
    };
    Assertions.assertArrayEquals(expected, queueReconstructionByHeight.reconstructQueueShorterFirst(people));
}

@Test
public void shorter_first_test_case2() {
    int[][] people = new int[][]{
            {6, 0},
            {5, 0},
            {4, 0},
            {3, 2},
            {2, 2},
            {1, 4},
    };
    int[][] expected = new int[][]{
            {4, 0},
            {5, 0},
            {2, 2},
            {3, 2},
            {1, 4},
            {6, 0},
    };
    Assertions.assertArrayEquals(expected, queueReconstructionByHeight.reconstructQueueShorterFirst(people));
}