make the full circular path, shortest path exercise?

后端 未结 8 1514
耶瑟儿~
耶瑟儿~ 2021-02-04 22:27

I got this question in an interview and I was not able to solve it.

You have a circular road, with N number of gas stations. You know the ammount of gas

相关标签:
8条回答
  • 2021-02-04 23:12

    Find the gas station with the most gas but for when the gas for the next station when added to the tank does not exceed the capacity of the tank.

    0 讨论(0)
  • 2021-02-04 23:25

    Make circular list of stations. Find any station with positive value of

    Excess = (gasAtStation - gasToMoveToNextStationNeeded)

    This is current base.

    While next station has negative Excess value, add it's gasAtStation and gasToMoveToNextStationNeeded to current base fields, and remove this station from list.

    Repeat for all positive stations circularly.

    When no more stations to remove:

    If one or some non-negative stations remains in list - any of them is suitable as starting point.

    Example:

    A(-50) B(100) C(-20) D(-90) E(60) [C->B]

    A(-50) B(80) D(-90) E(60) [D->B]

    A(-50) B(-10) E(60) [A->E]

    B(-10) E(10) [B->E]

    E(0)

    0 讨论(0)
提交回复
热议问题