Q134. Gas Station
Last updated
Was this helpful?
Last updated
Was this helpful?
直达:
There areNgas stations along a circular route, where the amount of gas at stationiisgas[i]
.
You have a car with an unlimited gas tank and it costscost[i]
of gas to travel from stationito its next station (i+1). You begin the journey with an empty tank at one of the gas stations.
Return the starting gas station's index if you can travel around the circuit once, otherwise return -1.
Note: The solution is guaranteed to be unique.
难点是理清思路,分析出该问题存在的几个性质。
性质1:如果gas[i] < cost[i],则i不能作为起点;
性质2:对所有路径求和,如果,则一定存在一点可以遍历整个环。
问题是当确定能够便利之后,如何确定起点:
性质3:如果i能够作为起点,则从i出发可以到任何一个点;
性质4:如果i是唯一的一个点,选择不存在点j可以到i;
性质5:如果i是唯一的一个点,则i一定可以到i+1到n,0到i-1一定不能到i
根据上面三个性质,得到解决问题的思路:
根据性质2,遍历两个数组,确定能不能完成环的遍历;能的话,进入2,否则直接返回-1;
根据性质5,从0出发,查找0不能到的i,如果i+1能到n,则可以返回i+1, 如果i+1不能到n,则将i更新为i能到的最近的节点。