Problem Description
There is height information for a group of students. You need to specify one student among them
and arrange the line so that this student stands at the front.
In this case, students shorter than the specified student must stand behind, and
the order of students with the same height must remain unchanged.
Write an algorithm that satisfies these conditions for lining up the students.
Input Format
An array containing the heights of students (e.g., [160, 170, 165, 180, 175]) Height of the specified student (e.g., 170)
Output Format
An array of the lined-up students' heights (e.g., [170, 160, 165, 180, 175])
Problem Solving Process
Step 1: Understand the Problem
The crux of the problem is to place the specified height student at the front of the given array, while
sorting the remaining students by height, maintaining their original order. This problem can primarily be solved using stable sorting.
The algorithm we will implement includes the following steps.
Step 2: Analyze a Simple Example
[160, 170, 165, 180, 175]
and 170
,the lined-up result should be
[170, 160, 165, 180, 175]
. The key point to note isthat when multiple students have the same height, their order must be preserved.
Step 3: Develop a Solution Strategy
The solution method is as follows.
- Find the student with the specified height in the given array and add that student as the first element of the result array.
- Add the remaining students to the result array while maintaining their original order, excluding students with the same height.
- Finally, return the modified array.
Step 4: Implement JavaScript Code
Based on the above strategy, I will write a JavaScript function. This function will take two parameters and
serve to move the specified height student to the front.
function lineUpStudents(students, targetHeight) { // Declare an array to store the result let result = []; // First, add the student corresponding to targetHeight const targetStudents = students.filter(height => height === targetHeight); result.push(...targetStudents); // Add the remaining students (maintaining original order) const otherStudents = students.filter(height => height !== targetHeight); result.push(...otherStudents); return result; }
Step 5: Test and Validate the Code
I will run a few test cases to confirm that the function works correctly.
console.log(lineUpStudents([160, 170, 165, 180, 175], 170)); // [170, 160, 165, 180, 175] console.log(lineUpStudents([160, 160, 165, 170, 180], 160)); // [160, 160, 165, 170, 180] console.log(lineUpStudents([180, 170, 160, 150], 160)); // [160, 180, 170, 150]
Step 6: Complexity Analysis
The time complexity of this algorithm is O(n). This is because we iterate through the given array once.
The space complexity is also O(n) since a separate result array is created.
Conclusion
In this tutorial, we learned how to solve the problem of lining up students based on their height information
using JavaScript.
Maintaining a stable sort by height was the key to this problem.
Such problems are very important as they frequently appear in coding tests.
Practicing various variations of this problem can also be good preparation for developing algorithmic thinking.