因为gas的总数大于cost总时间。你将能够圈住整个城市。

第一溶液:

如果一開始有足够的油。从位置i出发。到位置k时剩余的油量为L(i,k)。

对随意的k。L(i,k)依据i的不同,仅仅相差常数。

我们仅仅须要找到最小的L(0, k)相应的k,k+1为所求。

代码例如以下:

    int canCompleteCircuit(vector<int> &gas, vector<int> &cost) {
int start = 0;
int curGas = 0, minGas = 0, totalGas = 0;
for (int i = 0; i < gas.size(); i++)
{
int temp = gas[i] - cost[i];
curGas += temp;
totalGas += temp;
if (minGas > curGas)
{
start = i + 1;
minGas = curGas;
}
}
if (totalGas >= 0) return start % gas.size();
else return -1;
}

另外一种解法:

假设L(i,k) < 0,则从i和k之间全部的位置都不能到k

所以从k+1的位置从0開始找

    int canCompleteCircuit(vector<int> &gas, vector<int> &cost) {
int start = 0;
int curGas = 0, totalGas = 0;
for (int i = 0; i < gas.size(); i++)
{
int temp = gas[i] - cost[i];
curGas += temp;
totalGas += temp;
if (curGas < 0)
{
start = i + 1;
curGas = 0;
}
}
if (totalGas >= 0) return start % gas.size();
else return -1;
}

版权声明:本文博客原创文章,博客,未经同意,不得转载。

Gas Station [leetcode] 两个解决方案的更多相关文章

  1. Gas Station leetcode java

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

  2. 134. Gas Station leetcode

    134. Gas Station 不会做. 1. 朴素的想法,就是针对每个位置判断一下,然后返回合法的位置,复杂度O(n^2),显然会超时. 把这道题转化一下吧,求哪些加油站不能走完一圈回到自己,要求 ...

  3. Gas Station [LeetCode]

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

  4. Gas Station——LeetCode

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

  5. Gas Station|leetcode 贪心

    贪心:尽量将gas[i]-cost[i]>0的放在前面,gas[i]-cost[i]<0的放在后面.(路程的前面有汽油剩下,耗汽油的放在路程的后面). 能否全程通过的 条件 是:sum(g ...

  6. leetcode@ [134] Gas station (Dynamic Programming)

    https://leetcode.com/problems/gas-station/ 题目: There are N gas stations along a circular route, wher ...

  7. [LeetCode] 134. Gas Station 解题思路

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

  8. [LeetCode] Minimize Max Distance to Gas Station 最小化去加油站的最大距离

    On a horizontal number line, we have gas stations at positions stations[0], stations[1], ..., statio ...

  9. [LeetCode] Gas Station 加油站问题

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

随机推荐

  1. IE 加速插件之 Google Chrome Frame

    前言 IE 8 及以下版本的速度较慢. 特别是前端的js 和 css 内容较多时尤为突出. 就笔者的开发经验来说GWT, Ext JS, raphael , draw2d 等开发的系统在IE下使用是相 ...

  2. zoj 2972 - Hurdles of 110m

    题目:110米栏,运动员能够用三种状态跑,1状态耗体力且跑得快,2状态不消耗体力,3状态恢复体力且跑得慢. 体力上限是M,且初始满体力,如今想知到最小的时间跑全然程. 分析:dp,全然背包.题目是一个 ...

  3. C strstr() 函数

    包含文件:string.h 函数名: strstr 函数原型:extern char *strstr(const char *str1, const char *str2); 语法:* strstr( ...

  4. 具体解释java定时任务

    在我们编程过程中假设须要运行一些简单的定时任务,无须做复杂的控制.我们能够考虑使用JDK中的Timer定时任务来实现. 以下LZ就其原理.实例以及Timer缺陷三个方面来解析java Timer定时器 ...

  5. Volley该框架使用了大量的请求图片

    尊重原创 http://write.blog.csdn.net/postedit/26142025 代码下载:http://download.csdn.net/detail/yuanzeyao2008 ...

  6. 设计模式六大原则-OCP

    开闭原则(Open Closed Principle)是Java世界里最基础的设计原则,它指导我们如何建立一个稳定的.灵活的系统. 定义: 一个软件实体如类.模块和函数应该对扩展开放,对修改关闭. S ...

  7. HDU 4380 Farmer Greedy 计算几何+bitset

    枚举直线,对于直线的某个点在直线的左端还是右端,能够状压出一个数.用bitset记录. 然后三角形就是3个bitset&一下 #include <cstdio> #include ...

  8. windows phone (14) 简单了解Ellipse元素和Rectangle元素

    原文:windows phone (14) 简单了解Ellipse元素和Rectangle元素  System.Windows.Shapes命名空间中包含了显示矢量图形的元素分别为ellipse和re ...

  9. iOS国际化和genstrings所有子文件夹本地化字符串

    iOS国际化和genstrings所有子文件夹本地化字符串 在最近的一个繁忙的对外工程.每天加班.没有时间更新博客.简单谈一下知识的国际化. 首先,我们使用串.必须NSLocalizedString( ...

  10. 编辑简单的 shell程序

    编辑简单的 shell程序 知道了vi编辑器的使用规则之后,结合shell的使用规则,可以编辑简单的 shell程序试试手 题目如下: 1.用while语句创建一个根据输入的数值求累加和(1+2+3+ ...