Algorithm for truck moving around a circle of gas stations

前端 未结 11 1380
陌清茗
陌清茗 2020-12-13 07:08

You have a truck moving around a circular track with gas stations spaced out around the circle. Each station has a finite amount of gas. The gas tank on the truck is infinit

11条回答
  •  暖寄归人
    2020-12-13 07:45

    The solution to this problem is based on a greedy algorithm. It is based on the following two observations.

    1. if the total gas > cost, there must be a start index to finish the circle, else there isn't;

    2. as to an index i, if from i, j is the first index that we cannot reach, then any index from i to j, cannot be the start index.

    For more explanation, have a look at the following link - Gas Station problem.

提交回复
热议问题