- 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

# Program to find maximum building height in Python

Suppose we have a value n and another list of pairs called restrictions. We want to build n new buildings in a city. But there are few restrictions. We can built in a line and buildings are labeled from 1 to n. The restrictions has two parameters, so restrictions[i] = (id_i, max_height_i) indicates id_i must have height less than or equal to max_height_i. The city restrictions on the heights of the new buildings are as follows −

The height of each building must be 0 or positive values.

First building height must be 0.

The difference between any two adjacent buildings height cannot exceed 1.

We have to find the maximum possible height of the tallest building.

So, if the input is like n = 5, restrictions = [[2,1],[4,3]], then the output will be 4 because we have to find maximum possible height, so it can be 4 as shown in the diagram.

To solve this, we will follow these steps −

if restrictions is empty, then

return n-1

resi := sort the list restrictions based on id

k := 0

idx := 1

for each re in resi, do

re[1] := minimum of re[1] and (k+re[0]-idx)

k := re[1]

idx := re[0]

k := max height of last element in resi

idx := id of last element in resi

reverse the list resi

for each re in resi from first item onwards, do

re[1] := minimum of re[1] and (k-re[0]+idx)

k := re[1]

idx := re[0]

reverse the list resi

f := 0

idx := 1

res := 0

for each re in resi, do

ff := minimum of (f+re[0]-idx) and re[1]

res := maximum of res and quotient of (re[0] - idx + f + ff)/2

idx := re[0]

f := ff

return maximum of (f+n-idx) and res

## Example

Let us see the following implementation to get better understanding

def solve(n, restrictions): if not restrictions: return n-1 resi = sorted(restrictions, key = lambda x:x[0]) k = 0 idx = 1 for re in resi: re[1] = min(re[1], k+re[0]-idx) k = re[1] idx = re[0] k = resi[-1][1] idx = resi[-1][0] resi.reverse() for re in resi[1:]: re[1] = min(re[1], k-re[0]+idx) k = re[1] idx = re[0] resi.reverse() f = 0 idx = 1 res = 0 for re in resi: ff = min(f+re[0]-idx, re[1]) res = max(res, (re[0] - idx + f + ff) // 2) idx = re[0] f = ff return max(f+n-idx,res) n = 5 restrictions = [[2,1],[4,3]] print(solve(n, restrictions))

## Input

5, [[2,1],[4,3]]

## Output

4

- Related Articles
- Program to find a matrix for each condominium's height is increased to the maximum possible height in Python?
- C++ program to find out the center coordinates and the height of a building
- Program to find maximum width ramp in Python
- Program to find maximum erasure value in Python
- Program to find maximum in generated array in Python
- Program to find maximum average pass ratio in Python
- Program to find maximum ice cream bars in Python
- Program to find maximum subarray min-product in Python
- Python Program for Maximum height when coins are arranged in a triangle
- Program to find out the farthest building a parkour artist can reach in Python
- Program to find maximum score in stone game in Python
- Program to find maximum population year using Python
- Program to find maximum product of contiguous subarray in Python
- Python program to find the second maximum value in Dictionary
- Program to find maximum score from removing stones in Python