加油站问题 Gas Station

2019-06-01 17:09:30

问题描述:

问题求解:

其实本题本质上是一个数学题。

【定理】

对于一个循环数组,如果这个数组整体和 SUM >= 0,那么必然可以在数组中找到这么一个元素:从这个数组元素出发,绕数组一圈,能保证累加和一直是出于非负状态。

【证明】

从第一个数字开始进行累加和,中间必然会有一个累加和最低的点,我们设为x。最后的sum >= 0。

现在我们从x出发向后累加,那么必然处于非负状态,并且到了最后一个站点还会有一定的盈余,再从开始向最低点x进发也必然不会出现负数的情况。

【Leetcode Discuss】

If sum of all gas[i]-cost[i] is greater than or equal to 0, then there is a start position you can travel the whole circle.
Let i be the index such that the the partial sum

gas[0]-cost[0]+gas[1]-cost[1]+...+gas[i]-cost[i]

is the smallest, then the start position should be start=i+1 ( start=0 if i=n-1). Consider any other partial sum, for example,

gas[0]-cost[0]+gas[1]-cost[1]+...+gas[i]-cost[i]+gas[i+1]-cost[i+1]

Since gas[0]-cost[0]+gas[1]-cost[1]+...+gas[i]-cost[i] is the smallest, we must have

gas[i+1]-cost[i+1]>=0

in order for gas[0]-cost[0]+gas[1]-cost[1]+...+gas[i]-cost[i]+gas[i+1]-cost[i+1] to be greater.
The same reasoning gives that

 gas[i+1]-cost[i+1]>=0
 gas[i+1]-cost[i+1]+gas[i+2]-cost[i+2]>=0 ....... gas[i+1]-cost[i+1]+gas[i+2]-cost[i+2]+...+gas[n-1]-cost[n-1]>=0 

What about for the partial sums that wraps around?

gas[0]-cost[0]+gas[1]-cost[1]+...+gas[j]-cost[j] + gas[i+1]-cost[i+1]+...+gas[n-1]-cost[n-1] >= gas[0]-cost[0]+gas[1]-cost[1]+...+gas[i]-cost[i] + gas[i+1]-cost[i+1]+...+gas[n-1]-cost[n-1] >=0

The last inequality is due to the assumption that the entire sum of gas[k]-cost[k] is greater than or equal to 0.
So we have that all the partial sums

gas[i+1]-cost[i+1]>=0,
gas[i+1]-cost[i+1]+gas[i+2]-cost[i+2]>=0, gas[i+1]-cost[i+1]+gas[i+2]-cost[i+2]+...+gas[n-1]-cost[n-1]>=0, ... gas[i+1]-cost[i+1]+...+gas[n-1]-cost[n-1] + gas[0]-cost[0]+gas[1]-cost[1]+...+gas[j]-cost[j]>=0, ...

Thus i+1 is the position to start.

因此,对于本题来说,我们可以计算一下是否总的gas - cost >= 0?如果是,那么必然存在一个解,在这个前提下,只需要遍历一遍数组,如果碰到不能到达的情况,那么就从第一个不能到达的重新开始计算即可。

    public int canCompleteCircuit(int[] gas, int[] cost) {
        int n = gas.length;
        int sum = 0;
        for (int i = 0; i < n; i++) sum += gas[i] - cost[i];
        if (sum < 0) return -1;
        int start = 0;
        int tank = 0;
        for (int i = 0; i < n; i++) {
            tank += gas[i];
            if (tank < cost[i]) {
                start = i + 1;
                tank = 0;
            }
            else {
                tank -= cost[i];
            }
        }
        return start;
    }

  

 

转载于:https://www.cnblogs.com/hyserendipity/p/10960312.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
加油站问题是一个经典的贪心算法问题,可以使用贪心算法得到最优解。 问题描述如下:有一辆油箱容量为C的汽车从起点出发,需要行驶距离为D的路程,沿途有n个加油站,第i个加油站距离起点的距离为di,加油站提供的油量为vi,汽车每行驶1个单位距离需要消耗1单位油量。假设汽车初始油量为0,且每个加油站油量无限,问汽车是否可以到达终点。 解题思路:对于每个加油站,我们需要判断是否需要在该加油站加油,如果需要,在该加油站加足最少的油量,以保证到达下一个加油站时还有足够的油量。如果在某个加油站加不了足够的油量,那么汽车无法到达终点。在每个加油站加油的油量可以使用贪心策略,即在当前所有能够到达的加油站中,选择可以加最多油量的加油站加油。 以下是Python代码实现: ```python def gas_station(C, D, n, d, v): # 记录当前油量和已走的距离 cur_gas = 0 cur_dis = 0 # 记录加油次数 count = 0 # 当前能够到达的加油站 stations = [] for i in range(n): # 计算当前加油站距离和起点的距离 dis = d[i] - cur_dis # 如果当前油量不足以到达该加油站 while cur_gas < dis: # 如果没有加油站可以到达,返回False if not stations: return False # 在所有能够到达的加油站中,选择可以加最多油量的加油站加油 max_gas = max(stations) cur_gas += max_gas # 记录加油次数 count += 1 # 更新当前能够到达的加油站 stations.remove(max_gas) # 更新当前油量和已走的距离 cur_gas -= dis cur_dis = d[i] # 将该加油站加入当前能够到达的加油站列表中 stations.append(v[i]) return count ``` 其中,C表示油箱容量,D表示需要行驶的距离,n表示加油站数量,d和v分别表示每个加油站距离起点的距离和提供的油量。函数返回加油次数,如果无法到达终点则返回False。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值