@@ -0,0 +1,1154 @@
+/**%0A * Problem Link: https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iv/%0A * %0A * Solution: %0A *      http://blog.csdn.net/linhuanmars/article/details/23236995%0A *      http://www.cnblogs.com/grandyang/p/4295761.html%0A */%0A%0Apublic class Solution %7B%0A    public int maxProfit(int k, int%5B%5D prices) %7B%0A        int n = prices.length;%0A        if(n %3C 2 %7C%7C k %3C= 0) return 0;%0A        %0A    %09if (k %3E= n)%0A    %09%09return solveMaxProfit(prices);%0A %0A        int%5B%5D%5B%5D local = new int%5Bn%5D%5Bk+1%5D;%0A        int%5B%5D%5B%5D global = new int%5Bn%5D%5Bk+1%5D;%0A        %0A        for(int i=1; i%3Cn; i++) %7B%0A            int diff = prices%5Bi%5D - prices%5Bi-1%5D;%0A            for(int j=1; j%3C=k; j++) %7B%0A                local%5Bi%5D%5Bj%5D = Math.max(global%5Bi-1%5D%5Bj-1%5D + Math.max(0, diff), local%5Bi-1%5D%5Bj%5D + diff);%0A                global%5Bi%5D%5Bj%5D = Math.max(local%5Bi%5D%5Bj%5D, global%5Bi-1%5D%5Bj%5D);%0A            %7D%0A        %7D%0A        return global%5Bn-1%5D%5Bk%5D;%0A    %7D%0A    %0A    private int solveMaxProfit(int%5B%5D prices) %7B%0A        int res = 0;%0A        for (int i = 1; i %3C prices.length; ++i) %7B%0A            if (prices%5Bi%5D - prices%5Bi - 1%5D %3E 0) %7B%0A                res += prices%5Bi%5D - prices%5Bi - 1%5D;%0A            %7D%0A        %7D%0A        return res;%0A    %7D%0A%7D%0A

Create Best_Time_to_Buy_and_Sell_Stock_IV.java