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

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

Best Time to Buy and Sell Stock III

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

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


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 张家界市| 白山市| 什邡市| 茂名市| 开原市| 清水县| 禄丰县| 百色市| 景德镇市| 龙南县| 偃师市| 定安县| 芮城县| 高阳县| 许昌市| 荣成市| 和龙市| 清苑县| 旬阳县| 宣汉县| 正阳县| 海晏县| 车致| 桐乡市| 洪泽县| 应城市| 新巴尔虎右旗| 察哈| 南城县| 靖州| 红河县| 玉树县| 康马县| 周口市| 吉林省| 苍山县| 临武县| 盐山县| 曲阳县| 长宁县| 双城市|