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. PHP实现RabbitMQ的Publish/Subscribe

    <?php /** * Created by PhpStorm. * User: 豆腐居士 * Date: 2018/5/30 * Time: 上午11:01 */ class AqiTask ...

  2. swfit各种Function表现形式

    //: Playground - noun: a place where people can play import UIKit //多返回值函数 func countss(string: Stri ...

  3. js 淘宝评分

    <!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content ...

  4. 【设计模式】工厂方法(FactoryMethod)模式

    看不见PPT的请自行解决DNS污染问题. 相关类的代码: namespace FactoryPatternConsole.Model { public class Address { public s ...

  5. GetDlgItem() 出现错误

    写MFC程序ASSERT(IsWindow(pTemp->m_hWnd))报错 CRect rect; CWnd *pWnd = GetDlgItem(IDC_picture);//IDC_pi ...

  6. apache压缩页面, 全面加速网站

    介绍: 网页压缩来进一步提升网页的浏览速度,它完全不需要任何的成本,只不过是会让您的服务器CPU占用率稍微提升一两个百分点而已或者更少.   原理:   网页压缩是一项由 WEB 服务器和浏览器之间共 ...

  7. 防止vue组件渲染不更新

    1.key <el-dialog title="" :visible.sync="dialogVisible" @close="dialogCl ...

  8. 为什么我们有时不用配置java环境变量?

    答案都在这个图中 完毕,如果还不懂请自行查询注册表相关内容学习.

  9. OJ刷题---ASCII码排序

    题目要求: watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvbGluaGFpeXVuX3l0ZHg=/font/5a6L5L2T/fontsize/400/f ...

  10. 结构体定义:struct与typedef struct

    https://blog.csdn.net/haiou0/article/details/6877718?tdsourcetag=s_pcqq_aiomsg https://blog.csdn.net ...