Suppose a set of students have to be arranged in non-decreasing order of their heights for a photograph. If we have an array of students, we have to return the minimum number of students that are not present in correct position. So if the array is like [1, 1, 4, 2, 1, 3], then output will be 3. So students with height 4, 3 and the last 1 are not standing in the correct position.
To solve this, we will follow these steps −
Let us see the following implementation to get better understanding −
class Solution(object): def heightChecker(self, heights): """ :type heights: List[int] :rtype: int """ ans = 0 x = sorted(heights) y = heights for i in range(len(x)): if x[i]!=y[i]: ans+=1 return ans