[Leetcode Solution] Gas Station
Analysis
- The insight of problem is given a list of integers, find out if existing a point starting which to all of the resting points that the sum is always non negative.
- The fact is that starting from point x to point y if the sum is becoming negative from positive. Then point x won't be that point. And none of point from x to y could be that point.
- If the sum of all the integers is non negative, that point must exist which can be proven easily by contradiction. Taking advantage of this, a more graceful algorithm could be found here.
Note
- Always remember to update the loop variable after each iteration ends (always think about the architecture of program before programming)
Code
No comments:
Post a Comment
Enter you comment