题目描述

环形路上有n个加油站,第i个加油站的汽油量是gas[i].
你有一辆车,车的油箱可以无限装汽油。从加油站i走到下一个加油站(i+1)花费的油量是cost[i],你从一个加油站出发,刚开始的时候油箱里面没有汽油。
求从哪个加油站出发可以在环形路上走一圈。返回加油站的下标,如果没有答案的话返回-1。
注意:
答案保证唯一。

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.

输入

[2,3,1],[3,1,2]

输出

1

class Solution {
public:
    /**
     *
     * @param gas int整型vector
     * @param cost int整型vector
     * @return int整型
     */
    int canCompleteCircuit(vector<int>& gas, vector<int>& cost) {
        // write code here
        int  start=gas.size()-1;
        int end=0;
        int sum=gas[start]-cost[start];
        while (start>end)
        {
            if (sum>=0){
                sum+=gas[end]-cost[end];
                ++end;
                
            }else {
                --start;
                sum+=gas[start]-cost[start];
            }
        }
        return sum>=0 ?start:-1;
        
    }
};

leetcode17gas-station的更多相关文章

  1. [LeetCode] Gas Station 加油站问题

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

  2. PAT 1072. Gas Station (30)

    A gas station has to be built at such a location that the minimum distance between the station and a ...

  3. Leetcode 134 Gas Station

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

  4. POJ 2031 Building a Space Station

    3维空间中的最小生成树....好久没碰关于图的东西了.....              Building a Space Station Time Limit: 1000MS   Memory Li ...

  5. 【leetcode】Gas Station

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

  6. [LeetCode] Gas Station

    Recording my thought on the go might be fun when I check back later, so this kinda blog has no inten ...

  7. 20. Candy && Gas Station

    Candy There are N children standing in a line. Each child is assigned a rating value. You are giving ...

  8. [ACM_搜索] POJ 1096 Space Station Shielding (搜索 + 洪泛算法Flood_Fill)

    Description Roger Wilco is in charge of the design of a low orbiting space station for the planet Ma ...

  9. Service Station - An Introduction To RESTful Services With WCF

    Learning about REST An Abstract Example Why Should You Care about REST? WCF and REST WebGetAttribute ...

  10. LeetCode——Gas Station

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

随机推荐

  1. 【题解】[SCOI]windy数

    Link 题目大意:求给定一个区间内满足每一位的数相差大于\(2\)且没有前导零的数的个数. \(\text{Solution:}\) 我们可以按照数位\(dp\).设状态为当前要\(dp\)第\(p ...

  2. 怎么写一个Activity

    a.新建一个类继承Actitvity b.重写oncreate方法 setContentView(R.layout.XXX);//设置布局文件 c.注册activity <activity an ...

  3. 入职大厂,齐姐精选的 9 道 Java 集合面试题

    Java 集合框架其实都讲过了,有一篇讲 Collection 的,有一篇讲 HashMap 的,那没有看过的小伙伴快去补下啦,文末也都有链接:看过的小伙伴,那本文就是检测学习成果的时候啦 今天这篇文 ...

  4. JVM系列【3】Class文件加载过程

    JVM系列笔记目录 虚拟机的基础概念 class文件结构 class文件加载过程 jvm内存模型 JVM常用指令 GC与调优 Class文件加载过程 JVM加载Class文件主要分3个过程:Loadi ...

  5. 使用Android进行VR图像处理

    Source code at GitHub 介绍 VR或360图像,可以在耳机或在像谷歌街景这样的网站上观看是标准的JPG图像.你可以使用简单的Android图形处理技术,通过单独的移动设备或内部运行 ...

  6. Mac Idea你不知道的秘密

    导读 工欲善其事必先利其器,日常工作中,知道这些Idea技巧,可以极大提高日常开发效率. 技巧篇 以下内容不分先后顺序 显示类中的方法 搜索 搜索方法,按两下shift 文字搜索,control+sh ...

  7. Go strconv包

    strconv包 该包主要实现基本数据类型与其字符串表示的转换. 常用函数为Atoi().Itia().parse系列.format系列.append系列. 更多函数请查看官方文档. string与i ...

  8. 推荐算法之: DeepFM及使用DeepCTR测试

    算法介绍 左边deep network,右边FM,所以叫deepFM 包含两个部分: Part1: FM(Factorization machines),因子分解机部分 在传统的一阶线性回归之上,加了 ...

  9. 如何把C++的源代码改写成C代码?而C改C++只需一步!

    ★ 如何把C++的源代码改写成C代码? C++解释器比C语言解释器占用的存储空间要大,想要在某些特定场合兼容C++代码,同时为了节省有限的存储空间,降低成本,也为了提高效率,将用C++语言写的源程序用 ...

  10. Vagrant系列(一)----win10搭建Vagrant+VirtualBox环境_

      一.Vagrant是什么?     vagrant是一个操作虚拟机的工具.是一个基于Ruby的工具,用于创建和部署虚拟化开发环境.    通过命令和配置文件来管理虚拟机,很快就能完成一套开发环境的 ...