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.

解题思路:

由于答案唯一,所以,只能按照一个方向走(不需要考虑从i+1→i的情况)

考虑到从i出发,到达i+j之后无法前进,那么从i+i出发,最多也只能到达i+j,所以下次循环可以从i+j+1开始,JAVA实现如下:

	public int canCompleteCircuit(int[] gas, int[] cost) {
for (int i = 0; i < gas.length; i++) {
int sum = gas[i] - cost[i];
int index = i, count = 0;
while (sum >= 0) {
if (++count == gas.length)
return index;
int j = ++i % gas.length;
sum += gas[j] - cost[j];
}
}
return -1;
}

Java for LeetCode 134 Gas Station的更多相关文章

  1. leetcode 134. Gas Station ----- java

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

  2. leetcode@ [134] Gas station (Dynamic Programming)

    https://leetcode.com/problems/gas-station/ 题目: There are N gas stations along a circular route, wher ...

  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. 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 ...

  5. [leetcode]134. Gas Station加油站

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

  6. [leetcode] 134. Gas Station (medium)

    原题 题意: 过一个循环的加油站,每个加油站可以加一定数量的油,走到下一个加油站需要消耗一定数量的油,判断能否走一圈. 思路: 一开始思路就是遍历一圈,最直接的思路. class Solution { ...

  7. 134. Gas Station leetcode

    134. Gas Station 不会做. 1. 朴素的想法,就是针对每个位置判断一下,然后返回合法的位置,复杂度O(n^2),显然会超时. 把这道题转化一下吧,求哪些加油站不能走完一圈回到自己,要求 ...

  8. 贪心:leetcode 870. Advantage Shuffle、134. Gas Station、452. Minimum Number of Arrows to Burst Balloons、316. Remove Duplicate Letters

    870. Advantage Shuffle 思路:A数组的最大值大于B的最大值,就拿这个A跟B比较:如果不大于,就拿最小值跟B比较 A可以改变顺序,但B的顺序不能改变,只能通过容器来获得由大到小的顺 ...

  9. 【LeetCode】Gas Station 解题报告

    [LeetCode]Gas Station 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/gas-station/#/descr ...

随机推荐

  1. many-to-one多对一属性说明

    通过many-to-one元素,可以定义一种常见的与另一个持久化类的关联. 这种关系模型是多对一关联(实际上是一个对象引用-译注):这个表的一个外键引用目标表的 主键字段. <many-to-o ...

  2. Form元素示例

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  3. Git历险记(五)——Git里的分支&合并

    分支与合并 在Git里面我们可以创建不同的分支,来进行调试.发布.维护等不同工作,而互不干扰.下面我们还是来创建一个试验仓库,看一下Git分支运作的台前幕后: $rm -rf test_branch_ ...

  4. 2017.3.27 集成modeler后的一些主要路径(持续更新)

    1.设计器访问路径 项目名:wfs_web edtor-app和modeler.html的存放位置:webapp/designer/editor-app app-cfg.js中根路径设置:'conte ...

  5. 每天5道面试题(二)java基础

    说出Servlet的生命周期,并说出Servlet和CGI的差别 Servlet被server实例化后,容器执行其init方法,请求到达时执行其service方法,service方法自己主动派遣执行与 ...

  6. Neural Turing Machines-NTM系列(一)简述

    Neural Turing Machines-NTM系列(一)简述 NTM是一种使用Neural Network为基础来实现传统图灵机的理论计算模型.利用该模型.能够通过训练的方式让系统"学 ...

  7. Docker 三大核心工具

    Docker-machineDocker Machine是一个简化Docker安装的命令行工具,通过一个简单的命令行即可在相应的平台上安装Docker,比如VirtualBox. Digital Oc ...

  8. pojo和vo有什么区别

    pojo 是Plain Old Java Object的缩写,就是javabean.vo是view object的缩写,就是用于页面显示的javabean.vo就是pojo.只是通途上的用于携带页面显 ...

  9. 【Wechall.net挑战】Anderson Application Auditing

    Wechall.net是一个国外用于练习CTF和攻防的网站,国内资料writeup不多,只有个别几篇.作为小白,近日玩了几道有意思的题目,在此分享 题目地址:http://www.wechall.ne ...

  10. [Android Studio 权威教程]最有用的快捷键

    上篇中我们讲了Android Studio怎样加入插件.这篇我们讲讲AS的快捷键.这里我说明的快捷键都是最最有用的,希望刚刚加入AS的朋友尽快的熟悉一下这几个快捷键,这样能够帮助你提高coding的效 ...