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. 浅谈JavaScript中的this

    引言 JavaScript 是一种脚本语言,因此被很多人认为是简单易学的.然而情况恰恰相反,JavaScript 支持函数式编程.闭包.基于原型的继承等高级功能.本文仅采撷其中的一例:JavaScri ...

  2. 7 天玩转 ASP.NET MVC — 第 1 天

    0. 前言正如标题「7 天玩儿转 ASP.NET MVC」所言,这是个系列文章,所以将会向大家陆续推出 7 篇.设想一下,一天一篇,你将从一个愉快的周一开始阅读,然后在周末成为一个 ASP.NET M ...

  3. Java 中JOptionPane的基本使用方法

    JOptionPane 有助于方便地弹出要求用户提供值或向其发出通知的标准对话框.但是有时候看看API也特别烦,因为方法多,参数多,特别难记忆.这里我给出几种常用的方法供大家参考. (1) publi ...

  4. oracle数据库启动

    遇到个白痴问题,放假停电,回来时启动数据库,发现无法进入oracle管理员界面. 如下输入,但是显示的命令无效. [oracle@crm001 database]$ sqlplus / as sysd ...

  5. Spark之集群搭建

    注意,这种安装方式是集群方式:然后有常用两种运行模式: standalone , on yarn 区别就是在编写 standalone 与 onyarn 的程序时的配置不一样,具体请参照spar2中的 ...

  6. 利用loadrunner代理方式,录制手机APP脚本

    利用loadrunner代理方式录制手机(iPhone.android)应用程序HTTP脚本 工具/原料 loadrunner 智能手机 方法/步骤   利用笔记本网卡或者类似360随身wifi,在安 ...

  7. springMVC-1

    1.springMVC请求由前端到后端的流程 2.配置过程 (1)需要的jar包 spring-aop.jar spring-beans.jar spring-context.jar spring-c ...

  8. springmvc之log4j

    1.工程结构 2.所需jar包 3.web.xml <?xml version="1.0" encoding="UTF-8"?> <web-a ...

  9. windows和linux文件共享

    ###Samba安装     [root@samba ~]# yum install -y samba*     [root@samba ~]# rpm -qa | grep samba ###开启s ...

  10. CMake Error: your CXX compiler: "" was not found

    [root@amax src]# cmake . -- The CXX compiler identification is unknown CMake Error at /usr/local/sha ...