public static int maxProfit3(int[] prices) {
        if (prices.length == 0) return 0;

        int i, j, tmp, dp[][] = new int[3][prices.length + 1];

        // 初始化dp数组
        for (i = 0; i < 3; i++) {
            dp[i][0] = 0;
        }

        for (i = 0; i < prices.length + 1; i++) {
            dp[0][i] = 0;
        }

        // 动态规划dp[i][j] = max{dp[i - 1][j], dp[i - 1][k] + prices[j] - prices[k](0 <= k < j)}
         for (i = 1; i < 3; i++) {
            tmp = dp[i - 1][0] - prices[0];
            for (j = 1; j < prices.length + 1; j++) {
                dp[i][j] = dp[i][j - 1];

                if (tmp + prices[j - 1] > dp[i][j]) dp[i][j] = tmp + prices[j - 1];

                if (tmp < dp[i - 1][j] - prices[j - 1]) tmp = dp[i - 1][j] - prices[j - 1];
            }
        }

        return dp[2][prices.length];
    }