国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

Best Time to Buy and Sell Stock III

2019-11-11 07:39:41
字體:
來源:轉載
供稿:網友

Say you have an array for which the ith element is the PRice of a given stock on day i.

Design an algorithm to find the maximum profit. You may complete at most two transactions.

Note:You may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).

int fun(vector<int> &prices){	int n = prices.size();	int profit1[n];	int profit2[n];	memset(profit1, 0, sizeof(profit1));	memset(profit2, 0, sizeof(profit2));	int leftMin = prices[0];	int rightMax = prices[n-1];	for (int i = 1; i < n; i++)	{		profit1[i] = max(profit1[i-1], prices[i]-leftMin);		leftMin = min(leftMin, prices[i]);	}	for (int i = n-2; i >= 0; i--)	{		profit2[i] = max(profit2[i+1], rightMax-prices[i]);		rightMax = max(rightMax, prices[i]);	}	int result = 0;	for (int i = 0; i < n; i++)	{		int temp = profit1[i] + profit2[i];		result = max(result, temp);	}	return result;}


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 巴塘县| 博爱县| 惠东县| 江门市| 西安市| 张家川| 宣威市| 舞钢市| 宿迁市| 陵水| 新丰县| 东安县| 文安县| 德清县| 边坝县| 吴忠市| 荔浦县| 汶川县| 德阳市| 遂平县| 辽阳县| 冕宁县| 镶黄旗| 宝鸡市| 垣曲县| 若尔盖县| 长丰县| 西平县| 广宁县| 余庆县| 屯留县| 龙江县| 长兴县| 江口县| 高唐县| 永济市| 南汇区| 山西省| 闽侯县| 汕头市| 德阳市|