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:
โ€ข 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
๐Ÿ“ธ School Photo Height Checker๐Ÿ“ทCurrent Line-Up (Wrong!)๐Ÿ‘ฆ150Pos 0๐Ÿ‘ง150Pos 1๐Ÿง‘170Pos 2๐Ÿ‘จ160Pos 3๐Ÿ‘ถ150Pos 4๐Ÿ‘ฉ165Pos 5SORTExpected Line-Up (Correct!)๐Ÿ‘ฆ150Pos 0๐Ÿ‘ง150Pos 1๐Ÿ‘ถ150Pos 2๐Ÿ‘จ160Pos 3๐Ÿ‘ฉ165Pos 4๐Ÿง‘170Pos 5โœ“โœ“โœ—โœ“โœ—โœ—๐Ÿ“Š AnalysisCurrent: [150,150,170,160,150,165]Expected: [150,150,150,160,165,170]Position 0: 150 = 150 โœ“Position 1: 150 = 150 โœ“Position 2: 170 โ‰  150 โœ—Position 3: 160 = 160 โœ“Position 4: 150 โ‰  165 โœ—Position 5: 165 โ‰  170 โœ—Result: 3 students to move"Perfect! Now everyone is in the right position for the photo!"
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!
Asked in
Google 35 Amazon 28 Apple 22 Microsoft 18
42.4K Views
Medium Frequency
~8 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen