51.Best Time to Buy and Sell Stock(动态规划)
来源:互联网 发布:电脑上怎么看淘宝直播 编辑:程序博客网 时间:2024/06/17 07:19
Say you have an array for which the ith element is the price of a given stock on day i.
If you were only permitted to complete at most one transaction (ie, buy one and sell one share of the stock), design an algorithm to find the maximum profit.
分析:题目要求只能进行一次买卖操作,所以采取动态规划的思想。
sum表示目前已经p[0..i-1]满足条件的最大收益;
low 和 high表示目前已经p[0..i-1]满足条件的买卖价格;
如果新的prices[i]比high大,则将其置为新的high;
prices[i]比low小,则将其置为新的high和low;
拿之前的sum和新生成的high-low的值进行比较,决定是否更新sum的值.
public int maxProfit(int[] prices) { int len = prices.length; if(len==0){ return 0; } int sum = 0;//sum表示目前已经p[0..i-1]满足条件的最大收益 int high = prices[0];//low 和 high表示目前已经p[0..i-1]满足条件的买卖价格 int low = prices[0]; for(int i =1;i<len;i++){if(prices[i]>high){//如果新的prices[i]比high大,则将其置为新的highhigh = prices[i];}else if(prices[i]<low){//prices[i]比low小,则将其置为新的high和lowhigh = prices[i];low = prices[i];}if(sum < high - low){//拿之前的sum和新生成的high-low的值进行比较,决定是否更新sum的值sum = high - low;}} return sum; }
后面做题的时候发现小米2016年校招的题目也用到了这个思想,不同的时候那个题目要求最多可以有两次的买卖操作。
风口之下,猪都能飞。当今中国股市牛市,真可谓“错过等七年”。 给你一个回顾历史的机会,已知一支股票连续n天的价格走势,以长度为n的整数数组表示,数组中第i个元素(prices[i])代表该股票第i天的股价。 假设你一开始没有股票,但有至多两次买入1股而后卖出1股的机会,并且买入前一定要先保证手上没有股票。若两次交易机会都放弃,收益为0。 设计算法,计算你能获得的最大收益。 输入数值范围:2<=n<=100,0<=prices[i]<=100 。
那么题目转化为可以用第i天把所有的n天的股票分开,用left[i]和right[i]表示以第i天分开之后允许买卖一次的最大收益,在左边[0,i]和右边[i+1,len-1]的股票中分别至多买卖一次的收益,即题目转化为用动态规划求在m天内最多只允许买卖一次股票的最大收益。然后比较求得left[i]和right[i]和的最大值。
/** * 计算你能获得的最大收益. * 已知一支股票连续n天的价格走势,以长度为n的整数数组表示,数组中第i个元素(prices[i])代表该股票第i天的股价。 * 假设你一开始没有股票,但有至多两次买入1股而后卖出1股的机会,并且买入前一定要先保证手上没有股票。若两次交易机会都放弃,收益为0 * * @param prices Prices[i]即第i天的股价 * @return 整型 */ public int calculateMax(int[] prices) { int len = prices.length; /*left[i]和right[i]表示以第i天分开,在左边[0,i]和右边[i+1,len-1]的股票中分别至多买卖一次的收益, * 即题目转化为用动态规划求在m天内最多只允许买卖一次股票的最大收益*/ int[] left = new int[len]; int[] right = new int[len]; int max = 0;//表示整体的最后整体收益 for(int i=0;i<len;i++){ left[i] = maxProfit(prices,0,i); right[i] = maxProfit(prices,i+1,len-1); if(max <= left[i]+right[i]){ max = left[i]+right[i]; } } return max; } /** * 计算在数组prices的下标[start,end]对应的股票最多允许买卖一次的最大收益. * 采用动态规划的思想,同leetcode中51.Best Time to Buy and Sell Stock(动态规划) * */ public int maxProfit(int[] prices,int start,int end) { int len = end - start + 1; if(len<=1){ return 0; } int sum = 0;//sum表示目前已经p[0..i-1]满足条件的最大收益int high = prices[start];//low 和 high表示目前已经p[0..i-1]满足条件的买卖价格int low = prices[start];for(int i =start+1;i<=end;i++){if(prices[i]>high){//如果新的prices[i]比high大,则将其置为新的highhigh = prices[i];}else if(prices[i]<low){//prices[i]比low小,则将其置为新的high和lowhigh = prices[i];low = prices[i];}if(sum < high - low){//拿之前的sum和新生成的high-low的值进行比较,决定是否更新sum的值sum = high - low;}}return sum; }
- 【动态规划】Best Time to Buy and Sell Stock IV
- Best Time to Buy and Sell Stock IV(动态规划)
- 动态规划-121. Best Time to Buy and Sell Stock
- 51.Best Time to Buy and Sell Stock(动态规划)
- LeetCode--Best Time to Buy and Sell Stock (贪心策略 or 动态规划)
- LeetCode--Best Time to Buy and Sell Stock III (动态规划)
- Best Time to Buy and Sell Stock(JAVA)--动态规划
- Best Time to Buy and Sell Stock III(JAVA)-动态规划
- 【LeetCode】Best Time to Buy and Sell Stock IV 动态规划dp解法(C++)
- [LeetCode 121]Best Time to Buy and Sell Stock(动态规划)
- ※ Leetcode - Dynamic Programming - 121. Best Time to Buy and Sell Stock(动态规划)
- best-time-to-buy-and-sell-stock系列(贪心与动态规划)
- Best Time to Buy and Sell Stock I II III IV (第四周 动态规划)
- 动态规划入门级教学(leetcode)121.Best Time to Buy and Sell Stock
- 动态规划的经典问题初探: Best Time to Buy and Sell Stock(DP)
- Leetcode(W8):121. Best Time to Buy and Sell Stock(动态规划)
- Leetcode(W9):188. Best Time to Buy and Sell Stock IV(动态规划)
- Leetcode(W9):123. Best Time to Buy and Sell Stock III(动态规划)
- SVN命令行代码大全
- 真机调试安装bug----app installation failed
- Jackon转化,属性名单引号 或者 缺失 配置
- IDEA快捷键
- centos DHCP server
- 51.Best Time to Buy and Sell Stock(动态规划)
- iOS程序运行时上下各有一个黑色部分,屏幕不满的问题
- css样式学习笔记一
- 简单分享开放平台的设计理念和思路
- POI操作Excel常用方法总结
- 2222
- JAVA8 十大新特性详解
- 自习算法:LCS Problem
- UVa 12019 - Doom's Day Algorithm