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.

1、暴力的做法

public class Solution {
public int canCompleteCircuit(int[] gas, int[] cost) {
int len = gas.length;
int sum = 0;
int[] dp = new int[len];
for( int i = 0;i<len;i++){
dp[i] = gas[i]-cost[i];
sum+=dp[i];
}
int num = 0;
if( sum < 0)
return -1;
for( int i = 0;i<len;i++){
num = 0;
for( int j = i;j<len;j++){
num+=dp[j];
if( num < 0){
break;
}
}
if( num >= 0 )
return i;
} return -1; }
}

2、仔细看题目,发现答案其实是唯一解,那么就只需要从后向前遍历,找到最大子串和的起始位置i,(结束位置是最后一位)

在总消耗>0的前提下,返回起始位置i。

否则返回-1。

public class Solution {
public int canCompleteCircuit(int[] gas, int[] cost) { int len = gas.length;
int sum = 0;
int max = 0;
int start = 0;
for( int i = len-1;i>=0;i--){
sum+=(gas[i]-cost[i]);
if( sum > max ){
max = sum;
start = i;
}
}
if( sum < 0)
return -1;
return start; }
}

leetcode 134. Gas Station ----- java的更多相关文章

  1. Java for 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 ...

  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. 弹框工作区(dialog)

    弹出窗口分为普通弹出窗口和模态弹出窗口,普通弹出窗口可以铜鼓taskBar组件进行最小化等操作.弹出的窗口的DOM结构会放入主页面的body中,结构如下: <div class="bj ...

  2. Map-Reduce的工作机制

    Mapper “Map-Reduce”的思想就是“分而治之” Mapper负责“分”,即把复杂的任务分解为若干个“简单的任务”而执行 “简单的任务”有几个意思:1.数据或计算规模相对于原任务要大大缩小 ...

  3. 上架app 到app store 的出现: “The IPA is invalid. It does not inlude a Payload directory.”错误处理

    今天打包上传app到app store上遇到的一个错误,在xcode6.2下提示: The IPA is  invalid. It does not inlude a Payload director ...

  4. julia文件合并排序.jl

    julia文件合并排序.jl """ julia文件合并排序.jl http://bbs.bathome.net/thread-39841-1-1.html 2016年3 ...

  5. goldengate 12c 针对oracle 12c配置的主要变化

    由于oracle 12c已经是多租户架构,在使用OGG同步的时候,需要考虑下面一些情况 一个 CDB包含多个PDB,源端部署的一个extract可访问所有pdb redo,理论上不需要每个pdb单独配 ...

  6. 机器翻译(noip2010)

    分析:该题是经典的队列题目,直接用队列实现就可以.如果数据范围大一些的话还可hash判重! 这可以说是一道送分的题目,但是还有粗心的学生会在这里失分,主要原因是数组的范围定义的不合适,因为空间足够用, ...

  7. [Python陷阱]os.system调用shell脚本获取返回值

    当前有shell个脚本/tmp/test.sh,内容如下: #!/bin/bashexit 11 使用Python的os.system调用,获取返回值是: >>> ret=os.sy ...

  8. (转)iOS应用程序生命周期(前后台切换,应用的各种状态)详解

    原文:http://blog.csdn.net/totogo2010/article/details/8048652 iOS应用程序生命周期(前后台切换,应用的各种状态)详解         分类:  ...

  9. 爬虫学习--使用百度api---天气

    #coding:utf-8#version:0.1#note:该即用API能查询指定城市的空气质量指数,但城市数量有限,截止2015年3月26日,只能查到全国161个城市的. import urlli ...

  10. HDU 4869 (递推 组合数取模)

    Problem Turn the pokers (HDU 4869) 题目大意 有m张牌,全为正面朝上.进行n次操作,每次可以将任意ai张反面,询问n次操作可能的状态数. 解题分析 记正面朝上为1,朝 ...