Queue Reconstruction by Height in JavaScript


In this problem statement, our task is to write a function for queue reconstruction by height with the help of Javascript. So basically we have to arrange the given array data by height.

Understanding the problem statement

The problem statement says to write a function in Javascript with the help of we can arrange the queue as per the height. This will be done by queue reconstruction.

The Queue Reconstruction by height problem is done by arranging a queue of persons based on their height and the number of people in front of them who are taller or have the same height. Every person is represented by [h, n] in which h is the height of the person and m is the number of people in front of the person with a height greater than or equal to h.

The task is to sort the people in a way that the queue should be reconstructed with the help of their height and m values.The condition to arrange the heights should be: the tallest persons should be at the front of the queue and among persons of the same height, those with small value of k will be placed first.

Logic for the above problem

The given problem can be implemented by first sorting the given array by descending order of heights and if the height is same then arrange by ascending the value of k. So we can insert every person from the sorted persons array into the result array at the index specified by their k value with the help of splice method.

The resultant array will be containing the persons in the correct order as required.

Algorithm

Step 1 − Define a function to arrange the array values by height of the persons and name it reconstructQueue and pass an array of persons.

Step 2 − Inside the function sort the persons array by descending height, and if height is same, then by ascending m.

Step 3 − Now create a blank array for storing the result of the sorted queue.

Step 4 − Insert persons into the result array as per their m value. Use a for loop to iterate the persons array and with the help of splice method we can insert each person from the sorted persons array into the result array at the index specified by their m values.

Step 5 − Get the value of the result array and return its value to show the reconstructed queue.

Code for the algorithm

function reconstructQueue(persons) {
   // sort the persons array by descending height, and if height is same, then by ascending m
   persons.sort((a, b) => {
      if (a[0] === b[0]) {
         return a[1] - b[1];
      } else {
         return b[0] - a[0];
      }
   });
    
   let result = [];
   // insert persons into the result array according to their m value
   for (let i = 0; i < persons.length; i++) {
      result.splice(persons[i][1], 0, persons[i]);
   }
    
   return result;
}
const persons = [[7, 0],
   [4, 4],
   [7, 1],
   [5, 0],
   [6, 1],
   [5, 2]
];
const result = reconstructQueue(persons);
console.log(result);

Complexity

The time for the execution of the function reconstructQueue is O(n^2) in which n is the number of persons in the input array. This is because for every person in the array we have to insert them into the result array at a decided index with the help of splice method. The method has a worst case time complexity of O(n) as it involves shifting all the items after the specified index by one position to the right. So the overall time complexity will be O(n^2). The space complexity for the function is O(n) because we have created a new array to store the result with the final ordering of the persons.

Conclusion

The function we have created above sorts the given array of persons based on their height and m values and reconstructs a queue as per their order. The reconstructQueue function has time complexity of O(n^2) and space complexity of O(n).

Updated on: 18-May-2023

133 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements