Algoritmo: cómo obtener el máximo beneficio de la negociación de acciones

Como puerro que ha ganado unos meses de salario de su negocio secundario, obviamente es muy bueno en este tipo de problemas, pero en la práctica, el precio futuro de las acciones a menudo se desconoce, por lo que debe predecir y su empresa La oferta también afectará a la acción a su vez, por lo que nadie puede predecir completamente la tendencia de la acción. Los algoritmos que toman el valor máximo del backtracking son los siguientes. Es necesario dominarlo. Si cruza hacia atrás un día , puedes elegir uno. Este tipo de algoritmo, luego puedes pasar de 10,000 a 100 millones, tal vez un mes sea suficiente, oh, sí, si alguien puede cruzar el pasado, recuerda llevarme. . . . .

121. El mejor momento para comprar y vender acciones

// 股票只允许买卖一次 可利用贪心 找到最小的min 价格 再去找最大的max 价格 那么两者之间的差值就是 结果

public int maxProfit(int[] prices) {
    if (prices.length == 0) return 0;
    int min = prices[0];
    int res = 0;
    for (int i = 1; i < prices.length; i++) {
        res =Math.max(res,prices[i]-min);
        min = Math.min(min,prices[i]);
    }
    return res;
}

122. El mejor momento para comprar y vender acciones II

public int maxProfit(int[] prices) {
        if(prices.length == 0)return 0;
        int sell = 0;
        int buy = Integer.MIN_VALUE;
        for (int i = 0; i < prices.length; i++) {
            int t = sell;
            sell = Math.max(sell,prices[i] + buy);
            buy = Math.max(buy,t-prices[i]);
        }
        return sell;
}

123. El mejor momento para comprar y vender acciones III

public int maxProfit(int[] prices) {
        if (prices.length == 0)return 0;
        int k = 2;
        int[][][] dp = new int[prices.length][k+1][2];
        for (int i = 0; i < prices.length; i++) {
            for (int j = 1; j <= k ; j++) {
                if (i == 0){
                    dp[i][j][1] = -prices[i];
                }else {
                    dp[i][j][0] = Math.max(dp[i-1][j][0],prices[i]+dp[i-1][j][1]);
                    dp[i][j][1] = Math.max(dp[i-1][j][1],dp[i-1][j-1][0]-prices[i]);
                }
            }
        }
        return dp[prices.length -1][k][0];
}

188. El mejor momento para comprar y vender acciones IV

//这个是 买入k 次 但是 k 大于 数组长度的一半时候 实际和无限买入情况是一样的 可以加快速度
    public int maxProfit(int k, int[] prices) {
        if (prices.length == 0) return 0;
        if ( k > prices.length/2){
            return fastMaxProfit(prices);
        }
        int[][][] dp = new int[prices.length][k+1][2];
        for (int i = 0; i < prices.length; i++) {
            for (int j = 1; j <= k; j++) {
                if (i == 0){
                    dp[i][j][1] = -prices[i];
                }else {
                    dp[i][j][0] = Math.max(dp[i-1][j][0],dp[i-1][j][1] + prices[i]);
                    dp[i][j][1] = Math.max(dp[i-1][j][1],dp[i-1][j-1][0] - prices[i]);
                }
            }
        }
        return dp[prices.length - 1][k][0];
    }

    int fastMaxProfit(int[] price) {
        if (price.length == 0) return 0;
        int sell = 0;
        int buy = -price[0];
        for (int i = 0; i < price.length; i++) {
            int t = sell;
            sell = Math.max(sell,price[i] + buy);
            buy = Math.max(buy,t - price[i]);
        }
        return sell;
    }

309. El mejor momento para comprar y vender acciones incluye un período de congelación

//有冷冻期 就是 sell 保存多一天
public int maxProfit(int[] prices) {
    if (prices.length == 0) return 0;
    int sell = 0;
    int prev = 0;
    int buy = -prices[0];
    for (int i = 0; i < prices.length; i++) {
        int t = sell;
        sell = Math.max(sell,prices[i] + buy);
        buy = Math.max(buy,prev - prices[i]);
        prev = t;
    }
    return sell;

}

714. El mejor momento para comprar y vender acciones incluye las tarifas de gestión

// 思路 每次 交易的时候再减去手续费即可
public int maxProfit(int[] prices, int fee) {
    if (prices.length == 0) return 0;
    int sell = 0;
    int buy = -prices[0] - fee;
    for (int i = 0; i < prices.length; i++) {
        int t = sell;
        sell = Math.max(sell,prices[i] + buy);
        buy = Math.max(buy,t - prices[i] - fee);
    }
    return sell;

}

Wu Xie, Xiao San Ye, un pequeño novato en segundo plano, big data e inteligencia artificial. Presta atención a másarchivo

Supongo que te gusta

Origin blog.csdn.net/hu_lichao/article/details/112452023
Recomendado
Clasificación