Problem

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.

Solution

Key to the solution is a conclusion:

If sum of gas >= sum of costs, then there must exists one or more solution.

If sum of gas < sum of costs, then there is no solution.

So we can use method of exclusion here.

We need also note here that if A can not reach C in a the sequence of A-->B-->C, then B can not make it either.

 public class Solution {
public int canCompleteCircuit(int[] gas, int[] cost) {
if (gas == null || cost == null)
return -1;
if (gas.length != cost.length)
return -1;
int start = 0;
int sumRemaining = 0;
int totalRemaining = 0;
for (int i = 0; i < gas.length; i++) {
int tmp = gas[i] - cost[i];
if (sumRemaining >= 0) {
sumRemaining += tmp;
} else {
start = i;
sumRemaining = tmp;
}
totalRemaining += tmp;
}
if (totalRemaining < 0)
return -1;
return start;
}
}

Gas Station 解答的更多相关文章

  1. [LeetCode] Minimize Max Distance to Gas Station 最小化去加油站的最大距离

    On a horizontal number line, we have gas stations at positions stations[0], stations[1], ..., statio ...

  2. LeetCode: Gas Station 解题报告

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

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

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

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

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

  6. 【leetcode】Gas Station

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

  7. [LeetCode] Gas Station

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

  8. 20. Candy && Gas Station

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

  9. 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. PS抠图神器: KnockOut 2.0安装汉化和使用教程

    PS抠图神器: KnockOut 2.0安装汉化和使用教程 http://jingyan.baidu.com/article/6b97984d8aeadc1ca2b0bf3b.html

  2. Swap Nodes in Pairs 解答

    Question Given a linked list, swap every two adjacent nodes and return its head. For example,Given 1 ...

  3. 【HDU1879】继续畅通工程(MST基础题)

    真心大水题...不多说. #include <iostream> #include <cstring> #include <cstdlib> #include &l ...

  4. linux驱动面试题2

    1.什么是GPIO? general purpose input/output GPIO是相对于芯片本身而言的,如某个管脚是芯片的GPIO脚,则该脚可作为输入或输出高或低电平使用,当然某个脚具有复用的 ...

  5. poj 1017 Packets 贪心

    题意:所有货物的高度一样,且其底面积只有六种,分别为1*1 2*2 3*3 4*4 5*5 6*6的,货物的个数依次为p1,p2,p3,p4,p5,p6, 包裹的高度与货物一样,且底面积就为6*6,然 ...

  6. 2D和3D空间中计算两点之间的距离

    自己在做游戏的忘记了Unity帮我们提供计算两点之间的距离,在百度搜索了下. 原来有一个公式自己就写了一个方法O(∩_∩)O~,到僵尸到达某一个点之后就向另一个奔跑过去 /// <summary ...

  7. redis的简单使用

    一.简单使用Jedis 需要Jedis就从Maven获取吧! Maven Pom.xml <dependency> <groupId>redis.clients</gro ...

  8. Windows下PHP开发环境搭建

    PHP集成开发环境有很多,如XAMPP.AppServ......只要一键安装就把PHP环境给搭建好了.但这种安装方式不够灵活,软件的自由组合不方便,同时也不利于学习.所以我还是喜欢手工搭建PHP开发 ...

  9. 利用PHPExcel转Excel柱形图

    这在附还有一个转柱形图的效果及代码. 原PHP报表效果: 转成Excel后的效果: 附上代码: <? php /** * PHPExcel * * Copyright (C) 2006 - 20 ...

  10. hdu 4427 Math Magic

    一个长了一张数学脸的dp!!dp[ i ][ s ][ t ] 表示第 i 个数,sum为 s ,lcm下标为 t 时的个数.显然,一个数的因子的lcm还是这个数的因子,所以我们的第三维用因子下标代替 ...