首页 > 其他 > 详细

CareerCup Find a subarray

时间:2014-02-22 10:47:22      阅读:346      评论:0      收藏:0      [点我收藏+]

Given an array of pairs of the form <a, b>. We have to find a sub-array such that the 1st element in the pairs are in increasing order and the sum of 2nd element of the pairs in the sub-array is maximum possible

--------------------------------------------------------------------------------------------------------

Do you know what does subarray means ? It‘s continuous! So DP is enough like this:

int max_ending_here, max_ending_so_far = 0;
int begin, end = 0;
int begin_temp = begin;

for(int i = 1; i< pairsArray.length; i++)
{
	if(pairsArray[i-1].first < pairsArray[i].first)
	{
		if(max_ending_here < 0)
		{
			max_ending_here = pairsArray[i].second;
			begin_temp = i;
		}
		else
		{
			max_ending_here += pairsArray[i].second;
		}

		if(max_ending_here >= max_ending_so_far)
		{
			max_ending_so_far = max_ending_here;
			begin = begin_temp;
			end = i;
		}
	}
	else
	{	
		max_ending_here = 0;
		begin_temp = i; 
	}
}

If the sub-sequence is not continuous, it‘s similar to longest increasing sub-sequence.





CareerCup Find a subarray

原文:http://blog.csdn.net/taoqick/article/details/19609513

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!