leetcode problem 312. Burst Balloons 20190718

本文探讨了一道经典的动态规划问题——爆破气球获取最大金币数。通过递归和动态规划两种方法进行了解决,并详细分析了动态规划算法的设计思路及实现过程。

20190718

Description

Given n balloons, indexed from 0 to n-1. Each balloon is painted with a number on it represented by array nums. You are asked to burst all the balloons. If the you burst balloon i you will get nums[left] * nums[i] * nums[right] coins. Here left and right are adjacent indices of i. After the burst, the left and right then becomes adjacent.

Find the maximum coins you can collect by bursting the balloons wisely.

Note:

You may imagine nums[-1] = nums[n] = 1. They are not real therefore you can not burst them.
0 ≤ n ≤ 500, 0 ≤ nums[i] ≤ 100

Example:
Input: [3,1,5,8]
Output: 167 

Explanation:
nums = [3,1,5,8] --> [3,5,8] --> [3,8] --> [8] --> []
coins = 315 + 358 + 138 + 181 = 167

Solution

The first sight to see this problem, an idea came to my mind that: it should be dynamic programming.

But first I choose recursion to solve this problem, which is really slow and stucked at 24 / 70 test case.

Below is my first version code:

class Solution {
public:
    
    int maxCoins(vector<int>& nums) {
        if(nums.size()==0) return 0;
        if(nums.size()==1) return nums[0];
        int mm = 0;
        for(int i=0;i<nums.size();++i){
            int n = nums[i];
            // nums.erase(nums.begin()+i);
            if (i==0){
                int tmp = n*nums[1];
                nums.erase(nums.begin()+i);
                tmp += maxCoins(nums);
                mm = max(tmp,mm);
                nums.insert(nums.begin(),n);
            }
            else if(i==nums.size()-1){
                int tmp = n*nums[i-1];
                nums.erase(nums.begin()+i);
                tmp += maxCoins(nums);
                mm = max(tmp,mm);
                nums.insert(nums.end(),n);
            }
            else{
                int tmp = nums[i-1]*n*nums[i+1];
                nums.erase(nums.begin()+i);
                tmp += maxCoins(nums);
                mm = max(tmp,mm);
                nums.insert(nums.begin()+i,n);
            }
        }
        return mm;
    }
};

today I am a little bit lazy, so I turn to discussion, and found a elegant solution:

it split the problem into sub problems:
m a x C o i n s ( n u m s [ 1 : n ] ) = max ⁡ i m a x C o i n s ( n u m s [ 1 : i − 1 ] ) + n u m s [ i − 1 ] ∗ + m a x C o i n s ( n u m s [ i + 1 : n ] ) maxCoins(nums[1:n])=\max_{i} maxCoins(nums[1:i-1]) + nums[i-1]* +maxCoins(nums[i+1:n]) maxCoins(nums[1:n])=maximaxCoins(nums[1:i1])+nums[i1]+maxCoins(nums[i+1:n])
which means, it recurses on the last burst.

Below is the cpp code:

int maxCoins(vector<int>& nums) {
    int N = nums.size();
    nums.insert(nums.begin(), 1);
    nums.insert(nums.end(), 1);

    // rangeValues[i][j] is the maximum # of coins that can be obtained
    // by popping balloons only in the range [i,j]
    vector<vector<int>> rangeValues(nums.size(), vector<int>(nums.size(), 0));
    
    // build up from shorter ranges to longer ranges
    for (int len = 1; len <= N; ++len) {
        for (int start = 1; start <= N - len + 1; ++start) {
            int end = start + len - 1;
            // calculate the max # of coins that can be obtained by
            // popping balloons only in the range [start,end].
            // consider all possible choices of final balloon to pop
            int bestCoins = 0;
            for (int final = start; final <= end; ++final) {
                int coins = rangeValues[start][final-1] + rangeValues[final+1][end]; // coins from popping subranges
                coins += nums[start-1] * nums[final] * nums[end+1]; // coins from final pop
                if (coins > bestCoins) bestCoins = coins;
            }
            rangeValues[start][end] = bestCoins;
        }
    }
    return rangeValues[1][N];
}

tbc

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值