LeetCode——Gas Station

There are N gas stations along a circular route, where the amount of gas at station i is gas[i].

You have a car with an unlimited gas tank and it costs cost[i] of gas
to travel from station i to 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.

原题链接:https://oj.leetcode.com/problems/gas-station/

题目:在一个环上有N个加油站,站 i 有 gas[i] 的油。

你有一辆汽车有无限大小的油箱,它从站 i 到站 i+1 要耗cost[i] 的油。你以空油箱从其中的一个站出发。如果你转了一圈,返回起始油站的索引,否则返回-1.

思路:先计算车到达每个加油站时加油站的油量和汽车消耗的油量之差,如果之差小于0,则说明汽车至少要从下一站出发,因为从本站出发的话,就到达不了下一站。如果总的之差小于0,则车无法完成一圈。

	public int canCompleteCircuit(int[] gas, int[] cost) {
		int minus = 0, total = 0, index = -1;
		for (int i = 0; i < gas.length; i++) {
			minus += gas[i] - cost[i];
			total += minus;
			if (minus < 0) {
				index = i;
				minus = 0;
			}
		}
		return total < 0 ? -1 : index + 1;
	}

LeetCode——Gas Station

时间: 2024-11-07 08:05:43

LeetCode——Gas Station的相关文章

Leetcode:Gas Station 加油站

Gas Station There are N gas stations along a circular route, where the amount of gas at station i is gas[i]. You have a car with an unlimited gas tank and it costs cost[i] of gas to travel from station i to its next station (i+1). You begin the journ

[leetcode]Gas Station @ Python

原题地址:https://oj.leetcode.com/problems/gas-station/ 题意: There are N gas stations along a circular route, where the amount of gas at station i is gas[i]. You have a car with an unlimited gas tank and it costs cost[i] of gas to travel from station i to

[LeetCode] Gas Station,转化为求最大序列的解法,和更简单简单的Jump解法。

LeetCode上 Gas Station是比较经典的一题,它的魅力在于算法足够优秀的情况下,代码可以简化到非常简洁的程度. 原题如下 Gas Station There are N gas stations along a circular route, where the amount of gas at station i is gas[i]. You have a car with an unlimited gas tank and it costs cost[i] of gas to

LeetCode: Gas Station [134]

[题目] There are N gas stations along a circular route, where the amount of gas at station i is gas[i]. You have a car with an unlimited gas tank and it costs cost[i] of gas to travel from station i to its next station (i+1). You begin the journey with

LeetCode &quot;Gas Station&quot; - TO BE REVISED

First I thought of a DP solution.. but TLE. So there must be a O(n). I thought of Mono-Queue.. but looks too complex. So what is the naive solution? O(n^2) of course. If we pick next start station smartly, we can make it linear: http://blog.csdn.net/

LeetCode: Gas Station 解题报告

Gas Station There are N gas stations along a circular route, where the amount of gas at station i is gas[i]. You have a car with an unlimited gas tank and it costs cost[i] of gas to travel from station i to its next station (i+1). You begin the journ

[LeetCode]Gas Station

新年啦,大家新年快乐~~ 由于新年呢,所以最近很少做题,今天终于有时间可以打打代码了 134. Gas Station. There are N gas stations along a circular route, where the amount of gas at station i is gas[i]. You have a car with an unlimited gas tank and it costs cost[i] of gas to travel from station

[LeetCode] Gas Station 加油站问题

There are N gas stations along a circular route, where the amount of gas at station i is gas[i]. You have a car with an unlimited gas tank and it costs cost[i] of gas to travel from station i to its next station (i+1). You begin the journey with an e

[LeetCode]Gas Station 最大子序列和,贪心

在一个环形路径上分布着n个加油站,从一个加油站到下一个加油站会耗油.要找出一个起始点,从这个起始点出发,存油量>=耗油量.解保证唯一. 这要求在路径上不能出现非负的. 如果找出存油量最大的子序列,则可确保尽可能走完全程(贪心思想). 因此,需要在环形数组中找一个最大子序列和.最大子序列和是很经典的DP问题,而针对环形约束,采用的是将数组复制一份并放到原数组最后,然后采用普通的最大子序列求解.其间,需要记录起始位置(这是最终要求的),走过的站点数(站点数不能超过n,这是环形数组,长度为2n),最大