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

原创 2016年08月28日 15:43:54

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';
    }
};
版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

【leetcode】312. Burst Balloons

Difficulty:Hard Given n balloons, indexed from 0 to n-1. Each balloon is painted with a numbe...

【LEETCODE】312-Burst Balloons

Given n balloons, indexed from 0 to n-1. Each balloon is painted with a number on it represented by ...

[leetcode] 312. Burst Balloons 解题报告

题目链接:https://leetcode.com/problems/burst-balloons/ Given n balloons, indexed from 0 to n-1. Each ...

LeetCode 312. Burst Balloons

题目312. Burst Balloons思路考虑最后一个戳破的气球,这个气球的位置可以把整个气球数组分成两部分。 注意是最后一个,不是第一个,之前一直没转过弯来。 利用动态规划思路:动态规划数组...

LeetCode 312. Burst Balloons(戳气球)

原题网址:https://leetcode.com/problems/burst-balloons/ Given n balloons, indexed from 0 to n-1. Each ...
  • jmspan
  • jmspan
  • 2016年04月21日 14:39
  • 1318

[leetcode] 312. Burst Balloons

Given n balloons, indexed from 0 to n-1. Each balloon is painted with a number on it represented b...

【Leetcode】312. Burst Balloons

第一种思路可以采用backtracking。把一个list传递下去,每一步可以选择任意一个删除,然后再递归处理下一个,比较简单。 public int maxCoins1(int[] nums) ...

Leetcode 312. Burst Balloons

问题描述 Given n balloons, indexed from 0 to n-1. Each balloon is painted with a number...

LeetCode 题目:312. Burst Balloons

题目地址:点击打开链接  题目描述:Given n balloons, indexed from 0 to n-1. Each balloon is painted with a number on...

leetcode 312. Burst Balloons

Given n balloons, indexed from 0 to n-1. Each balloon is painted with a number on it represented by ...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:leetcode_c++:Divide and Conquer:Burst Balloons(312)
举报原因:
原因补充:

(最多只允许输入30个字)