Double Pointer < O(n^2) Approach


#1
class Solution:
    # @param A : tuple of integers
    # @param B : tuple of integers
    # @return an integer

    def canCompleteCircuit(self, arr_a, arr_b):
        total_a = len(arr_a)
        available_fuel = 0
        index = 0
        arr_c = []
        for i in range(total_a):
            arr_c.append(arr_a[i] - arr_b[i])
        for i in range(total_a):
            available_fuel += arr_c[i]
            while available_fuel < 0:
                available_fuel -= arr_c[index]
                index += 1
                if index == i:
                    index = i + 1  # index += 1
                    available_fuel = 0
                    break
        for i in range(index):
            available_fuel += arr_c[i]
            if available_fuel < 0:
                index = -1
                break
        return index