华为算法岗笔试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.使用拓扑排序小顶堆

全部评论
用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 四川
我这里有参考答案
点赞 回复 分享
发布于 2023-09-07 19:10 广东

相关推荐

这算盘打的
程序员小白条:都这样的,都是潜规则,你自己说可以实习一年就行了,实习可以随便跑路的
点赞 评论 收藏
分享
06-12 16:00
天津大学 Java
牛客30236098...:腾讯坏事做尽,终面挂是最破防的 上次被挂了后我连简历都不刷了
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
07-02 18:35
简历上把1个月实习写成了3个月,会进行背调吗?
码农索隆:一个月有一个月的实习经历,三个月有三个月的实习经历
简历当中有水分算不算造假...
点赞 评论 收藏
分享
评论
2
9
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务