Leetcode's facebook page has post an announcement of TLE on java implementation, it seems they are more restricted to brute force algorithms.

For this problem, a linear algorithm exists. So try to be O(n). In fact, theta n.

My linear solution based on a observation that, if you stops at certain gas station, the gas stations previous to the station you stop must not be a valid starting point. So all you need is just circle once, if you don't have enough gas, then start from next stop. In worst case, you will go through 2N gas stations. and In best case, you will go through N gas stations. So the time complexity is theta N. and Space complexity is constant.

So, simply start from the first station, and if you come back, return that station. If you stop at certain station, start from the next station. When you circle once from some station, return that station, when you starting point is greater than gas.length, it means no station can be a starting point, so return -1.