Gas Station

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

You have a car with an unlimited gas tank and it costs cost[i] of gas to travel from station i to its next station (i+1). You begin the journey with an empty tank at one of the gas stations.

Return the starting gas station's index if you can travel around the circuit once, otherwise return -1.

Note:
The solution is guaranteed to be unique.

Hide Tags

Greedy

 
注意一点,如果车从0,开到i没有油了,那么我们下一个尝试的点应该为i+1。
因为在0点加了油,开到1时必定有剩余,实际上对于后面的点来说,初始油量是多了的
所以如果从0开始都到不了i,说明0到i之间的任何点都到不了。
 
 class Solution {
public:
int canCompleteCircuit(vector<int> &gas, vector<int> &cost) { int total_oil=;
int index=;
int n=gas.size(); for(int i=;i<n;i++)
{
total_oil+=gas[i];
total_oil-=cost[i]; if(total_oil<)
{
total_oil=;
index=i+;
}
} //如果循环到了最后一个,没了油,说明任何点都没有希望到达了
if(index==n)
{
return -;
} //如果一直循环到最后,仍然有油,说明从0点可以遍历整个路径
if(index==)
{
return ;
} //如果开到最后还剩油,我们继续往后计算。
for(int i=;i<n;i++)
{
total_oil+=gas[i];
total_oil-=cost[i]; //又回到了初始点,则说明从该点可以遍历整个路径
if(i==index)
{
return i;
} //如果在中间任何一点没有了油,就不必再尝试了
if(total_oil<)
{
break;
}
} return -;
}
};

【leetcode】Gas Station的更多相关文章

  1. 【LeetCode】Gas Station 解题报告

    [LeetCode]Gas Station 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/gas-station/#/descr ...

  2. 【Leetcode】【Medium】Gas Station

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

  3. 【LeetCode】贪心 greedy(共38题)

    [44]Wildcard Matching [45]Jump Game II (2018年11月28日,算法群衍生题) 题目背景和 55 一样的,问我能到达最后一个index的话,最少走几步. 题解: ...

  4. 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java

    [LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...

  5. 【Leetcode】Pascal&#39;s Triangle II

    Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...

  6. 53. Maximum Subarray【leetcode】

    53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...

  7. 27. Remove Element【leetcode】

    27. Remove Element[leetcode] Given an array and a value, remove all instances of that value in place ...

  8. 【刷题】【LeetCode】007-整数反转-easy

    [刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接-空 007-整数反转 方法: 弹出和推入数字 & 溢出前进行检查 思路: 我们可以一次构建反转整数的一位 ...

  9. 【刷题】【LeetCode】000-十大经典排序算法

    [刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接 000-十大经典排序算法

随机推荐

  1. DNS域传送漏洞利用

    DNS区域传送(DNS zone transfer)指的是一台备用服务器使用来自主服务器的数据刷新自己的域(zone)数据库.这为运行中的DNS服务提供了一定的冗余度,其目的是为了防止主的域名服务器因 ...

  2. android控件库(1)-带删除功能的EditText

    DJEditText.java /** * Created by xp.chen on 2016/11/25. */ public class DJEditText extends AppCompat ...

  3. Java byte位移操作 注意事项

    Java对byte 的 + - * / >> >>> << & | ^ (加,减,乘,除,右移,左移,无符号右移,位与,位或,位异或)操作,均会是首先 ...

  4. 基本linux命令

    1.mkdir mkdir 创建目录   mkdir -p 循环创建目录 2.cd     切换目录 3.pwd   查看当前路径 4.mkdir 删除一个空的目录 5.cp 复制文件/目录  -r用 ...

  5. $(document).ready(){}、$(fucntion(){})、(function(){})(jQuery)onload()的区别

     1.首先说JQuery的几个写法  $(function(){     //do someting   });   $(document).ready(function(){     //do so ...

  6. java之google style

    Google的Java编码规范英文版: http://google-styleguide.googlecode.com/svn/trunk/javaguide.html Google的Java编码规范 ...

  7. Java转json

    一.json介绍 1. 作用:JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式,是存储和交换文本信息的语法. 2.json以key-value的格式书写,数 ...

  8. thinkphp中的自动验证

    array(验证字段,验证规则,错误提示,[验证条件,附加规则,验证时间]) 1.验证字段 需要验证的表单字段名称,这个字段不一定是数据库字段,也可以是表单的一些辅助字段,例如确认密码和验证码等等.有 ...

  9. static 的使用

    static用法小结 转自 http://blog.csdn.net/Kendiv/article/details/675941 在C语言中,static的字面意思很容易把我们导入歧途,其实它的作用有 ...

  10. Try-Catch机制使用场景分析

    (一)在什么场景下加Try-Catch机制   1)以业务逻辑功能为单位,在最上层加Try-Catch机制.为什么要这样做呢?这主要是增加程序的健壮性,防止因抛出异常过多,导致程序崩溃. try { ...