Leetcode: 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.

先来一个随手写的时间复杂度O(n^2)的,理所当然的没过。

class Solution {
public:
    int canCompleteCircuit(vector<int> &gas, vector<int> &cost) {
        bool found;
        int k, remaining;
        int num = gas.size();
        for (int i = 0; i < num; ++i) {
            remaining = 0;
            found = true;
            for (int j = i; j < i + num; ++j) {
                k = j % num;
                remaining = gas[k] + remaining - cost[k];
                if (remaining < 0) {
                    found = false;
                    break;
                }
            }
            
            if (found) {
                return i;
            }
        }
        
        return -1;
    }
};
分析一下,其实可用连续子数组最大和的思想来解,时间复杂度O(n)。

class Solution {
public:
    int canCompleteCircuit(vector<int> &gas, vector<int> &cost) {
        int num = gas.size();
        if (num == 0) {
            return -1;
        }
        
        int sum = 0;
        int result = 0;
        int *gap = new int[num];
        for (int i = 0; i < num; ++i) {
            gap[i] = gas[i] - cost[i];
            sum += gap[i];
            if (sum < 0) {
                sum = 0;
                result = i + 1;
            }
        }
        if (result == num) {
            result = -1;
        }
        else {
            for (int i = 0; i < result; ++i) {
                sum += gap[i];
                if (sum < 0) {
                    result = -1;
                    break;
                }
            }
        }
        delete []gap;
        
        return result;
    }
};

=====================第二次========================

还是没第一时间写出O(n)的来,超时啊。

class Solution {
public:
    int canCompleteCircuit(vector<int> &gas, vector<int> &cost) {
        vector<int> gap(gas.size());
        for (int i = 0; i < gas.size(); ++i) {
            gap[i] = gas[i] - cost[i];
        }
        
        for (int i = 0; i < gap.size(); ++i) {
            int remains = 0;
            for (int j = i; j < gap.size() && remains >= 0; ++j) {
                remains += gap[j];
            }
            for (int j = 0; j < i && remains >= 0; ++j) {
                remains += gap[j];
            }
            
            if (remains >= 0) {
                return i;
            }
        }
        
        return -1;
    }
};

连续子数组最大和,有点忘记了,看来必须保持温故而知新呀。。。。

class Solution {
public:
    int canCompleteCircuit(vector<int> &gas, vector<int> &cost) {
        int remains = 0;
        int result = 0;
        int size = gas.size();
        vector<int> gap(size);
        for (int i = 0; i < size; ++i) {
            gap[i] = gas[i] - cost[i];
            remains += gap[i];
            if (remains < 0) {
                remains = 0;
                result = i + 1;
            }
        }
        if (result == size) {
            return -1;
        }
        
        for (int i = 0; i < result; ++i) {
            remains += gap[i];
            if (remains < 0) {
                return -1;
            }
        }
        
        return result;
    }
};


你可能感兴趣的:(LeetCode)