华为算法岗笔试2024届秋招

1.力扣739.每日温度,使用单调栈,使用for循环不知道为什么会超时,使用while不会(看看有没有大佬知道为啥)

class Solution(object):
    def dailyTemperatures(self, temperatures):
        """
        :type temperatures: List[int]
        :rtype: List[int]
        """
        temp=[0]
        output=[0]*len(temperatures)
        for i in range(1,len(temperatures)):
            while len(temp):
			#使用for循环会超时
            #for _ in range(len(temp)):
                if temperatures[i]>temperatures[temp[-1]]:
                    output[temp[-1]]=i-temp[-1]
                    temp.pop()
                else:
                    break
            temp.append(i)

        return output

2.中庸行者行走最大步数,使用dfs

3.使用拓扑排序小顶堆

全部评论
我这里有参考答案
点赞 回复 分享
发布于 2023-09-07 19:10 广东
用stack list res = [0]*len(temperatures) stack = [] for i, t in enumerate(temperatures): while stack and t > stack[-1][0]: stackT, stackIND = stack.pop() res[stackIND] = i - stackIND stack.append((t,i)) return res
点赞 回复 分享
发布于 2023-09-09 14:15 四川

相关推荐

点赞 评论 收藏
分享
评论
2
9
分享

创作者周榜

更多
牛客网
牛客企业服务