714259. Seating Arrangement

Easy

Here is a detailed Markdown blog post for the Seating Arrangement problem:

Seating Arrangement

Summary

The Seating Arrangement problem involves arranging students in a classroom based on their heights. Given an array of student heights, find a seating arrangement where no student is seated below someone taller than them.

This problem requires using arrays and a greedy algorithm to ensure that each student is seated at a height that is not lower than any student who is taller than them.

Detailed Explanation

To solve this problem, we will use a simple yet effective greedy algorithm. First, we initialize an empty array seatingArrangement to store the final seating arrangement of students.

Next, we iterate over the input array of student heights in ascending order (i.e., from shortest to tallest). For each student height, we check if it is taller than any previously seated student. If so, we add this student to the end of our seatingArrangement array.

Here's a step-by-step breakdown:

  1. Initialize an empty array seatingArrangement.
  2. Iterate over the input array of student heights in ascending order.
  3. For each student height:
    • Check if it is taller than any previously seated student (i.e., the last element in our seatingArrangement array).
    • If so, add this student to the end of our seatingArrangement array.

The time complexity of this algorithm is O(n log n) due to the sorting step, where n is the number of students. The space complexity is O(n), as we need to store the seating arrangement in an array.

Optimized Solutions

Java

View on GeeksforGeeks
import java.util.Arrays;

public class SeatingArrangement {
    public static String[] seatingArrangement(int[] heights) {
        Arrays.sort(heights);
        int maxTall = 0;
        String[] result = new String[heights.length];
        for (int i = 0; i < heights.length; i++) {
            if (heights[i] > maxTall) {
                maxTall = heights[i];
                result[i] = "Student " + (i+1);
            } else {
                result[i] = "Student " + (i+1) + " is seated below someone taller than them";
            }
        }
        return result;
    }
}

DevExCode

Master coding interviews with Leetcode, system design, TechBit, QuickLearn, DevTips, Tech Battles, and career services.

© 2026 DevExCode. All rights reserved.