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 <= 100
1 <= heights[i] <= 100
When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The height checker problem wants to know how many students are out of order. The simplest way to solve this is to figure out what the correct order should be and then compare it to the given order.
Here's how the algorithm would work step-by-step:
def height_checker(heights):
# Create a copy to avoid modifying the original
expected_heights = heights.copy()
# Sort the copied list to represent the ideal order
expected_heights.sort()
mismatched_students = 0
# Compare the original and sorted lists
for index in range(len(heights)):
# Count the students that are not in the correct position
if heights[index] != expected_heights[index]:
mismatched_students += 1
return mismatched_students
The efficient way to solve this problem is to avoid comparing the original order to every single possible reordering. Instead, we create a perfectly sorted version and then compare it to the original to see where they differ.
Here's how the algorithm would work step-by-step:
def heightChecker(heights): # Create a sorted copy of the original heights expected_heights = sorted(heights[:])
mismatched_students = 0
# Compare the original and sorted lists for i in range(len(heights)): # Count discrepancies between the two lists
if heights[i] != expected_heights[i]: mismatched_students += 1
return mismatched_students
Case | How to Handle |
---|---|
Null or empty input array | Return 0 if the input array is null or empty, as there are no students to compare. |
Input array with only one element | Return 0 because if there is only one student, they are trivially in the correct position. |
Input array with all elements the same | Return 0 because the sorted array will be identical to the original array. |
Input array already sorted in non-decreasing order | Return 0 as no students are out of place. |
Input array sorted in strictly decreasing order | The return value will be equal to the number of elements, since every student is out of place. |
Input array with a large number of elements (scalability) | Using an efficient sorting algorithm (e.g., merge sort, quicksort) ensures the solution scales well. |
Input array containing large integer values | The sorting algorithm should handle large integer values without overflow issues (using `int` or `long` appropriately). |
Array with duplicate heights mixed with non-sorted heights | The standard sorting algorithm correctly handles duplicate heights in `heights` to generate `expected` array. |