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

首頁 > 學(xué)院 > 開發(fā)設(shè)計 > 正文

Best Time to Buy and Sell Stock III

2019-11-14 09:07:59
字體:
供稿:網(wǎng)友

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


發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 郴州市| 华亭县| 望奎县| 延津县| 龙山县| 威海市| 聊城市| 手机| 泸州市| 集安市| 都江堰市| 乐东| 巨鹿县| 汤阴县| 胶州市| 洱源县| 剑川县| 郁南县| 浦东新区| 历史| 泰安市| 霸州市| 获嘉县| 板桥市| 龙南县| 大埔区| 涡阳县| 建水县| 永靖县| 柳林县| 驻马店市| 万源市| 福建省| 米易县| 阆中市| 娄底市| 通化市| 平远县| 溆浦县| 怀来县| 宜阳县|