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. maven test 运行 指定类或方法 打包 mvn clean assembly:assembly

    >mvn test -Dtest=[ClassName] 运行测试类中指定的方法:(这个需要maven-surefire-plugin:2.7.3以上版本才能支持)   >mvn test ...

  2. Python基础之【第三篇】

    dir(): 默认打印当前模块的所有属性,如果传一个对象参数则打印当前对象的变量名 vars() 默认打印当前模块的所有属性,如果传一个对象参数则打印当前对象的变量名和值 reload() 将以前导入 ...

  3. Android Studio-设置代码自动提示

    None:代表模糊匹配(推荐). First Letter: 根据首字母进行匹配. All:与First Letter类似,不过匹配结果比First Letter多.

  4. netstat miscellaneousness

    netstat -a (--all) : show both listening and non-listening sockets 默认是不显示正在侦听的进程,只显示已经established的 n ...

  5. [译]git rebase -i

    使用rebase -i会在终端出现一个交互页面. 在这个交互页面中我们可以对要rebase的commit做一定的修改. 用法 git rebase -i <master> 把当前的分支的c ...

  6. 此请求的查询字符串的长度超过配置的 maxQueryStringLength 值 --不仅wen.fonfig一个地方需要设置

    提示已经很明确了... 搜出来的都是: <system.webServer> <security> <requestFiltering> <requestLi ...

  7. List<List<double>> lsls = null; 根据double值来重新排序lsls...

    "确定:Node-data = (7,2).具体是:根据x维上的值将数据排序, 6个数据的中值(所谓中值,即中间大小的值)为7, 所以Node-data域位数据点(,).这样, 该节点的分割 ...

  8. 清北学堂模拟day4 捡金币

    [问题描述]小空正在玩一个叫做捡金币的游戏.游戏在一个被划分成 n行 n列的网格状场地中进行.每一个格子中都放着若干金币,并且金币的数量会随着时间而不断变化. 小空的任务就是在网格中移动,拾取尽量多的 ...

  9. 清北学堂模拟day4 传球接力

    [问题描述]n 个小朋友在玩传球. 小朋友们用 1 到 n 的正整数编号. 每个小朋友有一个固定的传球对象,第 i 个小朋友在接到球后会将球传给第 ai个小朋友, 并且第 i 个小朋友与第 ai个小朋 ...

  10. 调用shell脚本,IP处理

    //调用shell脚本,IP处理 package com.letv.sdns.web.utils; import org.slf4j.Logger; import org.slf4j.LoggerFa ...