304 North Cardinal St.
Dorchester Center, MA 02124

# Height Checker LeetCode Solution

## Problem – Height Checker LeetCode Solution

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`

### Height Checker LeetCode Solution in Java

``````class Solution {
public int heightChecker(int[] heights) {
int[] heightToFreq = new int;

for (int height : heights) {
heightToFreq[height]++;
}

int result = 0;
int curHeight = 0;

for (int i = 0; i < heights.length; i++) {
while (heightToFreq[curHeight] == 0) {
curHeight++;
}

if (curHeight != heights[i]) {
result++;
}
heightToFreq[curHeight]--;
}

return result;
}
}
``````

### Height Checker LeetCode Solution in Python

``````class Solution:
def heightChecker(self, heights: List[int]) -> int:
max_nr = max(heights)
# initialize frequency array with 0's
count =  * (max_nr + 1)
# get frequencies
for number in heights:
count[number] += 1
# create a sumcount array
sumcount =  * (max_nr + 1)
for index, number in enumerate(count[1:],start=1):
sumcount[index] = number + sumcount[index-1]
# sumcount determines the index in sorted array
# create output array
output =  * len(heights)
# loop backwards starting with last element for stable sort
for p in range(len(heights)-1,-1,-1):
output[sumcount[heights[p]]-1] = heights[p]
sumcount[heights[p]] -= 1
# return the difference compared to original array
result = 0
for index, number in enumerate(heights):
if number != output[index]:
result += 1
return result
``````

### Height Checker LeetCode Solution in C++

``````    int heightChecker(vector<int>& heights) {
int mismatch = 0; //count the number of mismatches
vector<int> count(101, 0); // store the count of heights

for(int i = 0; i < heights.size(); i++) count[heights[i]]++;

int i = 1, j = 0;
while(i < 101){
// check value of count
if(count[i] == 0){
i++;
}
else{
// compare the index of count with value of height
if(i != heights[j]) mismatch++;
j++; count[i]--;
}
}

return mismatch;
}
``````
##### Height Checker LeetCode Solution Review:

In our experience, we suggest you solve this Height Checker LeetCode Solution and gain some new skills from Professionals completely free and we assure you will be worth it.

If you are stuck anywhere between any coding problem, just visit Queslers to get the Height Checker LeetCode Solution

Find on LeetCode

##### Conclusion:

I hope this Height Checker LeetCode Solution would be useful for you to learn something new from this problem. If it helped you then don’t forget to bookmark our site for more Coding Solutions.

This Problem is intended for audiences of all experiences who are interested in learning about Data Science in a business context; there are no prerequisites.

Keep Learning!

More Coding Solutions >>

LeetCode Solutions

Hacker Rank Solutions

CodeChef Solutions