• 动态规划求股票买入、卖出最大收益 java 实现( 最多可进行 1 次 “买入 ==> 卖出“ 操作 )


    MaxIncomeVO.java:
    1. import lombok.Getter;
    2. import lombok.Setter;
    3. import java.io.Serializable;
    4. **/
    5. @Getter
    6. @Setter
    7. public class MaxIncomeVO implements Serializable {
    8. /**
    9. * 当天处于持有股票状态时的最大收益( 可能是负数 )
    10. */
    11. private Integer maxIncome_holding;
    12. /**
    13. * 当天不处于持有( 已经卖出了或者还没有买入,反正就是当前手里没有股票的意思 )股票状态时的最大收益( 可能是负数 )
    14. */
    15. private Integer maxIncome_not_holding;
    16. }

    StockIncomeTest.java:
    1. import com.alibaba.fastjson.JSONObject;
    2. import java.util.ArrayList;
    3. import java.util.List;
    4. import java.util.Random;
    5. /**
    6. * 全程最多允许1次 "买入--》卖出" 操作 下的动态规划
    7. **/
    8. public class StockIncomeTest {
    9. public static void main(String[] args) {
    10. // 生成随机的股票价格集合
    11. // int[] prices = { 7,3,4,5,1,3,5,6,8,3,4,5,2,9,7,8,5,4,3,2,1,10,100,9,10,20,300,100,200,283 };
    12. int[] prices = generateRandomPriceList(20,20,1);
    13. System.out.print( "股票价格趋势:" );
    14. System.out.println( JSONObject.toJSONString( prices ) );
    15. // 使用动态规划算法计算最大收益
    16. MaxIncomeVO maxIncome = calculateMaxIncome(prices);
    17. System.out.println( "最大收益:" + maxIncome.getMaxIncome_not_holding() + " 元" );
    18. }
    19. private static MaxIncomeVO calculateMaxIncome(int[] prices) {
    20. if( prices == null){
    21. return null;
    22. }
    23. int size = prices.length;
    24. if( size <= 1 ){
    25. return null;
    26. }
    27. // 初始化 dp array
    28. MaxIncomeVO[] dp = new MaxIncomeVO[ size ];
    29. for( int i=0;i
    30. int price_curr = prices[i];
    31. MaxIncomeVO maxIncome_curr = new MaxIncomeVO();
    32. if( i == 0 ){
    33. // 第 1 天
    34. // [ 7,1,3,5,6,8,3,4,5,2 ]
    35. // 因为今天是第一天,要么进行买入操作( 首次买入操作 ),要么不操作( 当前的最大收益为0 )
    36. maxIncome_curr.setMaxIncome_holding( 0 - price_curr );
    37. maxIncome_curr.setMaxIncome_not_holding( 0 );
    38. }else{
    39. // 第 2、3、4、5、... 天
    40. MaxIncomeVO maxIncome_prev = dp[i - 1];
    41. // 求今天处于持有股票状态下的最大收益:
    42. // 今天持有股票,可能今天进行了买入操作( 首次买入操作 ),可能今天没做任何操作( 所以昨天一定持有 )
    43. int maxIncome_holding_1 = 0 - price_curr;
    44. int maxIncome_holding_2 = maxIncome_prev.getMaxIncome_holding();
    45. maxIncome_curr.setMaxIncome_holding( Math.max( maxIncome_holding_1,maxIncome_holding_2 ) );
    46. // 求今天不处于持有股票状态下的最大收益:
    47. // 今天未持有股票,可能今天卖出了( 昨天是持有的 ),可能今天没做任何操作( 即昨天也未持有 ),
    48. int maxIncome_not_holding_1 = maxIncome_prev.getMaxIncome_holding() + price_curr;
    49. int maxIncome_not_holding_2 = maxIncome_prev.getMaxIncome_not_holding();
    50. maxIncome_curr.setMaxIncome_not_holding( Math.max( maxIncome_not_holding_1,maxIncome_not_holding_2 ) );
    51. }
    52. dp[ i ] = maxIncome_curr;
    53. }
    54. return dp[ size - 1 ];
    55. }
    56. /**
    57. * 生成随机的股票价格集合
    58. * @param size
    59. * @return
    60. */
    61. private static int[] generateRandomPriceList(int size,int maxValue,int minValue) {
    62. int[] prices = new int[size];
    63. Random random = new Random();
    64. int range = maxValue - minValue;
    65. for (int i = 0; i < size; i++) {
    66. prices[ i ] = random.nextInt( range ) + minValue;
    67. }
    68. return prices;
    69. }
    70. }

  • 相关阅读:
    JavaFX 图像视图
    git仓删除当前仓且保留嵌套子仓--类似保留特定文件目录
    Flink学习16:算子介绍map
    前端需要知道的计算机网络知识----网络安全,自学网络安全,学习路线图必不可少,【282G】初级网络安全学习资源分享!
    小咪买东西--二分
    TDD、BDD、ATDD都是什么、有什么区别?(上)
    【kafka】——概述&安装
    AI绘图有哪些高效路径?
    机器学习入门教学——损失函数(交叉熵法)
    Selenium4.0 主流浏览器那些事儿
  • 原文地址:https://blog.csdn.net/heshiyuan1406146854/article/details/134482844