因为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. DataTable数据转换为实体

    我们在用三层架构编写软件时,常常会遇到例如以下问题,就是三层之间的參数传递问题:假设我们在D层查询出数据是DataTable类型的,那么我们在B层甚至U层使用这条数据时,就要用DataTable类型来 ...

  2. OC -- 第一个类

    OC -- 第一个类 类名:Car 属性:轮胎个数.时速 行为:跑 完整写一个类:类的声明和实现 1.    类的声明 代码: // NSObject 再Foundation框架中 #import & ...

  3. COST CUTTING THE ALAN GREENBERG WAY

    AnatBird.com COST CUTTING THE ALAN GREENBERG WAY

  4. Linux C 编程内存泄露检測工具(二):memwatch

    Memwatch简单介绍 在三种检測工具其中,设置最简单的算是memwatch,和dmalloc一样,它能检測未释放的内存.同一段内存被释放多次.位址存取错误及不当使用未分配之内存区域.请往http: ...

  5. 服务器编程入门(11)TCP并发回射服务器实现 - 单线程select实现

    问题聚焦: 当客户端阻塞于从标准输入接收数据时,将读取不到别的途径发过来的必要信息,如TCP发过来的FIN标志. 因此,进程需要内核一旦发现进程指定的一个或多个IO条件就绪(即输入已准备好被读取,或者 ...

  6. SlidingMenu开源项目滑动界面的实现总结

    先上图 须要准备的是先得在GitHub上下载ActionBarSherlock-master.zip,和SlidingMenu-master.zip这两个开源文件,然后解压这两个包,SlidingMe ...

  7. ArcGIS For Flex给定两个

    1.错误叙述性说明 2.错误原因 3.解决方案 版权声明:本文博客原创文章,博客,未经同意,不得转载.

  8. Learning Cocos2d-x for WP8(1)——创建首个项目

    原文:Learning Cocos2d-x for WP8(1)--创建首个项目 Cocos2d-x for WP8开发语言是C++,系列文章将参考兄弟篇Learning Cocos2d-x for ...

  9. SWT的CheckBoxTreeView的上级菜单与下级菜单的选中的实现

    是不是很神奇? treeViewer.addCheckStateListener(new ICheckStateListener() { @Override public void checkStat ...

  10. 《Linux设备驱动开发具体解释(第3版)》进展同步更新

    本博实时更新<Linux设备驱动开发具体解释(第3版)>的最新进展. 2015.2.26 差点儿完毕初稿. 本书已经rebase到开发中的Linux 4.0内核,案例多数基于多核CORTE ...