NeetCode #60LC-1051EasyArrays & Hashing
← Back to All Problems#60 · #1051 · Height Checker(高度检查器)
📌 Problem Statement & Constraints
Students are asked to stand in non-decreasing order of height. Given the current order in
heights, return the number of indices where the current order does not match the sorted order. Constraints: 1 <= heights.length <= 100, 1 <= heights[i] <= 100.💡 Core Algorithmic Approaches
- The expected order is simply the sorted array, so build it and compare position by position.
- Count the indices where the two differ.
- Because the value range is only 1..100, counting sort is a viable O(n + V) alternative to a comparison sort.
- The answer is the minimum number of moves needed, since each mismatched position needs exactly one student relocated.
💻 Benchmark Python3 Implementation
class Solution:
def heightChecker(self, heights: List[int]) -> int:
expected = sorted(heights)
return sum(1 for a, b in zip(heights, expected) if a != b)
# Counting sort: exploits the bounded value range 1..100
class Solution2:
def heightChecker(self, heights: List[int]) -> int:
cnt = [0] * 101
for h in heights:
cnt[h] += 1
i = 0
res = 0
for h in range(1, 101):
for _ in range(cnt[h]):
if heights[i] != h: # compare against the sorted stream
res += 1
i += 1
return res⚡ Complexity Deep Dive
⏱️ Time Complexity
O(n log n) for the sort-based version, O(n + V) for the counting-sort version with V = 100.
💾 Space Complexity
O(n) for the sorted copy; O(V) = O(1) for the counting version.
⚠️ Interview Pitfalls & Follow-ups
- Counting inversions instead of position mismatches: the answer is the number of positions where the arrays differ, not the number of out-of-order pairs.
- Sorting the input in place and then comparing: after sorting, the original order is lost, so a copy is needed.
- Assuming
heightscontains distinct values: duplicates are allowed, and equal values must be matched positionally.
⚡
Test muscle memory on this topic via 3-Layer Flashcards
Active Recall: Mentally formulate Prompt ➔ Math Derivation ➔ Benchmark Implementation in 3 minutes.
🧩 Related Arrays & Hashing Problems
中文速览与面试要点
高度检查器
学校打算为全体学生拍一张年度纪念照。根据要求,学生需要按照 非递减 的高度顺序排成一行。排序后的高度情况用整数数组 expected 表示,其中 expected[i] 是排在第 i 位的学生的高度。给你一个整数数组 heights,表示当前学生排列的高度。返回满足 heights[i] != expected[i] 的下标数目。约束:1 ≤ heights.length ≤ 100;1 ≤ heights[i] ≤ 100。
核心要点
- 排序后逐位比较(标准解) :把 heights 拷贝一份排序得到 expected,然后逐位比较,统计不相等的位置数。
- 为什么这样就能算出「需要移动的人数」 :排序后的序列是唯一的目标序列(非递减),当前位置与目标不一致的学生必然需要换位。
- 计数排序优化(利用值域小) :heights[i] ≤ 100,可用长度 101 的计数数组在 O(n + 100) 时间内得到排序结果,避免 O(n log n)。
- 原地比较技巧 :也可以直接按计数数组从低到高「重建」目标序列并与原数组逐位比较,无需真正排序。
常见陷阱
- 忘记先拷贝再排序 : heights.sort() 会原地排序,之后逐位比较时两边完全相同,答案恒为 0。必须用 sorted(heights) 。
- 误以为要统计「逆序对」 :本题统计的是「与目标位置不一致的个数」,不是逆序对数量,两者不同。
- 误以为答案要除以 2 :一次交换可以修正两个位置,但题目问的是「下标数目」(即人数),不需要除以 2。
参考实现
class Solution:
def heightChecker(self, heights: list[int]) -> int:
expected = sorted(heights)
return sum(a != b for a, b in zip(heights, expected))
# 计数排序版本(O(n + 100),利用值域 ≤ 100):
# cnt = [0] * 101
# for h in heights: cnt[h] += 1
# i = 0; ans = 0
# for h in range(1, 101):
# for _ in range(cnt[h]):
# if heights[i] != h: ans += 1
# i += 1
# return ans