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. Chrome 开发者工具有了设备模拟器

    今天从哥们那里学到了一个小技巧,使用chrome自带的多设备模拟器来调试页面在不同设备下的显示效果. 特地上网查了一下,记录一下. 如果想要在 Chrome 上测试网站在不同设备,不同分辨率的显示情况 ...

  2. Debugger Exception Notification

    --------------------------- Debugger Exception Notification --------------------------- Project PJSP ...

  3. 【STL】-function object

    // Generic findMax, with a function object, version #1 // Precondition, a.size() > 0 #include < ...

  4. 将Mat类型转换成QImage类型

    ui 头文件 #ifndef DIALOG_H #define DIALOG_H #include <QDialog> #include<opencv2/highgui/highgu ...

  5. 解决VS2010控制台程序运行结束不显示请按任意键继续

    在VS2010里的控制台应用程序在运行时,结果画面一闪而过,不管是用F5 还是用Ctrl + F5都是一样,导致无法看到结果. 网上有不少的办法,说是都是在程序最后加一个要程序暂停的语句( syste ...

  6. idea常用快捷键大全(转)

    IntelliJ Idea 常用快捷键列表   文章来自:http://lavasoft.blog.51cto.com/62575/97730/   Alt+回车 导入包,自动修正Ctrl+N   查 ...

  7. iPhone 6/6 Plus国行版开卖当日抢购攻略

    在距离苹果首批发售时隔一个月也就是北京时间10月17日,苹果iPhone 6.iPhone 6 Plus终于也要在中国大陆开卖,众多国内用户终于有机会安排自己的购机计划.据不完全数据显示,目前iPho ...

  8. .NET项目框架(转)

    摘要:本文描述了在用VS.NET进行B/S开发时采用的框架结构,一般建立类库项目和Web项目,在Web基本aspx页面类中调用类库中方法,同时在aspx页面类中不需要写任何对数据库操作的SQL代码,便 ...

  9. MATLAB简单实现ID3

    再看<MATLAB数据分析与挖掘实战>,简单总结下今天看到的经典的决策树算法——ID3. ID3:在决策树的各级节点上,使用信息增益的方法作为属性的选择标准,来帮助确定生成每个节点时所应采 ...

  10. two Sum ---- LeetCode 001

    Given an array of integers, return indices of the two numbers such that they add up to a specific ta ...