关闭

leetcode_c++:Divide and Conquer:Burst Balloons(312)

133人阅读 评论(0) 收藏 举报
分类:

https://leetcode.com/problems/burst-balloons/


class Solution {
public:
    int maxCoins(vector<int>& nums) {
        //remove all of zero item
        nums.erase(remove_if(nums.begin(), nums.end(), [](int n){return n==0;}), nums.end());

        //add 1 for head and tail
        nums.insert(nums.begin(),1);
        nums.push_back(1);

        int n = nums.size();
        vector< vector<int> > matrix(n, vector<int>(n,0));

        return maxCoins_DP(nums, matrix);
        return maxCoins_DC(nums, matrix, 0, n-1);
    }


    //Divide and Conquer
    //
    //  If we seprate the array to two part, left part and right part.
    //
    //  Then, we will find in this problem the left and right become adjacent 
    //  and have effects on the maxCoins in the future.
    //
    //  So, if we think reversely, if the balloon i is the last balloon of all to burst, 
    //  the left and right section now has well defined boundary and do not affect each other! 
    //  Therefore we can do either recursive method with memoization
    //
    int maxCoins_DC(vector<int>& nums, vector<vector<int>>& matrix, int low, int high) {
        if (low + 1 == high) return 0;
        if (matrix[low][high] > 0) return matrix[low][high];
        int result = 0;
        for (int i = low + 1; i < high; ++i){
            result = max(result,  nums[low] * nums[i] * nums[high] 
                                    + maxCoins_DC(nums, matrix, low, i) 
                                    + maxCoins_DC(nums, matrix, i, high));
        }
        matrix[low][high] = result;
        return result;
    }

    //Dynamic Programming
    // 
    //  using the same idea of above 
    //
    int maxCoins_DP(vector<int>& nums, vector<vector<int>>& dp) {
        int n = nums.size();
        for (int k = 2; k < n; ++k) {
            for (int low = 0; low < n - k; low++){
                int high = low + k;
                for (int i = low + 1; i < high; ++i)
                    dp[low][high] = max( dp[low][high],
                         nums[low] * nums[i] * nums[high] + dp[low][i] + dp[i][high]);
            }
        }
        return dp[0][n - 1];
    }

private:
    void printVector(vector<int>& nums) {
        cout << "nums: ";
        for (auto n: nums) {
            cout << n << ' ';
        }
        cout << '\n';
    }
};
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:116429次
    • 积分:6825
    • 等级:
    • 排名:第3536名
    • 原创:589篇
    • 转载:2篇
    • 译文:0篇
    • 评论:2条
    最新评论