Leetcode - Gas Station

My code:

public class Solution {
    public int canCompleteCircuit(int[] gas, int[] cost) {
        if (gas == null || gas.length == 0 || cost == null || cost.length == 0)
            return -1;
        int len = gas.length;
        int sum = 0;
        int total = 0;
        int ret = 0;
        int[] diff = new int[len];
        for (int i = 0; i < len; i++)
            diff[i] = gas[i] - cost[i];
        for (int i = 0; i < len; i++) {
            total += diff[i];
            if (sum < 0) {
                sum = diff[i];
                ret = i;
            }
            else {
                sum += diff[i];
            }
        }
        if (total < 0)
            return -1;
        else
            return ret;
    }
    
    /*
    public int canCompleteCircuit(int[] gas, int[] cost) {
        if (gas == null || gas.length == 0 || cost == null || cost.length == 0)
            return -1;
        for (int i = 0; i < gas.length; i++) {
            if (simulate(i, gas, cost))
                return i;
        }
        return -1;
    }
    
    private boolean simulate(int begin, int[] gas, int[] cost) {
        int tank = gas[begin];
        for (int i = 0; i< gas.length; i++) {
            int cos = cost[(begin + i) % gas.length];
            if (cos > tank)
                return false;
            else
                tank = tank - cos + gas[(begin + i + 1) % gas.length];
        }
        return true;
    }
    */
    
}

这道题目我的做法是对的,但是过不了测试,因为忽略了一点。
如果:
A -> B ok
A -> B -> C no
那么,
B 也到不了C。具体证明看博客。
那么我可以拿到我到不了的那个加油站的index。然后从他开始继续遍历。直到尾部。
然后在设置一个变量计算总量,如果小于0,那么就完成不了,直接返回-1
否则返回之前设置的index

http://www.programcreek.com/2014/03/leetcode-gas-station-java/

**
总结:
Math, Geedy
**

Anyway, Good luck, Richardo!

My code:

public class Solution {
    public int canCompleteCircuit(int[] gas, int[] cost) {
        if (gas == null || gas.length == 0 || cost == null || cost.length == 0) {
            return -1;
        }
        
        int i = 0;
        int pre = 0;
        int begin = 0;
        int end = 0;
        while (end < gas.length) {
            if (gas[end] + pre >= cost[end]) {
                pre += gas[end] - cost[end];
                end++;
            }
            else {
                begin = end + 1;
                end = begin;
                pre = 0;
            }
        }
        
        if (begin == end) {
            return -1;
        }
        else {
            i = begin;
            pre = gas[i] - cost[i];
            if (pre < 0) {
                return -1;
            }
            i++;
            while (i % gas.length != begin) {
                pre += gas[i % gas.length] - cost[i % gas.length];
                if (pre < 0) {
                    return -1;
                }
                i++;
            }
            return begin;
        }
        
    }
}

这道题目我这次倒是做出来了。
我的想法是:
A如果能到B但不能到C,
那么B一定靠自己到不了C

按照这个逻辑和题目的假设(如果有解,一定只有一个)
我先遍历一遍环,如果A到不了某点D,那么A到D之间的所有点全部放弃,不用检测。
直接从D开始。
如果最后循环结束,还没有找到符合条件的店,返回-1
否则,就自己再重新测试下这个点,看看对不对。对的话,就返回。

差不多就这个思路

Anyway, Good luck, Richardo! -- 09/15/2016

你可能感兴趣的:(Leetcode - Gas Station)