
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Height Checker in Python
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 −
- answer := 0
- let x := Array in sorted form
- ley y := Array
- for i := 0 to size of Array – 1 −
- if x[i] is not same as y[i], then increase answer by 1
- return answer
Example
Let us see the following implementation to get better understanding −
class Solution(object): def heightChecker(self, heights): ans = 0 x = sorted(heights) y = heights for i in range(len(x)): if x[i]!=y[i]: ans+=1 return ans ob1 = Solution() print(ob1.heightChecker([1,2,4,2,1,3]))
Input
[1,1,4,2,1,3]
Output
4
- Related Articles
- Strong Password Checker in Python
- Changing ttk Button Height in Python
- Program to find maximum building height in Python
- Python Program to Read Height in Centimeters and convert the Height to Feet and Inches
- Getting screens height and width using Tkinter Python
- Program to find a matrix for each condominium's height is increased to the maximum possible height in Python?
- Python Program for Maximum height when coins are arranged in a triangle
- Setting Line Height in CSS
- Check if a given Binary Tree is height balanced like a Red-Black Tree in Python
- What is the difference between height and line-height?
- Queue Reconstruction by Height in C++
- The max-height Property in CSS
- The min-height Property in CSS
- Queue Reconstruction by Height in JavaScript
- Program to find minimum number of bricks required to make k towers of same height in Python

Advertisements