Gas Station Leetcode #134 题解[C++]

22 篇文章 0 订阅
18 篇文章 0 订阅

题目来源


https://leetcode.com/problems/gas-station/description/

题目理解


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 in the clockwise direction, otherwise return -1.

Note:

If there exists a solution, it is guaranteed to be unique.
Both input arrays are non-empty and have the same length.
Each element in the input arrays is a non-negative integer.
Example 1:

Input:
gas = [1,2,3,4,5]
cost = [3,4,5,1,2]

Output: 3

Explanation:
Start at station 3 (index 3) and fill up with 4 unit of gas. Your tank = 0 + 4 = 4
Travel to station 4. Your tank = 4 - 1 + 5 = 8
Travel to station 0. Your tank = 8 - 2 + 1 = 7
Travel to station 1. Your tank = 7 - 3 + 2 = 6
Travel to station 2. Your tank = 6 - 4 + 3 = 5
Travel to station 3. The cost is 5. Your gas is just enough to travel back to station 3.
Therefore, return 3 as the starting index.

Example 2:

Input:
gas = [2,3,4]
cost = [3,4,3]

Output: -1

Explanation:
You can’t start at station 0 or 1, as there is not enough gas to travel to the next station.
Let’s start at station 2 and fill up with 4 unit of gas. Your tank = 0 + 4 = 4
Travel to station 0. Your tank = 4 - 3 + 2 = 3
Travel to station 1. Your tank = 3 - 3 + 3 = 3
You cannot travel back to station 2, as it requires 4 unit of gas but you only have 3.
Therefore, you can’t travel around the circuit once no matter where you start.

给出两个数组, 一个代表一加油站里所拥有的油量, 另一个代表从该加油站去往它紧接着的下一个加油站所需要的油量.

现在, 假定你有一辆容积无限但初始油量为 0 的油罐车. 问是否存在这样一个加油站, 你从这个加油站出发, 能否完成一次顺时针方向的环游?

注意: 环游是指还要回到出发点, 题目给出的数据如果有解那么便只有一个解.

解题思路


在着手做这道题之前, 仔细思考不难发现这两个显然的规律:

  1. 如果从加油站 A 出发能到达加油站 C 但无法到达加油站 D, 那么从 A 到 C中的任意一个加油站 B 出发, 更不可能到达加油站 D.
  2. 如果所有加油站的油量之和大于所有路程消耗的油量之和, 那么问题必然有解.

第一个规律是很显然的, 因为从A到B时油罐车里所剩的油量会大于等于0, 必然优于单纯从B出发时百分百开一辆空车出门.

我们来仔细论证第二个规律的正确性. 首先想象一个圈, 圈上点的数量与加油站的数量相同. 但每个点代表的值我们用 加油站所拥有的油量 - 到下一个加油站所需要消耗的油量 这个差值来替代.

不失一般性, 我们可以认为这些点中有正有负. 那么, 对于任意一段连续为正的圆弧, 从它的一端出发必然是可以走完这一段圆弧的. 因此, 我们不妨将含有多个连续为正值的点的圆弧合并成一个点. 这个新出现的点的值即是这些被合并的点的值之和.

接下来, 我们考虑一正一负(顺时针)相邻的两个点. 假如正值大于负值, 那么也合并这两个点, 得到一个新的正值稍小的点, 这意味着油罐车能够顺利而连续的通过这两个点. 不断重复这一操作, 先抛出结论:

最后一定能合并成一个值大于等于 0 的点, 它的值即是所有加油站油量与所有路程消耗的油量之差.

若不然, 则在某一时刻, 对于所有的正值点, 都找不到与它(顺时针)相邻的绝对值小于它的负值点. 就会有负值点之和大于正值点之和, 与规律二给出的前提条件矛盾.

证毕.

根据以上两个规律, 我们便能很容易的写出代码了.

代码实现


#include <iostream>
#include <vector>
using namespace std;
class Solution {
public:
int canCompleteCircuit(vector<int>& gas, vector<int>& cost) {
    int count,start, total = 0;
    int sum = 0;
    for (start=count = 0; count < gas.size(); count++) {
        total += gas[count] - cost[count];
        sum += gas[count] - cost[count];
        if (sum < 0) {
            sum = 0;
            start = count + 1;
        }
    }
    return total >= 0 ? start : -1;

}
};

ac_4

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值