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

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

Best Time to Buy and Sell Stock III

2019-11-11 07:16:05
字體:
來源:轉載
供稿:網友

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;}


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 凌源市| 平顺县| 德昌县| 三原县| 闽清县| 赣州市| 二连浩特市| 台北县| 沅陵县| 嫩江县| 南陵县| 郸城县| 加查县| 斗六市| 扎囊县| 婺源县| 巴彦淖尔市| 乌拉特前旗| 新和县| 扶绥县| 陆河县| 北辰区| 赤壁市| 五峰| 洞头县| 双柏县| 舟山市| 汕头市| 紫金县| 连江县| 肇东市| 东丰县| 兰西县| 从化市| 吉林省| 老河口市| 军事| 磴口县| 保靖县| 枣庄市| 台江县|