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. dx wpf的各种坑

    这篇随笔总结dx wpf使用中的各种坑,持续更新~ LookUpEdit里内嵌的DXGrid的名字必须是"PART_GridControl",不能不写.也不能写错.我对比了2个小时 ...

  2. POJ 1700 F(Contest #3)

    Description A group of N people wishes to go across a river with only one boat, which can at most ca ...

  3. Enumeration 接口

    Enumeration是遍历集合元素的一种方法. Enumeration中只有两个方法: 1.hasMoreElements()  测试此枚举是否包含更多的元素. 2.nextElement()  如 ...

  4. Autolayout-VFL语言添加约束

    一.VFL语言简洁 VFL(Visual format language)语言是苹果为了简化手写Autolayout代码所创建的专门负责编写约束的代码.为我们简化了许多代码量. 二.使用步骤 使用步骤 ...

  5. Android Studio 配置JPush

    1.在JPush官方下载 JPush SDK(jpush-android-arm-2.1.0.zip),我下载的是2.1.0: 2.解压下载好的压缩包(jpush-android-arm-2.1.0. ...

  6. RPI学习--wiringPi_setups

    reference: http://wiringpi.com/reference/setup/ There are four ways to initialise wiringPi. wiringPi ...

  7. hive内部表、外部表

    hive内部表.外部表区别自不用说,可实际用的时候还是要小心. Hive的数据分为表数据和元数据,表数据是Hive中表格(table)具有的数据:而元数据是用来存储表的名字,表的列和分区及其属性,表的 ...

  8. pyqt5 笔记(四)cx_Freeze 实现代码打包exe

    下载地址:https://pypi.python.org/pypi/cx_Freeze 教程:http://www.cnblogs.com/xinzaitian/archive/2010/12/10/ ...

  9. jQuery 常用动画

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  10. alpha,hidden,opaque的一些认识

    如果opaque设置为YES,那么视图会被当做全视图来对待,系统会重绘整个视图 如果opaque设置为NO,那么系统会减少开销,以其中的内容来判定重绘的视图 如果把视图的背景色设置为透明那个,那么op ...