国产探花免费观看_亚洲丰满少妇自慰呻吟_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;}


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 句容市| 东光县| 巩义市| 呼图壁县| 兴义市| 宝应县| 荔浦县| 长白| 甘泉县| 云霄县| 江都市| 中牟县| 济源市| 磐石市| 青岛市| 桦南县| 于田县| 凌云县| 汕头市| 通辽市| 仪陇县| 金秀| 大悟县| 珠海市| 六盘水市| 梁河县| 吴忠市| 华池县| 扶风县| 衡阳市| 台江县| 定远县| 临江市| 大城县| 陇川县| 商都县| 阜宁县| 连云港市| 芦溪县| 马鞍山市| 荔浦县|