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. hdu1355

    题意:有一片矩形花生田在路的一侧,田上的整数坐标位置有0个或多个花生,现规定从路上走到田地最边上的某个格点位置.从田边上走回路上.从一个格点移动到另一个格点.采摘格点上的花生,这四种动作都要花费一单位 ...

  2. Boosting学习笔记(Adboost、GBDT、Xgboost)

    转载请注明出处:http://www.cnblogs.com/willnote/p/6801496.html 前言 本文为学习boosting时整理的笔记,全文主要包括以下几个部分: 对集成学习进行了 ...

  3. sql server insert values 多值 与oracle 的不同

    类似的语句在 oracle 中是不能执行的 insert into temp_tbl values('app'),('demo'); 但是在sql server 中是可以的 insert into c ...

  4. C#中DateTime的缺陷 ---- 代替品DateTimeOffset

    C#中的DateTime在逻辑上有个非常严重的缺陷: > var d = DateTime.Now; > var d2 = d.ToUniversalTime(); > d == d ...

  5. [C++] 一个能够定时自毁的类的实现

    试想一下, 有没有这种需求: 对于每一个新的对象, 我们希望它能够在一定时间后自动销毁, 前提是我们没有在这段时间内给它发出重置信号. 这种需求其实是有的, 比如在电影里, 主角知道了一个反派不希望被 ...

  6. 投行的码工 Dead-end job

    发信人: icestonesy (无忧), 信区: Quant 标  题: 投行的码工怎么样? 发信站: BBS 未名空间站 (Sat May 16 23:25:00 2015, 美东) 最近看到不少 ...

  7. 使用过的bug跟踪系统

    MantisBT jira

  8. osql执行数据库查询命令并保存到txt文件

    osql -Usa -P123 -d AppBox -Q "select * from Menus where sortindex > 1000" -o e:\xxx.txt ...

  9. Openwrt 3G模块的添加

    一. 在menuconfig中添加相关驱动 1. Kernel Modules -> USB Support <*> kmod-usb2 <*> kmod-usb-ohc ...

  10. TextBox(只允许输入字母或者数字)——重写控件

    实现如下: using System;using System.Collections.Generic;using System.Linq;using System.Text;using System ...