Anyone can explain why the expected answer is like this?


  • 0
    T

    The input is:
    [3, 1, 9, 5, 1, 1, 10]
    [5, 6, 2, 3, 4, 8, 3]

    And the expected answer is 6

    Ok, start at station 6, the car get 10 gas and consume 3 to reach station 0
    Then the car has 7 gas at station 0

    Go ahead:

    at station 0 -> 0 + 10 - 3 = 7
    at station 1 -> 7 + 3 - 5 = 5
    at station 2 -> 5 + 1 - 6 = 0
    at station 3 -> 0 + 9 - 2 = 7
    at station 4 -> 7 + 5 - 3 = 9
    at station 5 -> 9 + 1 - 4 = 6

    We still need to travel to station 6. However, we only have 6(remaining) + 1(this station) = 7 gas and we need to consume 8 to get to station 6. So I think the answer is -1.

    Anyone can tell me why the answer is 6 instead of -1?


  • 0

    @tanghaomo The expected answer is -1 if you put these arrays in the test case. By the way, the total gas is 30 but the total cost is 31. So it's gonna to be -1.


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.