京东笔试第3题

这个代码自己看不出来有什么问题,测试一个都不通过
def min_cost(N, matrix):
    directions = [(0, 1), (1, 0), (-1,0)]
    visited = set()
    start = (0, 0)
    end = (1, N-1)
    total_cost = matrix[0][0]
    i = 0
    while start != end:
        visited.add(start)
        tmp_cost = {}
        for direction in directions:
            new_x = start[0] + direction[0]
            new_y = start[1] + direction[1]
            if 0 <= new_x < 2 and 0 <= new_y < N and (new_x, new_y) not in visited:
                tmp_cost[(new_x, new_y)] = matrix[new_x][new_y]
        if i % 2 == 1:
            max_key = max(tmp_cost, key=tmp_cost.get)
            max_value = tmp_cost[max_key]
            total_cost += max_value
            start = max_key
            i += 1
        else:
            min_key = min(tmp_cost, key=tmp_cost.get)
            min_value = tmp_cost[min_key]
            total_cost += min_value
            start = min_key
            i += 1
    return total_cost
全部评论
我样例过了,提交一个不过
1 回复 分享
发布于 2024-08-31 12:18 湖北
老哥,第一手貌似是走到初始位置,这个我也最后看了用例才注意到,然后来不及了😰😰
点赞 回复 分享
发布于 2024-08-31 12:12 上海

相关推荐

丿南烟丶:黑白模板吧,不要这样花哨的。 主要成就太空了,和获奖融在一起,写一两行就行了。 职业技能不要这样排,就传统的掌握精通什么什么然后举例补充的一些重要技术点。 自我介绍说实话也没啥用,可以删了。 把自己的两个项目方案细节补充上去,为什么这样设计,怎么设计,成果是什么按star法则来写 你要引导面试官来问你的技能和项目,你的获奖和自我介绍别人可能看都不看一眼或者不太在乎,重要的是展示你能干活的能力
点赞 评论 收藏
分享
牛客54175811...:今年对双非很难。1、争取一段大厂实习经历,2、狂磕八股,3、再跑个难度提升的项目。
点赞 评论 收藏
分享
评论
点赞
1
分享

创作者周榜

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