Intermediate Level

Implement the reconstructQueueCount method that returns the number of people after reconstructing the queue.

The input contains people represented as [height, k]. The value k means how many people with height greater than or equal to this person must stand before them.

Your task is to reconstruct the queue according to this rule and return the number of people placed in the final queue.

For example, after arranging [[7,0],[4,4],[7,1],[5,0],[6,1],[5,2]], all six people are placed correctly, so the answer is 6.

Example 1

Input
people (int[][]) = [[7,0],[4,4],[7,1],[5,0],[6,1],[5,2]]
size (int) = 6
Returns (int) 6

Example 2

Input
people (int[][]) = [[6,0],[5,0],[4,0]]
size (int) = 3
Returns (int) 3

Example 3

Input
people (int[][]) = []
size (int) = 0
Returns (int) 0

Sort taller people before shorter people. If heights are equal, sort by smaller k first.

Then insert each person at index k in the queue. This works because when a person is inserted, all people already in the queue are taller or equal, so inserting at position k creates exactly k taller-or-equal people before them.

After all insertions, return the queue size.

Pseudocode:

function reconstructQueueCount(people, size):
    sort people by height descending
    if heights are equal, sort by k ascending
    queue = empty list
    for each person in people:
        insert person at index person[1] in queue
    return length(queue)
Ready to test your solutionRun Code checks your solution against the test cases.Shift + Enter to run

Notifications

Updates are enabled for this browser.

Receive new tutorials, articles and learning resources. You can turn updates off anytime.

Test browser code

Use this code in admin to send a test notification to this browser.