• [leetcode-312-Burst Balloons]


    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: 
    (1) You may imagine nums[-1] = nums[n] = 1. They are not real therefore you can not burst them.
    (2) 0 ≤ n ≤ 500, 0 ≤ nums[i] ≤ 100

    Example:

    Given [3, 1, 5, 8]

    Return 167

        nums = [3,1,5,8] --> [3,5,8] -->   [3,8]   -->  [8]  --> []
       coins =  3*1*5      +  3*5*8    +  1*3*8      + 1*8*1   = 167
    

    Credits:
    Special thanks to @dietpepsi for adding this problem and creating all test cases.

    思路:

    看到所有的题解都是考虑最后一个气球爆炸的状态,这里有一些解释。

    Well, the nature way to divide the problem is burst one balloon and separate the balloons into 2 sub sections one on the left and one one the right. However, in this problem the left and right become adjacent and have effects on the maxCoins in the future.

    Then another interesting idea come up. Which is quite often seen in dp problem analysis. That is reverse thinking. Like I said the coins you get for a balloon does not depend on the balloons already burst. Therefore
    instead of divide the problem by the first balloon to burst, we divide the problem by the last balloon to burst.

    Why is that? Because only the first and last balloons we are sure of their adjacent balloons before hand!

    For the first we have nums[i-1]*nums[i]*nums[i+1] for the last we have nums[-1]*nums[i]*nums[n].

    OK. Think about n balloons if i is the last one to burst, what now?

    参考大牛分析:http://www.cnblogs.com/grandyang/p/5006441.html

    像这种求极值问题,我们一般都要考虑用动态规划Dynamic Programming来做,我们维护一个二维动态数组dp,其中dp[i][j]表示打爆区间[i,j]中的所有气球能得到的最多金币。题目中说明了边界情况,当气球周围没有气球的时候,旁边的数字按1算,这样我们可以在原数组两边各填充一个1,这样方便于计算。这道题的最难点就是找递归式,如下所示:

    dp[i][j] = max(dp[i][j], nums[i - 1]*nums[k]*nums[j + 1] + dp[i][k - 1] + dp[k + 1][j])                 ( i ≤ k ≤ j )

    有了递推式,我们可以写代码,我们其实只是更新了dp数组的右上三角区域,我们最终要返回的值存在dp[1][n]中,其中n是两端添加1之前数组nums的个数。参见代码如下:

    int maxCoins(vector<int>& nums)
         {
             int n = nums.size();
             nums.insert(nums.end(), 1);
             nums.insert(nums.begin(), 1);
    
             vector<vector<int>>dp(n+2,vector<int>(n+2,0));
             for (int len = 1; len <= n;len++)
             {
                 for (int left = 1; left <= n - len + 1;left++)
                 {
                     int right = left + len - 1;
                     for (int k = left; k <= right;k++)
                     {
                         dp[left][right] = max(dp[left][right], dp[left][k-1]+dp[k+1][right]+ nums[left-1]*nums[k]*nums[right+1]);
                     }
                 }
             }
             return dp[1][n];
         }

    参考:

    http://www.cnblogs.com/grandyang/p/5006441.html

    https://www.hrwhisper.me/leetcode-burst-balloons/

    https://discuss.leetcode.com/topic/30746/share-some-analysis-and-explanations

  • 相关阅读:
    Java动态代理详解
    (10) openssl dhparam(密钥交换)
    (9) openssl enc(对称加密)
    (8) openssl rsautl(签名/验证签名/加解密文件)和openssl pkeyutl(文件的非对称加密)
    (7) openssl dgst(生成和验证数字签名)
    (6) openssl passwd(生成加密的密码)
    (5) openssl speed(测试算法性能)和openssl rand(生成随机数)
    (4) openssl rsa/pkey(查看私钥、从私钥中提取公钥、查看公钥)
    (3) openssl genrsa(生成rsa私钥)
    (2) OpenSSL命令
  • 原文地址:https://www.cnblogs.com/hellowooorld/p/7081789.html
Copyright © 2020-2023  润新知