Guest User

Untitled

a guest
Jun 20th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.49 KB | None | 0 0
  1. class Solution:
  2. def canCompleteCircuit(self, gas, cost):
  3. """
  4. :type gas: List[int]
  5. :type cost: List[int]
  6. :rtype: int
  7. """
  8. #FIRST METHOD N^2 DP
  9. # THE METHOD IS O(N)
  10. if sum(gas) < sum(cost):
  11. return -1
  12. n = len(gas)
  13. sum_num = 0
  14. index = -1
  15. for i in range(n):
  16. sum_num += gas[i]-cost[i]
  17. if sum_num <0:
  18. index = i
  19. sum_num = 0
  20. return index+1
Add Comment
Please, Sign In to add comment