建筑物使用堆栈的高度

发布于 2025-02-01 11:13:38 字数 1627 浏览 2 评论 0原文

我有一个问题:

一行有n个建筑物。给您一个大小为n的整数数组高度,代表线路中建筑物的高度。 对于每个建筑物,您都需要将建筑物的位置返回左侧的每个建筑物的位置, 如果没有更高的建筑物,则需要返回0。 例如:

高度= [140,160,140,​​110,90,120,120,160,140,​​110]

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

注意:时间复杂性必须为O(N)

输出 //i.sstatic.net/c6sq4.png“ rel =” nofollow noreferrer'>一个用于理解问题的图片

我尝试了此代码:

s=[140,160,140,110,90,60,90,140,110]
s=[10000000]+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)

起初它不起作用,因为while while loop停止了,因为stack stack变得空了 因此,我了解我能做的唯一方法是添加一个大数字:1000000到“ S”的开始 但是我不能使用它,因为我不想更改Givan数组。

我尝试在循环中使用如果Len(stack)&gt; 1,但它不起作用。

我更改代码:

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

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

它使代码有效,但不正确 我注意到它给了我阵列 [0,0,1,2,3,4,4,2,7] 设置 [0,0,2,3,4,5,5,3,8]

因此,我写了这篇文章:

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

for i in range(1,len(s)):
    while s[stack[-1]]<s[i]:
        if len(stack)>1:
            stack.pop()
        else:
            break
    if stack[-1]==0:
        res.append(stack[-1])
    else:
        res.append(stack[-1]+1)
    stack.append(i)
print(res)

它使代码正确,但仅在这种情况下,如果我放置了数组

[101,87,122,208,74,107,107,152,130] 它排在第1位。

有人可以告诉我我在哪里? 然后你!

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

I have tried this code:

s=[140,160,140,110,90,60,90,140,110]
s=[10000000]+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)

At first it didn't work because the while loop had stop because stack became empty
so I understand that the only way I can do is add a big number like: 1000000 to the start of "s"
but I can't use it because I don't want to change the givan array.

I tried using if len(stack)>1 in the loop but it didn't work.

I change the code to this:

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

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

and it make the code works but not in the right way
i notice it give me the array
[0, 0, 1, 2, 3, 4, 4, 2, 7]
insted of
[0, 0, 2, 3, 4, 5, 5, 3, 8]

so i wrote this:

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

for i in range(1,len(s)):
    while s[stack[-1]]<s[i]:
        if len(stack)>1:
            stack.pop()
        else:
            break
    if stack[-1]==0:
        res.append(stack[-1])
    else:
        res.append(stack[-1]+1)
    stack.append(i)
print(res)

which make the code correct but only for this case, if i put the array

[101,87,122,208,74,107,152,130]
it falls in number 1.

someone can pleas tell me where i am worg?
thenk you!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

眼眸里的那抹悲凉 2025-02-08 11:13:38

old=[140,160,140,110,90,120,160,140,110]
s=[max(old)+1]+old
stack=[0]
res=[]

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

你像我一样向新数组添加值怎么样


old=[140,160,140,110,90,120,160,140,110]
s=[max(old)+1]+old
stack=[0]
res=[]

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

how about u add values to new array like i did

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文