134. Gas Station
题目:
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.
链接: http://leetcode.com/problems/gas-station/
题解:
经典题gas station,贪婪法。设计一个局部当前的gas,一个全局的gas,当局部gas小于0时,局部gas置零,设置结果为当前index的下一个位置,此情况可能出现多次。当全局gas小于0的话说明没办法遍历所有gas站,返回-1。
Time Complexity - O(n), Space Complexity - O(1)。
public class Solution {
    public int canCompleteCircuit(int[] gas, int[] cost) {
        if(gas == null || cost == null || gas.length == 0 || cost.length == 0 || gas.length != cost.length)
            return 0;
        int curGas = 0;
        int totalGas = 0;
        int result = 0;
        for(int i = 0; i < gas.length; i++){
            curGas += gas[i] - cost[i];
            totalGas += gas[i] - cost[i];
            if(curGas < 0){
                result = i + 1;
                curGas = 0;
            }
        }
        if(totalGas < 0)
            return - 1;
        return result;
    }
}
Update:
public class Solution {
    public int canCompleteCircuit(int[] gas, int[] cost) {
        if(gas == null || cost == null || gas.length != cost.length || gas.length == 0)
            return -1;
        int len = gas.length;
        int totalGasRequired = 0, curGas = 0, station = 0;
        for(int i = 0; i < len; i++) {
            totalGasRequired += gas[i] - cost[i];
            curGas += gas[i] - cost[i];
            if(curGas < 0) {
                curGas = 0;
                station = i + 1;
            }
        }
        return totalGasRequired >= 0 ? station : -1;
    }
}
二刷:
Java:
public class Solution {
    public int canCompleteCircuit(int[] gas, int[] cost) {
        if (gas == null || cost == null || gas.length != cost.length) return -1;
        int totalCost = 0, totalGas = 0, curTank = 0, startingIndex = 0;
        for (int i = 0; i < gas.length; i++) {
            totalGas += gas[i];
            totalCost += cost[i];
            curTank += (gas[i] - cost[i]);
            if (curTank < 0) {
                curTank = 0;
                startingIndex = i + 1;
            }
        }
        return totalGas >= totalCost ? startingIndex : -1;
    }
}
测试:
134. Gas Station的更多相关文章
- 134. Gas Station leetcode
		134. Gas Station 不会做. 1. 朴素的想法,就是针对每个位置判断一下,然后返回合法的位置,复杂度O(n^2),显然会超时. 把这道题转化一下吧,求哪些加油站不能走完一圈回到自己,要求 ... 
- 贪心: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的顺序不能改变,只能通过容器来获得由大到小的顺 ... 
- 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 ... 
- 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 ... 
- leetcode@ [134] Gas station (Dynamic Programming)
		https://leetcode.com/problems/gas-station/ 题目: There are N gas stations along a circular route, wher ... 
- [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 ... 
- 【LeetCode】134.Gas Station
		Problem: There are N gas stations along a circular route, where the amount of gas at station i is ga ... 
- 134. Gas Station加油站
		[抄题]: There are N gas stations along a circular route, where the amount of gas at station i is gas[i ... 
- 134. Gas Station(数学定理依赖题)
		There are N gas stations along a circular route, where the amount of gas at station i is gas[i]. You ... 
随机推荐
- Android Activity常用生命周期函数
			在Activity中主要有7个常用的周期函数,他们分别是: (一)onCreate 在Activity对象被第一次创建时调用 注: 从另一个Activity返回到前一个Activity时,不会调用该函 ... 
- ios数据库FMDB
			一.下载fmdb类库 二.添加libsqulite3.0.dylib 三.添加头文件#import "FMDB.h" 四.打开数据库 a.设置路径NSString *path = ... 
- asp.net 邮件发送类
			using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.We ... 
- C# ACM poj1005
			大水题呀 public static void acm1005(int n, float[,] a) { float pi = 3.1415926f, rr; int years; ; i < ... 
- vs2012找不到system web optimization命名空间
			今天新装了vs2012,安装完成后,创建了一个mvc4应用程序,创建生成出现了几个错误.通过错误我们的解决方案就是去找引用不到的路径,如何在vs2012中实现呢? 在工具栏中找工具--库程序包管理器- ... 
- js中对闭包的理解
			本人丝一枚,在刚刚过去的javascript学习中,对闭包这个知识真是,听课两分钟,懵逼一整天.今天闲来没事,看了下闭包.话不多说先上代码. <!DOCTYPE html> <htm ... 
- WPF 关于XDocument(xml) 的部分操作记录
			(1)删除xml文件中的一个结点的方法,有如下两种方式(只有存在数据绑定的情况下才会有第二种情况,否则一般是第一种情况): private void DeletePacsNode() { //从xml ... 
- 第43条:返回零长度的数组或者集合,而不是null
			private final List<Cheese> cheesesInStock = ...; public Cheese[] getCheese() { if(cheesesInSto ... 
- 队列(链式存储)C++模板实现
			#include <iostream> using namespace std; //队列结点类 template <typename T> class QueueNode{ ... 
- MVC文件上传 - 使用jquery异步上传并客户端验证类型和大小
			本篇体验MVC上传文件,从表单上传过渡到jquery异步上传. MVC最基本的上传文件是通过form表单提交方式 □ 前台视图部分 <% using(Html.BeginForm("F ... 
