LeetCode //C - 312. Burst Balloons

312. Burst Balloons

You are given n balloons, indexed from 0 to n - 1. Each balloon is painted with a number on it represented by an array nums. You are asked to burst all the balloons.

If you burst the i t h i^{th} ith balloon, you will get nums[i - 1] * nums[i] * nums[i + 1] coins. If i - 1 or i + 1 goes out of bounds of the array, then treat it as if there is a balloon with a 1 painted on it.

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

Example 1:

Input: nums = [3,1,5,8]
Output: 167
Explanation

nums = [3,1,5,8] --> [3,5,8] --> [3,8] --> [8] --> []

coins = 31 5 + 35 8 + 13 8 + 181 = 167

Example 2:

Input: nums = [1,5]
Output: 10

Constraints:
  • n == nums.length
  • 1 <= n <= 300
  • 0 <= nums[i] <= 100

From: LeetCode

Link: 312. Burst Balloons


Solution:

Ideas:
  • newNums: We create a new array newNums with additional 1s at the boundaries, which simplifies edge cases where we deal with the first or last balloon.

  • Dynamic Programming (dp): We use a 2D array dp where dp[left][right] represents the maximum coins that can be collected from bursting all the balloons between left and right (exclusive).

  • Window Size Iteration: We iterate over all possible lengths (len) of subarrays and calculate the maximum coins that can be obtained by bursting balloons in that subarray.

  • Time Complexity: The algorithm runs in O(n^3) time due to the triple nested loop, which is efficient enough given the constraints.

Code:
c 复制代码
int maxCoins(int* nums, int numsSize) {
    // Create a new array with 1s at the boundaries
    int* newNums = (int*)malloc((numsSize + 2) * sizeof(int));
    newNums[0] = 1;
    newNums[numsSize + 1] = 1;
    for (int i = 1; i <= numsSize; i++) {
        newNums[i] = nums[i - 1];
    }
    
    int n = numsSize + 2;
    int** dp = (int**)malloc(n * sizeof(int*));
    for (int i = 0; i < n; i++) {
        dp[i] = (int*)calloc(n, sizeof(int));
    }
    
    for (int len = 2; len < n; len++) { // len is the window size
        for (int left = 0; left < n - len; left++) {
            int right = left + len;
            for (int i = left + 1; i < right; i++) {
                dp[left][right] = fmax(dp[left][right], newNums[left] * newNums[i] * newNums[right] + dp[left][i] + dp[i][right]);
            }
        }
    }
    
    int result = dp[0][n - 1];
    
    for (int i = 0; i < n; i++) {
        free(dp[i]);
    }
    free(dp);
    free(newNums);
    
    return result;
}
相关推荐
paopaokaka_luck7 分钟前
基于Spring Boot+Vue的多媒体素材管理系统的设计与实现
java·数据库·vue.js·spring boot·后端·算法
CoderBob15 分钟前
【EmbeddedGUI】脏矩阵设计说明
c语言·单片机
浪里个浪的102416 分钟前
【C语言】计算3x3矩阵每行的最大值并存入第四列
c语言·开发语言·矩阵
视觉小萌新23 分钟前
VScode+opencv——关于opencv多张图片拼接成一张图片的算法
vscode·opencv·算法
2的n次方_34 分钟前
二维费用背包问题
java·算法·动态规划
敲敲敲-敲代码41 分钟前
游戏设计:推箱子【easyx图形界面/c语言】
c语言·开发语言·游戏
simple_ssn1 小时前
【C语言刷力扣】1502.判断能否形成等差数列
c语言·算法·leetcode
寂静山林1 小时前
UVa 11855 Buzzwords
算法
Curry_Math1 小时前
LeetCode 热题100之技巧关卡
算法·leetcode
ahadee1 小时前
蓝桥杯每日真题 - 第10天
c语言·vscode·算法·蓝桥杯