1051. Height Checker - Explanation

Problem Link



Prerequisites

Before attempting this problem, you should be comfortable with:

  • Sorting - Creating a sorted copy to compare against the original order
  • Counting Sort - Using frequency arrays for efficient sorting when values are constrained to a small range

1. Sorting

Intuition

The problem asks us to count how many students are standing in the wrong position compared to where they should be if arranged by height.
If we sort the array, we get the "expected" order.
By comparing each position in the original array with the sorted version, we can count mismatches.

Algorithm

  1. Create a copy of the heights array and sort it to get the expected order.
  2. Initialize a counter res to 0.
  3. Iterate through both arrays simultaneously.
  4. For each index, if the original height differs from the expected height, increment the counter.
  5. Return the count.
class Solution:
    def heightChecker(self, heights: List[int]) -> int:
        expected = sorted(heights)

        res = 0
        for i in range(len(heights)):
            if heights[i] != expected[i]:
                res += 1

        return res

Time & Space Complexity

  • Time complexity: O(nlogn)O(n \log n)
  • Space complexity: O(n)O(n)

2. Counting Sort

Intuition

Since heights are constrained to a small range (1 to 100), we can use counting sort for a more efficient solution.
Instead of sorting the entire array, we count how many times each height appears.
Then we reconstruct the expected sorted order by iterating through possible heights and adding each one according to its count.
Finally, we compare the original array with this expected array.

Algorithm

  1. Create a count array of size 101 (to cover heights 1 through 100).
  2. Count occurrences of each height in the original array.
  3. Build the expected array by iterating from height 1 to 100 and appending each height according to its count.
  4. Compare the original array with the expected array and count positions where they differ.
  5. Return the mismatch count.
class Solution:
    def heightChecker(self, heights: List[int]) -> int:
        count = [0] * 101
        for h in heights:
            count[h] += 1

        expected = []
        for h in range(1, 101):
            c = count[h]
            for _ in range(c):
                expected.append(h)

        res = 0
        for i in range(len(heights)):
            if heights[i] != expected[i]:
                res += 1

        return res

Time & Space Complexity

  • Time complexity: O(n+k)O(n + k)
  • Space complexity: O(n+k)O(n + k)

Where nn is the size of the input array, and kk is the range of numbers.


Common Pitfalls

Modifying the Original Array

When sorting the array to get the expected order, some developers accidentally sort the original array in place. This destroys the original order, making it impossible to compare positions. Always create a copy of the array before sorting, or use a method that returns a new sorted array without modifying the original.

Using Lexicographic Sort Instead of Numeric Sort

In languages like JavaScript, the default sort() method sorts elements as strings, not numbers. This means [1, 10, 2] would be sorted as [1, 10, 2] instead of [1, 2, 10]. Always provide a numeric comparator function (e.g., (a, b) => a - b) to ensure correct sorting behavior.