C++ | Leetcode C++题解之第312题戳气球

题目:

C++ | Leetcode C++题解之第312题戳气球_第1张图片

题解:

class Solution {
public:
    int maxCoins(vector& nums) {
        int n = nums.size();
        vector> rec(n + 2, vector(n + 2));
        vector val(n + 2);
        val[0] = val[n + 1] = 1;
        for (int i = 1; i <= n; i++) {
            val[i] = nums[i - 1];
        }
        for (int i = n - 1; i >= 0; i--) {
            for (int j = i + 2; j <= n + 1; j++) {
                for (int k = i + 1; k < j; k++) {
                    int sum = val[i] * val[k] * val[j];
                    sum += rec[i][k] + rec[k][j];
                    rec[i][j] = max(rec[i][j], sum);
                }
            }
        }
        return rec[0][n + 1];
    }
};

你可能感兴趣的:(经验分享,C++,Leetcode,题解)