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. UVa 10410 树重建

    Tree Reconstruction Problem Description You have just finished a compiler design homework question w ...

  2. gitlab操作

    一.初始设置 在某一个具体的project下: 1.gitlab中删除一个工程Setting-->General-->Advanced settings-->RemoveProjec ...

  3. cocos2dx学习资料

    [9秒原创]cocos2d-x——CCScrollView用法 http://www.9miao.com/thread-45619-1-1.html [9秒原创]cocos2d-x——场景切换效果示例 ...

  4. Arrays、ArrayUtils 区别

    Arrays java.util 包提供的静态类:java.util.Arrays 此静态类专门用来操作array ,提供搜索.排序.复制等静态方法. ArrayUtils apache 提供的类:o ...

  5. tomcat和servlet的关系

    一.什么是servlet? 处理请求和发送响应的过程是由一种叫做Servlet的程序来完成的,并且Servlet是为了解决实现动态页面而衍生的东西.理解这个的前提是了解一些http协议的东西,并且知道 ...

  6. CentOS7 安装mysql(YUM方式)

    1.下载mysql源安装包 shell> wget http://dev.mysql.com/get/mysql57-community-release-el7-8.noarch.rpm   2 ...

  7. Hyperic Sigar API 举例

    Hyperic HQ 是什么? Hyperic HQ 是一个开源的(General Public License,GPL授权)IT资源管理框架,让用户使用统一的界面来管理各种不同的IT资源的管理,Hy ...

  8. 敏感词文本文件 filtered_words.txt,里面的内容为以下内容,当用户输入敏感词语时,则打印出 Freedom,否则打印出 Human Rights

    敏感词文件内容: 代码: def filtered_words(path='filtered_words.txt'): words = [] with open(path, 'r', encoding ...

  9. 一些css杂项笔记

    div[class*="col-"]{ background-color: gold; border: 1px solid #ccc; } //给class开头等于col-的div ...

  10. python selenium-2 定位元素

    元素 方法 示例 id find_element_by_id('su') driver.get("http://www.baidu.com")driver.find_element ...