Leetcode gas station Two characteristics, two methods perfect solution-Update proof method

Source: Internet
Author: User
Gas Station

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

You are have a car with the unlimited gas tank and it costs cost[i] of the gas to the travel from station I to its next station (I+1). You begin the journey with a empty tank at one of the gas stations.

Return to the starting gas station ' s index if you can travel around the circuit once, otherwise return-1.

Note:
The solution is guaranteed unique.

This problem is also very troublesome. At first glance, with the simplest algorithm is one point of calculation, calculated to no oil, proof that this can not be a starting point. Move to the next point as a starting point. This way of thinking is quite simple, but this is not accepted, because the compiler timed out.

I think the key to doing this problem is to be able to sum up the properties of this topic, note this place, the main properties of two:

1 if the total gas-cost is less than 0, then no solution returns-1

2 If all the previous gas-cost add up to less than 0, then all the previous points are not a starting point.

2013-12-1 Update:

Original: Jing Xin http://write.blog.csdn.net/postedit/14106137

The correctness of the first attribute is well understood. So why is the second attribute set up?

First of all we are calculated from the i = 0 gas station, set the starting residual oil to left=0, if this station oil can reach the next station, then Left=gas-cost is positive,

In the next station there are two things:

1 if I=1 a station gas-cost is also positive, then the left of the front plus the current station residual oil amount is also positive.

2 If the gas-cost of a station i=1 is negative, then the left of the front plus the amount of oil remaining in the current station can also be in two ways:

A) left is positive, then you can continue to the next station, repeat the previous calculation i=2,i=3 ... until the second one occurs

If left is negative, then can not go to the next station, this time if I=k (i<k<n), this time need to start from the I=1 station began to recalculate. No, because all left before the K-station is positive, and the K-station becomes negative.

Prove:

Left (i) >0, it is not necessary to restart the test from I+1 station if there is i+1 <0; then left (i) + Left (i+1) >0; Left (i) + Down (i+1) +left (i+2) (i+2) then it is not necessary to start from I+2 station, and so on, and so on .... Left (i) + ... + Left (k-2) >0. +left (K-2) > Left (k-1), then there is no need to restart the calculation from the K-1 node, now to the K station of the remaining oil amount to negative, also can not be a starting point, then directly to the k+1 calculation can be. This will result in attribute 2.

It is difficult to prove a theorem without paying attention to the method of proving theorem of mathematics before. But the original method is not to prove the theorem, but to find the rules and characteristics.

The main use of attribute 2 can write two programs:

Program One: Record the last index added up to less than 0, and then return the index +1 is the answer.

Program two: Jump-type, jump not as a starting point, accelerate the cycle

It's hard to make out these characteristics without summarizing them. Both of these methods run at the same time.

Program One:

Class Solution {public
:
	int cancompletecircuit (vector<int> &gas, vector<int> &cost) 
	{
		int sum = 0;
		int total = 0;
		Int j =-1;
		for (int i = 0; i < gas.size (); ++i)
		{
			sum + = Gas[i]-cost[i];
			Total + = Gas[i]-cost[i];
			if (Sum < 0)
			{
				j=i; sum = 0; 
			}
		}
		if (total<0) return-1;
		else return j+1;
	}
;

Program Two:

Class Solution {public
:
	int cancompletecircuit (vector<int> &gas, vector<int> &cost) 
	{
		int n = gas.size ();
		int j = 0;
		for (int i=0; i<n;)
		{
			j=i;
			if (StartPoint (gas, Cost, j)) return
				i;
			i + j;
		}
		return-1;
	}
 
	BOOL StartPoint (vector<int> &gas, vector<int> &cost, int& start)
	{
		int n = gas.size () ;
		int left = 0;
		int temp;
		for (int i=start; i< (N+start); i++)
		{
			temp = i%n;
			Left + + gas[temp]-cost[temp];
			if (left<0) 
			{
				start = i-start+1;
				return false;
			}
		}
		return true;
	}
;

The first kind of code is simple, but more difficult to think out, the second is better to come out.
I think about how to deal with these problems. Especially if the interview, time is limited, it is more difficult.

I think of the strategy is: first to give some examples, observe their characteristics, and then summed up, and then design algorithms. Who has not done, can suddenly see the law of it.

Update the more concise point code for the second idea:

int CanCompleteCircuit2 (vector<int> &gas, vector<int> &cost) 
	{for
		(int i = 0; i < cost.si Ze (); )
		{
			int leftgas = 0;
			int j = 0;
			for (; J < Cost.size (); j + +)
			{
				int k = (i+j)%cost.size ();
				Leftgas + + (Gas[k]-cost[k]);
				if (Leftgas < 0) break;
			if (j = = Cost.size ()) return i;
			i+=j+1;
		}
		return-1;
	}

When you prove it, it's easier to use contradiction.

2014-2-18 Update
	int cancompletecircuit (vector<int> &gas, vector<int> &cost) 
	{ for
		(int i = 0; i < gas.size ();)
		{
			int left_gas = 0, j = 0;
			for (; J < Gas.size (); j + +)
			{
				int t = (i+j)%gas.size ();
				Left_gas = Left_gas + gas[t]-cost[t];
				if (Left_gas < 0) break;
			if (j = = Gas.size ()) return i;//error: J=i?
			else i + = j+1;//j+1, Compute good subscript
		}
		return-1;
	



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.