Advertisement

Height Checker - LeetCode 1051 Solution

Height Checker - Complete Solution Guide

Height Checker is LeetCode problem 1051, a Easy level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

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 i th 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 i th student in line ( 0-indexed ). Return the number of indices where h

Detailed Explanation

Imagine you're trying to line up a group of students for a photo, where everyone needs to be ordered from shortest to tallest. You've got the students standing in some arbitrary current arrangement. This problem asks you to figure out how many students are *not* in the correct spot they *should* be in if the line were perfectly sorted by height. We're interested in counting how many *positions* are occupied by a student whose height doesn't match the height of the student who *should* be at that position in the ideal, sorted line.

Solution Approach

The most intuitive way to tackle this 'spot the difference' challenge is to first construct the 'ideal' sorted line. The provided solution achieves this by taking the input `heights` array and creating a new array, `expected`, which is simply a sorted version of `heights`. This `expected` array now represents the non-decreasing order of heights that the students *should* be in. Once we have both `heights` (the current arrangement) and `expected` (the ideal arrangement), the solution iterates through both arrays simultaneously using an index `i`. For each position `i`, it compares `heights[i]` with `expected[i]`. If these two values are different, it means the student currently at position `i` is not the correct height for that position in a perfectly sorted line. A counter, `count`, is incremented for each such mismatch. Finally, after checking every position, the total `count` of mismatches is returned. This direct, element-by-element comparison against a pre-computed ideal state is highly effective because it precisely isolates every instance where the current reality deviates from the desired order.

Step-by-Step Algorithm

  1. Step 1: Create a copy of the `heights` array (or sort in place, depending on the language).
  2. Step 2: Sort the copied array (this becomes the `expected` array).
  3. Step 3: Initialize a counter `count` to 0.
  4. Step 4: Iterate through the `heights` array and compare each element `heights[i]` with the corresponding element `expected[i]`.
  5. Step 5: If `heights[i]` != `expected[i]`, increment `count`.
  6. Step 6: After iterating through the entire array, return the value of `count`.

Key Insights

  • **Constructing the 'Ground Truth':** The crucial first step is to establish what the perfectly sorted lineup *should* look like. This is done by creating a sorted copy of the input `heights` array. Without this `expected` array, comparing individual `heights[i]` values wouldn't have a meaningful reference point. It explicitly defines the target state for comparison.
  • **Preserving Original Order for Comparison:** When sorting, it's essential to create a *new*, sorted array (like `expected`) rather than sorting the `heights` array in-place. This ensures that the original `heights` array, representing the current student arrangement, remains intact. We need both the current (unsorted) and the ideal (sorted) arrangements available simultaneously to perform the direct index-by-index comparison.
  • **Direct Index-to-Index Mismatch Tally:** The problem simplifies to a straightforward `if (heights[i] != expected[i]) count++` loop. There's no need for complex graph traversals, dynamic programming, or intricate permutation logic. Once the `expected` array is available, the problem boils down to a single pass, O(N) scan, making it remarkably efficient after the initial sort.

Complexity Analysis

Time Complexity: O(n log n)

Space Complexity: O(n)

Topics

This problem involves: Array, Sorting, Counting Sort.

Companies

Asked at: Salesforce.