0
点赞
收藏
分享

微信扫一扫

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

MaxIncomeVO.java:


import lombok.Getter;
import lombok.Setter;

import java.io.Serializable;


 **/
@Getter
@Setter
public class MaxIncomeVO implements Serializable {

    /**
     * 当天处于持有股票状态时的最大收益( 可能是负数 )
     */
    private Integer maxIncome_holding;

    /**
     * 当天不处于持有( 已经卖出了或者还没有买入,反正就是当前手里没有股票的意思 )股票状态时的最大收益( 可能是负数 )
     */
    private Integer maxIncome_not_holding;

}

StockIncomeTest.java:


import com.alibaba.fastjson.JSONObject;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * 全程最多允许1次 "买入--》卖出" 操作 下的动态规划
 **/
public class StockIncomeTest {

    public static void main(String[] args) {
        // 生成随机的股票价格集合
        // 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 };
        int[] prices = generateRandomPriceList(20,20,1);
        System.out.print( "股票价格趋势:" );
        System.out.println( JSONObject.toJSONString( prices ) );

        // 使用动态规划算法计算最大收益
        MaxIncomeVO maxIncome = calculateMaxIncome(prices);
        System.out.println( "最大收益:" + maxIncome.getMaxIncome_not_holding() + " 元" );
    }

    private static MaxIncomeVO calculateMaxIncome(int[] prices) {
        if( prices == null){
            return null;
        }
        int size = prices.length;
        if( size <= 1 ){
            return null;
        }
        // 初始化 dp array
        MaxIncomeVO[] dp = new MaxIncomeVO[ size ];
        for( int i=0;i<size;i++ ){
            int price_curr = prices[i];
            MaxIncomeVO maxIncome_curr = new MaxIncomeVO();
            if( i == 0 ){
                // 第 1 天
                //  [ 7,1,3,5,6,8,3,4,5,2 ]
                // 因为今天是第一天,要么进行买入操作( 首次买入操作 ),要么不操作( 当前的最大收益为0 )
                maxIncome_curr.setMaxIncome_holding( 0 - price_curr );
                maxIncome_curr.setMaxIncome_not_holding( 0 );
            }else{
                // 第 2、3、4、5、... 天
                MaxIncomeVO maxIncome_prev = dp[i - 1];
                // 求今天处于持有股票状态下的最大收益:
                // 今天持有股票,可能今天进行了买入操作( 首次买入操作 ),可能今天没做任何操作( 所以昨天一定持有 )
                int maxIncome_holding_1 = 0 - price_curr;
                int maxIncome_holding_2 = maxIncome_prev.getMaxIncome_holding();
                maxIncome_curr.setMaxIncome_holding( Math.max( maxIncome_holding_1,maxIncome_holding_2 ) );

                // 求今天不处于持有股票状态下的最大收益:
                // 今天未持有股票,可能今天卖出了( 昨天是持有的 ),可能今天没做任何操作( 即昨天也未持有 ),
                int maxIncome_not_holding_1 = maxIncome_prev.getMaxIncome_holding() + price_curr;
                int maxIncome_not_holding_2 = maxIncome_prev.getMaxIncome_not_holding();
                maxIncome_curr.setMaxIncome_not_holding( Math.max( maxIncome_not_holding_1,maxIncome_not_holding_2 ) );
            }
            dp[ i ] = maxIncome_curr;
        }
        return dp[ size - 1 ];
    }


    /**
     * 生成随机的股票价格集合
     * @param size
     * @return
     */
    private static int[] generateRandomPriceList(int size,int maxValue,int minValue) {
        int[] prices = new int[size];
        Random random = new Random();
        int range = maxValue - minValue;
        for (int i = 0; i < size; i++) {
            prices[ i ] = random.nextInt( range ) + minValue;
        }
        return prices;
    }
}
举报

相关推荐

0 条评论