算法-股票交易最大收益
2018-03-14 11:34
429 查看
题目描述
在股市的交易日中,假设最多可进行两次买卖(即买和卖的次数均小于等于2),规则是必须一笔成交后进行另一笔(即买-卖-买-卖的顺序进行)。给出一天中的股票变化序列,计算一天可以获得的最大收益。
遍历算法(复杂度较高)public class MaxProfit {
public static void main(String[] args) {
int samples[] = {10,22,50,75,65,80};
int len = samples.length;
System.out.println(calcMaxProfit(samples, 0, len-1));
}
/**
* 计算最多两次买卖最大收益
*
* @param p_samples: 样本数组;
* @param startIndex: 样本数组开始索引;
* @param endIndex: 样本数组结束索引;
*
* 如果样本个数小于2,不够一次买卖操作,直接返回;
* 步骤一:寻找第一次买卖节点,计算本次收益,并与最大收益比较;
* 步骤二:提取剩余样本数据,寻找第二次买卖节点,计算本次收益,与第一次买卖收益累加后,和最大收益比较。
*
* */
public static long calcMaxProfit(int[] p_samples, int startIndex, int endIndex)
{
long maxProfit = 0; //返回值,最大利润;
long firstPartProfitPerloop = 0; //局部变量;第一次操作利润;
long secondPartProfitPerloop = 0;//局部变量;第二次操作利润;
int startSample = 0;//局部变量;买入值;
int endSample = 0;//局部变量;卖出值;
if (endIndex - startIndex >= 2)
{
for (int i = startIndex; i < endIndex; i++) //固定第一次买卖开始样本点
{
startSample = p_samples[i];
for (int j = i+1; j &l
93da
t;= endIndex; j++) //固定第一次买卖结束样本点
{
endSample = p_samples[j];
firstPartProfitPerloop = endSample - startSample;
maxProfit = compareAndSwap(maxProfit, firstPartProfitPerloop);
secondPartProfitPerloop = calcSecondTimeMaxProfit(p_samples, j+1, endIndex); //根据剩余样本数据,计算第二次买卖最大收益
maxProfit = compareAndSwap(maxProfit, firstPartProfitPerloop + secondPartProfitPerloop);
}
}
}
return maxProfit;
}
public static long compareAndSwap(long p_maxProfit, long p_loopProfit)
{
if (p_maxProfit < p_loopProfit)
{
p_maxProfit = p_loopProfit;
}
return p_maxProfit;
}
public static long calcSecondTimeMaxProfit(int[] p_samples, int startIndex, int endIndex)
{
long maxProfit = 0; //返回值,最大利润;
long secondPartProfitPerloop = 0;//局部变量;第二次操作利润;
int startSample = 0;//局部变量;买入值;
int endSample = 0;//局部变量;卖出值;
for (int i = startIndex; i < endIndex; i++) //固定买卖开始样本点
{
startSample = p_samples[i];
for (int j = i+1; j <= endIndex; j++) //固定买卖结束样本点
{
endSample = p_samples[j];
secondPartProfitPerloop = endSample - startSample;
maxProfit = compareAndSwap(maxProfit, secondPartProfitPerloop);
}
}
return maxProfit;
}
}
在股市的交易日中,假设最多可进行两次买卖(即买和卖的次数均小于等于2),规则是必须一笔成交后进行另一笔(即买-卖-买-卖的顺序进行)。给出一天中的股票变化序列,计算一天可以获得的最大收益。
遍历算法(复杂度较高)public class MaxProfit {
public static void main(String[] args) {
int samples[] = {10,22,50,75,65,80};
int len = samples.length;
System.out.println(calcMaxProfit(samples, 0, len-1));
}
/**
* 计算最多两次买卖最大收益
*
* @param p_samples: 样本数组;
* @param startIndex: 样本数组开始索引;
* @param endIndex: 样本数组结束索引;
*
* 如果样本个数小于2,不够一次买卖操作,直接返回;
* 步骤一:寻找第一次买卖节点,计算本次收益,并与最大收益比较;
* 步骤二:提取剩余样本数据,寻找第二次买卖节点,计算本次收益,与第一次买卖收益累加后,和最大收益比较。
*
* */
public static long calcMaxProfit(int[] p_samples, int startIndex, int endIndex)
{
long maxProfit = 0; //返回值,最大利润;
long firstPartProfitPerloop = 0; //局部变量;第一次操作利润;
long secondPartProfitPerloop = 0;//局部变量;第二次操作利润;
int startSample = 0;//局部变量;买入值;
int endSample = 0;//局部变量;卖出值;
if (endIndex - startIndex >= 2)
{
for (int i = startIndex; i < endIndex; i++) //固定第一次买卖开始样本点
{
startSample = p_samples[i];
for (int j = i+1; j &l
93da
t;= endIndex; j++) //固定第一次买卖结束样本点
{
endSample = p_samples[j];
firstPartProfitPerloop = endSample - startSample;
maxProfit = compareAndSwap(maxProfit, firstPartProfitPerloop);
secondPartProfitPerloop = calcSecondTimeMaxProfit(p_samples, j+1, endIndex); //根据剩余样本数据,计算第二次买卖最大收益
maxProfit = compareAndSwap(maxProfit, firstPartProfitPerloop + secondPartProfitPerloop);
}
}
}
return maxProfit;
}
public static long compareAndSwap(long p_maxProfit, long p_loopProfit)
{
if (p_maxProfit < p_loopProfit)
{
p_maxProfit = p_loopProfit;
}
return p_maxProfit;
}
public static long calcSecondTimeMaxProfit(int[] p_samples, int startIndex, int endIndex)
{
long maxProfit = 0; //返回值,最大利润;
long secondPartProfitPerloop = 0;//局部变量;第二次操作利润;
int startSample = 0;//局部变量;买入值;
int endSample = 0;//局部变量;卖出值;
for (int i = startIndex; i < endIndex; i++) //固定买卖开始样本点
{
startSample = p_samples[i];
for (int j = i+1; j <= endIndex; j++) //固定买卖结束样本点
{
endSample = p_samples[j];
secondPartProfitPerloop = endSample - startSample;
maxProfit = compareAndSwap(maxProfit, secondPartProfitPerloop);
}
}
return maxProfit;
}
}
相关文章推荐
- 风口之下,猪都能飞。当今中国股市牛市,真可谓“错过等七年”。 给你一个回顾历史的机会,已知一支股票连续n天的价格走势,以长度为n的整数数组表示,数组中第i个元素(prices[i])代表该股票第i天的股价。 假设你一开始没有股票,但有至多两次买入1股而后卖出1股的机会,并且买入前一定要先保证手上没有股票。若两次交易机会都放弃,收益为0。 设计算法,计算你能获得的最大收益。 输入数值范围:2<=n<
- 在股市的交易日中,假设最多可进行两次买卖(即买和卖的次数均小于等于2),规则是必须一笔成交后进行另一笔(即买-卖-买-卖的顺序进行)。给出一天中的股票变化序列,请写一个程序计算一天可以获得的最大收益。
- 假如一个数组存储了一个股票,在一天交易窗口内各时间点的股票价格(正整数),只允许一次买入和一次卖出,请提供一个算法,计算出通过买入和卖出可以得到的最大利润
- <每日一题>算法导论:最大股票收益
- 假如一个数组存储了一个股票,在一天交易窗口内各时间点的股票价格(正整数),只允许一次买入和一次卖出,请提供一个算法,计算出通过买入和卖出可以得到的最大利润
- LeetCode 121. Best Time to Buy and Sell Stock--股票买入后再卖出,求最大收益,最多交易一次
- Best Time to Buy and Sell Stock 股票交易最大收益
- 说你有一个数组,其中第i个元素是第i天给定股票的价格。设计一个算法来找到最大的利润,最多可以完成两个交易。
- 股票什么时候买进什么时候卖出收益最大----求最大子数组(算法导论第四章 4.1-3)
- Leetcode 121 Best Time to Buy and Sell Stock 单次买卖股票最大收益
- 如何买卖股票以获得最大收益 java算法
- LeetCode WIith JS || 121. Best Time to Buy and Sell Stock[求股票的最佳收益,只能交易一次]
- leetcode_121. Best Time to Buy and Sell Stock 求股票的最大收益 动态规划
- 股票最大收益问题
- 给出N天的股价,能买入和卖出股票,但是任何时候手上最多持有一份股票.允许交易K次求最多的收益.
- 算法导论之求股票最大收益
- 面试题38:股票最大收益问题
- 小米笔试-股票最大收益(动态规划)
- LeetCode - 股票交易的最大利润
- 股票最大收益问题3 Best Time to Buy and Sell Stock III