Height Checker - Problem
School Photo Height Arrangement Problem
It's picture day at school! ๐ธ The students need to line up in non-decreasing order by height for the perfect annual photo. However, some students are currently standing in the wrong positions.
You are given two scenarios:
โข
โข
Your task is to determine how many students are standing in the wrong position. In other words, count the number of indices where
Goal: Return the minimum number of students that need to move to achieve the correct height arrangement.
It's picture day at school! ๐ธ The students need to line up in non-decreasing order by height for the perfect annual photo. However, some students are currently standing in the wrong positions.
You are given two scenarios:
โข
heights[] - The current arrangement of students by heightโข
expected[] - The correct arrangement (sorted in non-decreasing order)Your task is to determine how many students are standing in the wrong position. In other words, count the number of indices where
heights[i] != expected[i].Goal: Return the minimum number of students that need to move to achieve the correct height arrangement.
Input & Output
example_1.py โ Basic Case
$
Input:
heights = [1,1,4,2,1,3]
โบ
Output:
3
๐ก Note:
Expected sorted order: [1,1,1,2,3,4]. Students at positions 2, 4, and 5 are in wrong positions (4โ1, 1โ3, 3โ4), but the optimal answer considers that position 2 should have 1 instead of 4, position 3 should have 2 (correct), position 4 should have 3 instead of 1, and position 5 should have 4 instead of 3. Total mismatches: 3.
example_2.py โ Already Sorted
$
Input:
heights = [5,1,2,3,4]
โบ
Output:
5
๐ก Note:
Expected sorted order: [1,2,3,4,5]. Every single position has the wrong student: position 0 has 5โ1, position 1 has 1โ2, position 2 has 2โ3, position 3 has 3โ4, position 4 has 4โ5. All 5 students are in wrong positions.
example_3.py โ No Changes Needed
$
Input:
heights = [1,2,3,4,5]
โบ
Output:
0
๐ก Note:
The array is already sorted in non-decreasing order. Expected order: [1,2,3,4,5]. Every student is in the correct position, so 0 students need to move.
Constraints
- 1 โค heights.length โค 100
- 1 โค heights[i] โค 100
- All heights are positive integers
- Array represents student heights in centimeters or similar unit
Visualization
Tap to expand
Understanding the Visualization
1
Current Line-Up
Students are currently standing: [1,1,4,2,1,3] - some are clearly out of place
2
Expected Order
For the photo, they should be: [1,1,1,2,3,4] - shortest to tallest
3
Count Mismatches
Compare position by position: 4 students need to move to correct positions
Key Takeaway
๐ฏ Key Insight: Compare each position in the current arrangement with what should be there in the sorted order - count the mismatches!
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code