Click here to Skip to main content
15,901,373 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
i have a problem:

There are n buildings in a line. You are given an integer array heights of size n that represents the heights of the buildings in the line.
for every building you need to return the position of the building each building sess in front to the left,
if there is no building higher then you need to return 0.
For example:

height = [140,160,140,110,90,120,160,140,110]

Output = [0,0,2,3,4,3,2,7,8]

NOTE: The time complexity needs to be O(n)

A picture for understanding the question[^]

What I have tried:

i have solution using 'inf' but i cant use it because i cant change the given array:

stack = [0]
res = []

for i in range(1,len(s)):
    while s[stack[-1]]<s[i]:

i need to write a similar code, that is to say using while loop but without the 'inf'.

Thank you !!
Updated 27-May-22 23:11pm
Richard MacCutchan 27-May-22 5:00am    
Draw a representation of the buildings as a bar chart and the answer should be easier to figure out.

While we are more than willing to help those that are stuck, that doesn't mean that we are here to do it all for you! We can't do all the work, you are either getting paid for this, or it's part of your grades and it wouldn't be at all fair for us to do it all for you.

So we need you to do the work, and we will help you when you get stuck. That doesn't mean we will give you a step by step solution you can hand in!
Start by explaining where you are at the moment, and what the next step in the process is. Then tell us what you have tried to get that next step working, and what happened when you did.

If you are having problems getting started at all, then this may help: How to Write Code to Solve a Problem, A Beginner's Guide[^]
Share this answer
Hint: the trick for reaching time O(N) complexity is building and updating a list of tuples (position, height) representing the meaningful buildings to the left of the current one.
Where meaningful buildings means buildings occurring in decreasing height order.
For instance, when your code processes the fourth building
               relev.  |
               v   v   V
height = [140,160,140,110,90,120,160,140,110]
the list of meaningful buildings should be
Share this answer
Hi thank you both for your answer!
I have written this:

s = [140,160,140,110,90,60,90,140,110]
stack =[0]

for i in range(1,len(s)):
    while s[stack[-1]]<s[i] and len(stack)>1:

but it's not correct :(

someone can help me understand where i am wrong?
i am stuck on this for more than two weeks and i tried everything

Share this answer
Richard MacCutchan 28-May-22 5:45am    
Try this:
    s = [140,160,140,110,90,60,90,140,110]

    for i in range(1,len(s)):
        for j in range(i - 1, 0, -1):
            if s[j] > s[i]:

Itay Drori 28-May-22 5:55am    
hi thank you for your answer !

it return [0, 0, 2, 3, 4, 5, 4, 2, 8]

insted of [0, 0, 2, 3, 4, 5, 5, 3, 8]

you know way?
Richard MacCutchan 28-May-22 6:29am    
No, the result from that code is correct. The value 140 at position 8 has only one value to the left which is greater, and that is 160 at position 2. Your expected result suggests checking for greater than or equal.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900