• leetcode123


    
    /**
     * <p>给定一个数组,它的第<em> </em><code>i</code> 个元素是一支给定的股票在第 <code>i</code><em> </em>天的价格。</p>
     *
     * <p>设计一个算法来计算你所能获取的最大利润。你最多可以完成 <strong>两笔 </strong>交易。</p>
     *
     * <p><strong>注意:</strong>你不能同时参与多笔交易(你必须在再次购买前出售掉之前的股票)。</p>
     *
     * <p> </p>
     *
     * <p><strong>示例 1:</strong></p>
     *
     * <pre>
     * <strong>输入:</strong>prices = [3,3,5,0,0,3,1,4]
     * <strong>输出:</strong>6
     * <strong>解释:</strong>在第 4 天(股票价格 = 0)的时候买入,在第 6 天(股票价格 = 3)的时候卖出,这笔交易所能获得利润 = 3-0 = 3 。
     * 随后,在第 7 天(股票价格 = 1)的时候买入,在第 8 天 (股票价格 = 4)的时候卖出,这笔交易所能获得利润 = 4-1 = 3 。</pre>
     *
     * <p><strong>示例 2:</strong></p>
     *
     * <pre>
     * <strong>输入:</strong>prices = [1,2,3,4,5]
     * <strong>输出:</strong>4
     * <strong>解释:</strong>在第 1 天(股票价格 = 1)的时候买入,在第 5 天 (股票价格 = 5)的时候卖出, 这笔交易所能获得利润 = 5-1 = 4 。
     * 注意你不能在第 1 天和第 2 天接连购买股票,之后再将它们卖出。
     * 因为这样属于同时参与了多笔交易,你必须在再次购买前出售掉之前的股票。
     * </pre>
     *
     * <p><strong>示例 3:</strong></p>
     *
     * <pre>
     * <strong>输入:</strong>prices = [7,6,4,3,1]
     * <strong>输出:</strong>0
     * <strong>解释:</strong>在这个情况下, 没有交易完成, 所以最大利润为 0。</pre>
     *
     * <p><strong>示例 4:</strong></p>
     *
     * <pre>
     * <strong>输入:</strong>prices = [1]
     * <strong>输出:</strong>0
     * </pre>
     *
     * <p> </p>
     *
     * <p><strong>提示:</strong></p>
     *
     * <ul>
     * <li><code>1 <= prices.length <= 10<sup>5</sup></code></li>
     * <li><code>0 <= prices[i] <= 10<sup>5</sup></code></li>
     * </ul>
     * <div><div>Related Topics</div><div><li>数组</li><li>动态规划</li></div></div><br><div><li> 1169</li><li> 0</li></div>
     */
    
    //leetcode submit region begin(Prohibit modification and deletion)
    class Solution {
        public int maxProfit(int[] prices) {
           return maxProfit(2,prices);
        }
    
        int maxProfit(int k, int[] prices) {
    
            int n  = prices.length;
    
            if (n <= 0) {
                return 0;
            }
    
            int[][][] dp = new int[n+1][k+1][2];
    
            for (int i = 0; i < n; i++) {
                for (int j = k; j >=1; j--) {
                    if(i-1==-1){
                        dp[i][j][0]=0;
                        dp[i][j][1]=-prices[i];
                        continue;
                    }
    
                    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[n-1][k][0];
        }
    }
    //leetcode submit region end(Prohibit modification and deletion)
    
    
  • 相关阅读:
    WebView用法与JS交互(2) 响应webview中的图片点击事件
    出栈序列(栈和队列)
    Log Files
    Mr. Frog’s Game
    Comparison of Android versions
    Linearization of the kernel functions in SVM
    Game of Nuts
    Easy Summation
    Automatic Judge
    Coprime Sequence
  • 原文地址:https://www.cnblogs.com/xiaoshahai/p/16476706.html
Copyright © 2020-2023  润新知