Click here to Skip to main content
15,353,841 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hi,
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


https://gm1.ggpht.com/e-wYGDjfeuUn5lkkDcBAW0bYmSwgyLP0Tg08Ho7jtHDjV3l3dEa1GQw2FjX8dZmzJPxdHO-41kHU5reQgvCsgx2OcSYUNc50fnhkqYyvH3QiMRqpRs8xF4mCbdmWpDPm0wHAe03DhxyujjJ-ATFOYkOL0EMSs4db5yPg8aPDLr9p3BUIdo_SZ60gxVgZazpAHkmJhkydNROd1-hDw3_Sp6YHu5qsWI5SH1hIQCDa1RNmN_qaQO2hQHFQrD3w49yeiU7M-qvh1qGK1hK_NJJyGyvthEo7ZrsbwzvC9yu6UAaY39_Xf7yxXr9x63gocYXQ2nVm079U8Xo6XbLeNyr5NTA56E4O_6CyF8gSs03VTFSts-5DQmm0NMiz-QQqKtfTuASJJRQoRqIc_bvbOYE0UeO1HkJpjd2mpfuxALlnt_v-N6NrXulD_G2hIJp7P3ATveht4st1iva3eWYpExSXQw0NKiuAcI2A-_5NwxQyfaI1_AuuhzV9YUPEIoXXlr5mrmck36MkWZj35GH4c0E_T6g3NV1F0QyI3Q55FrJDLk0ZLWdVihZfrJWVNuaYPjuYJMXMYvbpJlstIQEzG-ER7M8Syri7ytck4I6RLuFyG20jPmgTyWrdj8E2AwWKwilBEB8vwb6xb-ceD_6B6JjHPISGkM2QJ3-mUxx40IpG7lGBWfo3jXNPAVbPqy3wbzkdbGbrZIWan5Aa5z4j5hnrrN_TndrOZ0Dd1huoT4FyzJgpPVjXyApsFP_BPJkn_Qvq7Vd2bxw4bToEpBZ8jNkwJKMjmP0CPQhG21_UyAZrl-vdEFq3A1G_=s0-l75-ft-l75-ft[^]

What I have tried:

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

s=[101,87,122,208,74,107,152,130]
s=[float('inf')]+s
stack = [0]
res = []

for i in range(1,len(s)):
    while s[stack[-1]]<s[i]:
        stack.pop()
    res.append(stack[-1]) 
    stack.append(i)
    
print(res)


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

Thank you !!
Posted
Updated 27-May-22 23:11pm
v2
Comments
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[^]
   
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
Python
               relev.  |
               v   v   V
height = [140,160,140,110,90,120,160,140,110]
the list of meaningful buildings should be
Python
[(2,160),(3,140)]
   
Hi thank you both for your answer!
I have written this:

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

for i in range(1,len(s)):
    while s[stack[-1]]<s[i] and len(stack)>1:
        stack.pop()
    res.append(stack[-1])
    stack.append(i)
    
print(res)



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

thenks!
   
Comments
Richard MacCutchan 28-May-22 5:45am
   
Try this:
    s = [140,160,140,110,90,60,90,140,110]
    res=[0]

    for i in range(1,len(s)):
        for j in range(i - 1, 0, -1):
            if s[j] > s[i]:
                res.append(j)
                break;
        else:
            res.append(0)
        

    print(res)
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