Watch 10 video solutions for Height Checker, a easy level problem involving Array, Sorting, Counting Sort. This walkthrough by NeetCodeIO has 10,199 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
A school is trying to take an annual photo of all the students. The students are asked to stand in a single file line in non-decreasing order by height. Let this ordering be represented by the integer array expected where expected[i] is the expected height of the ith student in line.
You are given an integer array heights representing the current order that the students are standing in. Each heights[i] is the height of the ith student in line (0-indexed).
Return the number of indices where heights[i] != expected[i].
Example 1:
Input: heights = [1,1,4,2,1,3] Output: 3 Explanation: heights: [1,1,4,2,1,3] expected: [1,1,1,2,3,4] Indices 2, 4, and 5 do not match.
Example 2:
Input: heights = [5,1,2,3,4] Output: 5 Explanation: heights: [5,1,2,3,4] expected: [1,2,3,4,5] All indices do not match.
Example 3:
Input: heights = [1,2,3,4,5] Output: 0 Explanation: heights: [1,2,3,4,5] expected: [1,2,3,4,5] All indices match.
Constraints:
1 <= heights.length <= 1001 <= heights[i] <= 100Problem Overview: You receive an array heights representing student heights in the order they currently stand. Students should be arranged in non‑decreasing order. The task is to count how many indices contain a height different from the height that would appear in the correctly sorted order.
Approach 1: Sorting and Comparison (O(n log n) time, O(n) space)
The most direct solution is to compute what the correct ordering should look like. Create a copy of the heights array and sort it using a standard sorting algorithm. Then iterate through both arrays simultaneously and count how many indices differ. Each mismatch represents a student who is not in the position they would occupy in the sorted lineup. This approach is simple, easy to implement in any language, and works well because the problem only requires comparison against the sorted order rather than modifying the original array.
The key insight is that the sorted array represents the expected arrangement. By comparing index by index, you avoid complex rearrangement logic. The algorithm performs one sort operation and one linear pass through the array, leading to O(n log n) time complexity and O(n) additional space for the copy.
Approach 2: Counting Sort Optimization (O(n + k) time, O(k) space)
The constraints of the problem reveal that student heights fall within a small fixed range (1 to 100). Instead of sorting explicitly, you can use counting sort. Create a frequency array where index i stores how many students have height i. This structure lets you reconstruct the sorted order implicitly without performing comparisons.
Traverse the original heights array while maintaining a pointer that represents the current expected height in sorted order. Move this pointer forward through the frequency array until you find a height with remaining count. If the expected height differs from the current element in the original array, increment the mismatch counter. Decrease the frequency and continue. Because each element is processed once and the height range k is constant (100), the runtime becomes O(n + k), which effectively behaves like O(n).
This approach uses ideas from array frequency counting and works best when the value range is limited. It avoids the O(n log n) cost of general sorting and is considered the optimal solution for this problem.
Recommended for interviews: Start with the sorting comparison solution because it clearly demonstrates understanding of the problem. After that, mention the counting sort optimization once you recognize the bounded height range. Interviewers often expect candidates to notice this constraint and reduce the complexity from O(n log n) to near O(n).
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Sorting and Comparison | O(n log n) | O(n) | General solution when no assumptions about value range are used |
| Counting Sort Optimization | O(n + k) | O(k) | Best when heights fall within a small bounded range such as 1–100 |