算法问题动态规划之股票购买最大利润(Java)

 public static void main(String[] args) {
   int[] stocks = {18,19,10,3,2,11,9,24,19};
    //一次购买时
   simpleStock(stocks);
    //三次购买时,打印最大利润值
   MutiStock(stocks,3);
    //三次购买时,打印购买方案
   MutiStock2(stocks,3);
}


/**
 * 一次买卖,如何实现最大收益.
  * 例如 {18,1,3,2,9,4,19}, 在股票为2时买入,19时,卖出。
  *
  * 分析:数组元素之间的最大差值.
  *
  * 解决:对于任何元素stocks[i],如果stocks[i] 大于 stocks[j](stocks[j]是前i-1个元素中的最小值),
  *
  * @param stocks
  */
 private static void simpleStock(int[] stocks){
     int smallValue, diff = Integer.MIN_VALUE;
     int startIndex=0,endIndex=0;
     smallValue = stocks[0];
     for (int i = 1; i < stocks.length; i++) {
         int item =  stocks[i];
         if (item > smallValue && diff < item - smallValue) {
             diff = item-smallValue;
             endIndex = i;
         }else if(item<smallValue){
             smallValue=item;
             startIndex = i;
         }
     }

     System.out.println(endIndex + " ,  " + startIndex + ", diff = " + diff);
 }

 /**
  * k次买卖,如何实现最大收益.
  * 例如 {18,1,3,2,9,4,5}, 。
  *
  * 分析:定义表达式s[k][j] = max(s[k][j-1],s[k-1][m]+ max (stock[j]- stock[m]))
  *
  *
  *
  * @param stocks
  */
 private static void MutiStock(int[] stocks,int K){

     int size = stocks.length;

    //创建一个二维数组 s[k][j];
     int [][] s = new int[K+1][stocks.length];
    //画动态规划图
     for (int k = 1; k <=K ; k++) {
         for (int i = 1; i<size; i++) {
             s[k][i] = s[k][i-1];
             for (int j = 0; j <i; j++) {
                 s[k][i] = Math.max(s[k][i],s[k-1][j]+stocks[i]-stocks[j]);
             }
         }
     }

    //打印数组
     for (int i = 0; i < stocks.length; i++) {
         System.out.print(stocks[i]+"  ");
     }
     System.out.println("");

     for (int i = 1; i <=K; i++) {
         for (int j = 0; j < stocks.length; j++) {
             System.out.print(s[i][j]+"  ");
         }
         System.out.println("");
     }

 }

 /**
  * k次买卖,如何实现最大收益.
  * 例如 {18,1,3,2,9,4,5}, 。
  *
  * 分析:定义表达式s[k][j] = max(s[k][j-1],s[k-1][m]+ max (stock[j]- stock[m]))
  *
  * @param stocks
  */
 private static void MutiStock2(int[] stocks,int K){

     int size = stocks.length;

     //创建一个二维数组 s[k][j];
     SNode [][] s = new SNode[K+1][stocks.length];
     s[1][0] = new Stock.SNode();
     s[2][0] = new Stock.SNode();
     s[3][0] = new Stock.SNode();
     for (int i = 0; i < size; i++) {
         s[0][i] = new SNode();
     }

     //画动态规划图
     for (int k = 1; k <=K ; k++) {
         for (int i = 1; i<size; i++) {

             s[k][i] = new SNode();
             s[k][i].value = s[k][i-1].value;
             s[k][i].preIndex = -1;
             int tmp =  s[k][i].value;
             for (int j = 0; j <i; j++) {
                 if(s[k][i].value>s[k-1][j].value+stocks[i]-stocks[j]){

                 }else {
                     s[k][i].value = s[k-1][j].value+stocks[i]-stocks[j];
                     s[k][i].preIndex = j;
                 }
             }
             if(s[k][i].value!=tmp){
                 System.out.println("time = " + k + ", i = " + s[k][i].preIndex +"买入,i = " + i+"卖出");
             }


         }
     }

     System.out.println(s[K][size-1]);
 }

 static class SNode{
     /**
      * 最大利润值
      */
     public int value;
     /**
      * 如果产生利润值时,是由当天卖出购票造成,这preIndex记录买入时间点
      */
     public int preIndex;
 }

你可能感兴趣的:(算法,动态规划)