【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-十大经典排序算法
随机推荐
- MFS文件系统
一.MFS文件系统概论 MFS是linux下的开源存储系统,是由波兰人开发的.MFS文件系统能够实现RAID的功能,不但能够节约存储成本,而且不逊于专业的存储系统,能够实现在线扩展.MFS是一种半分布 ...
- MVC缓存OutPutCache学习笔记 (一) 参数配置
OutPutCache 参数详解 Duration : 缓存时间,以秒为单位,这个除非你的Location=None,可以不添加此属性,其余时候都是必须的. Location : 缓存放置的位置; 该 ...
- APACHE POI教程 --java应用程序用POI与Excel交互
POI报表 --用POI与Excel交互 AURISOFT 第一章 POI简介 --Jakata Poi HSSF:纯java的Excel解决方案 在我们实际的开发中,表现层的解决方案虽然有多样,但是 ...
- fopen()和fclose()的用法
fopen()和fclose()的用法 1.fopen()函数的用法 fopen函数用于打开文件, 其调用格式为: FILE *fopen(char *filename, *type); fopen( ...
- php 遍历目录下的所以文件和文件夹
<?php/** * 遍历文件夹和文件列 * @author lizhiming * @date 2016/06/30 */define('DS', DIRECTORY_SEPARATOR); ...
- php从零开始
吐槽:今天开始撸PHP了,从此前端少了个小白,PHP多了个小白... 本白从3年前陆陆续续开始一会儿撸会儿PHP一会儿撸前端.前端撸的比较多,PHP撸的比较少,当然本白撸php大多都是被逼的!! 然后 ...
- [译]ES6箭头函数和它的作用域
原文来自我的前端博客: http://www.hacke2.cn/arrow-functions-and-their-scope/ 在ES6很多很棒的新特性中, 箭头函数 (或者大箭头函数)就是其中值 ...
- [译]Mongoose指南 - 查询
查询有带callback和不带callback两种方式 所有mongoose的callback都是这种格式: callback(err, result) var Person = mongoose.m ...
- HDOJ 4717 The Moving Points
The Moving Points Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...
- CodeForces 55D Beautiful numbers
D. Beautiful numbers time limit per test 4 seconds memory limit per test 256 megabytes input standar ...