题解 | #加油站#

加油站

https://www.nowcoder.com/practice/3b1abd8ba2e54452b6e18b31780b3635

#
# 
# @param gas int整型一维数组 
# @param cost int整型一维数组 
# @return int整型
#
class Solution:
    def canCompleteCircuit(self , gas , cost ):
        # write code here
        b=[]
        for i in range(len(gas)):
            b.append(gas[i]-cost[i])
        if max(b)<0:
            return -1
        i=b.index(max(b))
        while(i<len(gas)):
            next=(i+1)%len(gas)
            curOil=b[i]
            while(next!=i and next<len(gas)):
                curOil +=b[next]
                if curOil<0:
                    break
                next=(next+1)%len(gas)
            if curOil>=0:
                return i
            i += 1
        return -1

全部评论

相关推荐

争当牛马还争不上
码农索隆:1.把简历改哈 2.猛投,狠投 3.把基础打牢 这样你在有机会的时候,才能抓住
点赞 评论 收藏
分享
06-23 11:43
门头沟学院 Java
allin校招的烤冷...:我靠,今天中午我也是这个hr隔一个星期发消息给我。问的问题还是一模一样的😅
点赞 评论 收藏
分享
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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