public int canCompleteCircuit(int[] gas, int[] cost) {
            int num = gas.length;

            if (num != cost.length)
                return -1;

            int[] rest = new int[num];
            int restSum = 0;
            for (int i = 0; i < num; i++) {
                rest[i] = gas[i] - cost[i];
                restSum += rest[i];
            }

            if (restSum < 0)
                return -1;

            int partSum = 0;
            int pos = -1;
            for (int i = 0; i < num; i++) {
                partSum += rest[i];
                if (partSum < 0) {
                    pos = i;
                    partSum = 0;
                }
            }

            return pos + 1;
        }