class Solution {
public:
inline int get_next(int idx, int size)
{
return idx == size- ? : idx+;
} int aux(int idx, vector<int>& gas, vector<int>& cost)
{
int i = idx;
int left = gas[i] - cost[i];
if(left < )
return -;
i = get_next(i, gas.size());
while( i!= idx)
{
left = left + gas[i] - cost[i];
if(left < )
return -;
i = get_next(i, gas.size());
}
return idx;
} int canCompleteCircuit(vector<int>& gas, vector<int>& cost) {
int ret;
int i = ;
for(; i<gas.size(); ++i)
{
if(gas[i] >= cost[i])
{
ret = aux(i, gas, cost);
cout << ret << endl;
if(ret == -)
continue;
else
break;
}
}
return ret;
}
};

补充一个python的实现:

 class Solution:
def canCompleteCircuit(self, gas: 'List[int]', cost: 'List[int]') -> int:
n = len(gas)
total =
sums =
begin =
for i in range(n):
diff = gas[i] - cost[i]
total += diff
sums += diff
if sums < :
begin = i +
sums =
if total < :
return -
return begin

题目确保,如果存在解,是唯一的解,因此程序只需要找出可以完成全程的第一个起点就行。

因为全程的加油数量和耗油数量都是固定的,因此从哪个点计算全程的总的消耗都是一样的。不需要先找到起点,然后再进行计算。

因此可以在一次循环中,完成两种计算:1判断是否可以走完全程(total是否小于0),2寻找第一个可以作为起点的坐标(begin)。

leetcode134的更多相关文章

  1. [Swift]LeetCode134. 加油站 | Gas Station

    There are N gas stations along a circular route, where the amount of gas at station i is gas[i]. You ...

  2. LeetCode134:Gas Station

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

  3. leetcode134 Gas Station

    思路: https://leetcode.com/problems/gas-station/discuss/269604/Java-Greedy-thought-process 关键是要想清楚如果从加 ...

  4. Leetcode134. Gas Station加油站

    在一条环路上有 N 个加油站,其中第 i 个加油站有汽油 gas[i] 升. 你有一辆油箱容量无限的的汽车,从第 i 个加油站开往第 i+1 个加油站需要消耗汽油 cost[i] 升.你从其中的一个加 ...

  5. leetcode134:3sum

    题目描述 给出一个有n个元素的数组S,S中是否有元素a,b,c满足a+b+c=0?找出数组S中所有满足条件的三元组. 注意: 三元组(a.b.c)中的元素必须按非降序排列.(即a≤b≤c) 解集中不能 ...

随机推荐

  1. Lists.transform的使用

    转自:https://blog.csdn.net/weixin_42201566/article/details/81513769 Lists.transform:能够轻松的从一种类型的list转换为 ...

  2. Struts2重新学习之自定义拦截器(判断用户是否是登录状态)

    拦截器 一:1:概念:Interceptor拦截器类似于我们学习过的过滤器,是可以再action执行前后执行的代码.是web开发时,常用的技术.比如,权限控制,日志记录. 2:多个拦截器Interce ...

  3. test20180907 day1

    T1 256MB,1Sec T2 512MB,3Sec T3 512MB,1Sec 总分:150 试题一 餐馆 题目背景 铜企鹅是企鹅餐馆的老板,他正在计划如何使得自己本年度收益增加. 题目描述 共有 ...

  4. windows 版 nginx 运行错误的一些解决方法

    1. 关于文件夹的中文的问题. 错误的截图如下: 看得到这个 failed (1113: No mapping for the Unicode character exists in the targ ...

  5. 清理IE和使用历史痕迹

    清除IE临时文件的Batch脚本 @echo off title: IE temporary file deleter echo 正在 清除Internet临时文件 ............ RunD ...

  6. 01.ubuntu14.04安装HI3518EV200 SDK的过程

    转载,侵删 1.海思SDK安装编译 Hi3518EV200_SDK是基于Hi3518EV200_DMEB的软件开发包,包含了在Linux相关应用开发时使用的各种工具及其源代码,是用户开发中最基本的软件 ...

  7. Linux下远程备份、上传工程,重启服务器

    Linux下远程备份.上传工程,重启服务器 Linux服务器实现远程,原项目的备份.删除,新项目上传,以及远程重启服务器!分成一个主shell调用三个shell文件步骤完成.mainsh.sh一次按顺 ...

  8. actor model vs tasked based parallizm

    举例子:计算pi actor model概念:一般有n个actor(task),和一个调度线程(本身也是一个actor)调度线程负责向每个task发送命令执行计算,以及接收每个task的结果并归并到一 ...

  9. 【转】每天一个linux命令(7):mv命令

    原文网址:http://www.cnblogs.com/peida/archive/2012/10/27/2743022.html mv命令是move的缩写,可以用来移动文件或者将文件改名(move  ...

  10. md5,base64,rsa

    MD5功能:    输入任意长度的信息,经过处理,输出为128位的信息(数字指纹):    不同的输入得到的不同的结果(唯一性):    根据128位的输出结果不可能反推出输入的信息(不可逆): 1. ...