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 numsi - 1 * numsi * numsi + 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 <= numsi <= 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 dpleftright 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;
}
相关推荐
可编程芯片开发2 小时前
基于MATLAB的PEF湍流风场生成器模拟与仿真
算法
罗西的思考3 小时前
【Agentic RL / 强化学习 / OPD】OpenClaw-RL 源码阅读笔记 --- (9)--- Reward Judging
人工智能·算法·机器学习
Mr.HeBoYan4 小时前
一次持续三天才出现的丢包故障——深入解析 DPDK Memory Ordering、rte_ring 与 CPU Memory Barrier (下)
linux·网络·算法·架构·dpdk
wabs6664 小时前
关于图论【深度优先搜索的理论基础】
算法·深度优先·图论
先吃饱再说5 小时前
LeetCode 101. 对称二叉树
算法
是Dream呀6 小时前
人眼看不出的伪造,AI如何识别?实测合合信息跨模态鉴伪与去反光技术
算法
Discipline~Hai6 小时前
ARM01-ARM体系架构
linux·c语言·arm开发·架构
QN1幻化引擎6 小时前
SFA 信号场注意力:用8KB参数换248x KV Cache压缩,边缘设备也能跑长序列
人工智能·算法·gitee·gitlab
段一凡-华北理工大学6 小时前
向量数据库实战:选型、调优与落地~系列文章03:向量相似度算法全解:余弦、欧氏、内积,到底该用哪个?
大数据·数据库·人工智能·算法·机器学习·向量相似度·高炉炼铁
cjr_xyi8 小时前
AT1202Contest_c binarydigit 题解
c语言·c++·算法