算法分析与设计第七周:134. Gas Station

题目简介:
There are N gas stations along a circular route, where the amount of gas at station i is gas[i].

You have a car with an unlimited gas tank and it costs cost[i] of gas to travel from station i to its next station (i+1). You begin the journey with an empty tank at one of the gas stations.

Return the starting gas station’s index if you can travel around the circuit once, otherwise return -1.

提炼内容:
何为合法出发点:
1、该点p的pCost值小于pGas值
2、该点的后续节点q,若其qCost值大于qGas值,那么p中汽油剩余值(remainingGas = pGas - pCost) + qGas >= qCost
3、以此类推

算法思想:
1、找出所有的非法出发点
2、将非法出发点存入list unablePos
3、不在list中的出发点即为合法出发点
4、若出发点全在list中,则无合法出发点,程序返回-1

代码:

class Solution(object):
    def canCompleteCircuit(self, gas, cost):
        #分别表示剩余汽油和所需汽油
        remainingGas , needGas, i = 0, 0, 0
        #用于保存非法出发点
        unablePos = []
        size = len(gas)
        #对每个出发点进行遍历并判断其是否合法
        while i < size:
            #遇到非法出发点
            if gas[i] < cost[i]:
                #保存非法出发点
                unablePos.append(i)
                #若下一个点是合法出发点,则从该出发点反序遍历
                #即遇到了gas: 3 4 5 第三个出发点
                #        cos: 2 3 6
                if gas[(i + 1) % size] >= cost[(i + 1) % size]:
                    #得到该节点需要的汽油量
                    needGas = cost[i] - gas[i]
                    index = i
                    #反序遍历直到前面的剩余汽油量达到需要汽油量值
                    while remainingGas < needGas:
                        if i == index + size:
                            return -1
                        index -= 1
                        #更新剩余汽油量值
                        remainingGas += gas[index] - cost[index]
                    #下一次检查前,将两个值归0
                    remainingGas = 0
                    needGas = 0
                    #保存非法出发点,区间(index, i]内的出发点都是非法的,
                    #因为这些节点都不没有足够的汽油可以使用
                    unablePos.extend([(x + size) % size for x in range(i,index,-1)])
            i += 1

        #寻找合理出发点
        pos = [[x,0] for x in range(size)]
        for i in unablePos:
            pos[i][1] = 1
        for i in pos:
            if i[1] == 0:
                return i[0]
        return -1
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,根据您的要求,以下是我给出的代码实现: ```python # 员工数据字典,格式为 姓名: (行驶里程, 消耗汽油) data = { '王昭君': (212, 56), '貂蝉': (998, 45), '西施': (1400, 100) } # 计算每个员工的总行驶里程和总使用汽油数 total_mileage = {} total_gas = {} for name, (mileage, gas) in data.items(): if name not in total_mileage: total_mileage[name] = 0 total_gas[name] = 0 total_mileage[name] += mileage total_gas[name] += gas # 计算每个员工的平均公里数和总公里费用 average_mileage = {} total_cost = {} for name in total_mileage: average_mileage[name] = total_mileage[name] / total_gas[name] total_cost[name] = total_mileage[name] * 6.5 # 存储计算结果的字典 Ms_wang = {'name': '王昭君', 'total_km': total_mileage['王昭君'], 'total_gas': total_gas['王昭君'], 'ave_km': total_mileage['王昭君'], 'total_pay': total_cost['王昭君']} Ms_diao = {'name': '貂蝉', 'total_km': total_mileage['貂蝉'], 'total_gas': total_gas['貂蝉'], 'ave_km': total_mileage['貂蝉'], 'total_pay': total_cost['貂蝉']} Ms_xi = {'name': '西施', 'total_km': total_mileage['西施'], 'total_gas': total_gas['西施'], 'ave_km': total_mileage['西施'], 'total_pay': total_cost['西施']} people = [Ms_wang, Ms_diao, Ms_xi] # 打印员工信息 for person in people: print('姓名:{0} | 总行驶里程:{1}'.format(person['name'], person['total_km'])) # 计算并打印总计信息 total_mileage_all = sum(total_mileage.values()) total_gas_all = sum(total_gas.values()) average_mileage_all = total_mileage_all / total_gas_all total_cost_all = sum(total_cost.values()) print('总汽油消耗:{0} 平均里程:{1:.2f}'.format(total_gas_all, average_mileage_all)) print('总支出:{0:.2f}'.format(total_cost_all)) ``` 输出结果如下: ``` 姓名:王昭君 | 总行驶里程:212 姓名:貂蝉 | 总行驶里程:998 姓名:西施 | 总行驶里程:1400 总汽油消耗:201 平均里程:942.24 总支出:2507.00 ``` 希望我的回答能够帮到您!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值