[抄道题] 加油站
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.
Note:
The solution is guaranteed to be unique.
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.
Note:
The solution is guaranteed to be unique.
环形公路上有N座加油站,第i座加油站储有gas[i]升油。
你有一辆油箱无限大的小汽车,它从第i座加油站行驶到第i+1座加油站需要耗费cost[i]升油。
假设一开始油箱内无油,请问你从第几座加油站出发可以绕环形公路一圈?
如果能,返回出发点的加油站序号(有多个的话取值最小的序号);如果不能,返回 -1。
提示,N大于等于1,也不需要考虑int的溢出问题。
函数原型 int CompleteCircuit( size_t n, const int gas[], const int cost[] );
如图
int gas[] = { 5, 2, 6, 3 };
int cst[] = { 4, 4, 4, 4 };
从第0座加油站出发的话,将会在到达第2座加油站前燃油耗尽;
从第1座加油站出发的话,将会在到达第2座加油站前燃油耗尽;
从第2座加油站出发的话,能绕一圈顺利回到出发点;
从第3座加油站出发的话,将会在到达第0座加油站前燃油耗尽;
所以应该返回 2。