1051. Height Checker LeetCode Solution
In this guide, you will get 1051. Height Checker LeetCode Solution with the best time and space complexity. The solution to Height Checker problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Height Checker solution in C++
- Height Checker solution in Java
- Height Checker solution in Python
- Additional Resources

Problem Statement of Height Checker
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
Complexity Analysis
- Time Complexity:
- Space Complexity:
1051. Height Checker LeetCode Solution in C++
class Solution {
public:
int heightChecker(vector<int>& heights) {
int ans = 0;
int currentHeight = 1;
vector<int> count(101);
for (int height : heights)
++count[height];
for (int height : heights) {
while (count[currentHeight] == 0)
++currentHeight;
if (height != currentHeight)
++ans;
--count[currentHeight];
}
return ans;
}
};
/* code provided by PROGIEZ */
1051. Height Checker LeetCode Solution in Java
class Solution {
public int heightChecker(int[] heights) {
int ans = 0;
int currentHeight = 1;
int[] count = new int[101];
for (int height : heights)
++count[height];
for (int height : heights) {
while (count[currentHeight] == 0)
++currentHeight;
if (height != currentHeight)
++ans;
--count[currentHeight];
}
return ans;
}
}
// code provided by PROGIEZ
1051. Height Checker LeetCode Solution in Python
class Solution:
def heightChecker(self, heights: list[int]) -> int:
ans = 0
currentHeight = 1
count = [0] * 101
for height in heights:
count[height] += 1
for height in heights:
while count[currentHeight] == 0:
currentHeight += 1
if height != currentHeight:
ans += 1
count[currentHeight] -= 1
return ans
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.