Best Time to Buy and Sell Stock_Runtime error 1

#include<iostream>
#include<vector>

#define INF -32760 
using namespace std;

class Solution {
public:
  int maxProfit(vector<int> &prices) 
	{
		int *P;
		int result;
		P=new int[prices.size()];
		for(int i=0;i<prices.size();i++)
			P[i]=prices[i];
		result=max_sub_arry(P,0,prices.size()-1);
		delete []P;
		return result;
	}
	int MAX_cross_sum(int A[],int low,int mid,int high);
	int max_three(int a,int b,int c);
	int max_two(int a,int b);
	int max_sub_arry(int A[],int low,int high);
};

int Solution::MAX_cross_sum(int A[],int low,int mid,int high)  
{  
	int max_cross;  
	int left_sum=INF;  
	int max_left=0;  
	int i=0;  
	int sum=0;  
	int right_sum=INF;//右边的最大值  
	int max_right=0;//右边的最大值时的编号.  
	//左边的最大  
	for(i=mid;i>=low;i--)  
	{  
		sum=sum+A[i];  
		if(sum>=left_sum)  
		{  
			left_sum=sum;  
			max_left=i;  
		}  
	}  
	//右边的最大  
	sum=0;  
	for(i=mid+1;i<=high;i++)  
	{  
		sum=sum+A[i];  
		if(sum>=right_sum)  
		{  
			right_sum=sum;  
			max_right=i;  
		}  
	}  
	max_cross=left_sum+right_sum;  
	return max_cross;  
}

int Solution::max_three(int a,int b,int c)  
{  
	if(a>=b&&a>=c)  
		return a;  
	else if(b>=a&&b>=c)  
		return b;  
	else  
		return c;  
} 

int Solution::max_two(int a,int b)  
{  
	if(a>=b)  
		return a;  
	else  
		return b;  
} 

int Solution::max_sub_arry(int A[],int low,int high)  
{  
	if(low==high)  
		return A[low];  
	else if((high-low)==1)  
		return max_two(A[low],A[high]);  
	else  
		return max_three(MAX_cross_sum(A,low,(low+high)/2,high),  
		max_sub_arry(A,low,(low+high)/2-1),  
		max_sub_arry(A,((low+high)/2)+1,high));   
} 

int main()
{
	int arry[10]={-1,2,3,9,-2,3,4,-2,6,-1};
	vector<int> prices;
	for(int i=0;i<10;i++)
		prices.push_back(arry[i]);
	for(int i=0;i<10;i++)
		cout<<prices[i]<<endl;
	cout<<prices.size()<<endl;


	Solution A;
	cout<<A.maxProfit(prices)<<endl;

	system("pause");  
	return 1;  
}







评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值