【leetcode】Gas Station
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.
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的更多相关文章
- 【LeetCode】Gas Station 解题报告
[LeetCode]Gas Station 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/gas-station/#/descr ...
- 【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 ...
- 【LeetCode】贪心 greedy(共38题)
[44]Wildcard Matching [45]Jump Game II (2018年11月28日,算法群衍生题) 题目背景和 55 一样的,问我能到达最后一个index的话,最少走几步. 题解: ...
- 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java
[LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...
- 【Leetcode】Pascal's Triangle II
Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...
- 53. Maximum Subarray【leetcode】
53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...
- 27. Remove Element【leetcode】
27. Remove Element[leetcode] Given an array and a value, remove all instances of that value in place ...
- 【刷题】【LeetCode】007-整数反转-easy
[刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接-空 007-整数反转 方法: 弹出和推入数字 & 溢出前进行检查 思路: 我们可以一次构建反转整数的一位 ...
- 【刷题】【LeetCode】000-十大经典排序算法
[刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接 000-十大经典排序算法
随机推荐
- js中,还真不了解 console
参考链接: https://segmentfault.com/a/1190000000481884
- 浅谈JavaScript中的定时器
引言 使用setTimeout()和setInterval()创建的定时器可以实现很多有意思的功能.很多人认为定时器是一个单独的线程(之前我也是),但是JavaScript是运行在单线程环境中的,而定 ...
- Java多线程编程核心技术---线程间通信(一)
线程是操作系统中独立的个体,但这些个体如果不经过特殊处理就不能成为一个整体.线程间的通信就是成为整体的必用方案之一.线程间通信可以使系统之间的交互性更强大,在大大提高CPU利用率的同时还会使程序员对各 ...
- Android Studio-设置代码自动提示
None:代表模糊匹配(推荐). First Letter: 根据首字母进行匹配. All:与First Letter类似,不过匹配结果比First Letter多.
- 【8-17】HTML测试
Source : http://www.w3school.com.cn/html HTML 测验 结果:9/20 您的回答: 1.HTML 指的是? 您的回答:超文本标记语言(Hyper Text M ...
- System.exit(0)和System.exit(1)区别
System.exit(0)是将你的整个虚拟机里的内容都停掉了 ,而dispose()只是关闭这个窗口,但是并没有停止整个application exit() .无论如何,内存都释放了!也就是说连JV ...
- linux下使用yum安装mysql
1. 安装mysql 服务器端: yum install mysql-server yum install mysql-devel 2. 安装mysql客户 ...
- php开发工具之火狐浏览器插件
相信做开发的都有一种火狐情怀吧! 下面来介绍下一些自己在php开发工程中用到几个火狐浏览器插件. 1.[firebug]: 这个插件可以说是一个神奇,功能不用过对介绍. 2.[hostAdmin]: ...
- CF467C George and Job (DP)
Codeforces Round #267 (Div. 2) C. George and Job time limit per test 1 second memory limit per test ...
- php apache用户写文件夹权限设置
php一般是以apache用户身份去执行的,把apache加入到存储你文件的父文件夹属组里去,然后改该父文件夹权限为775,这样属组成员就有写的权限,而apache属于这个组就可以改写该目录下所有文件 ...