312 - Burst Balloons

Problem

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 =  3*1*5      +  3*5*8    +  1*3*8      + 1*8*1   = 167

Code

public class Solution {
    
    public int maxCoins(int[] nums) {
        int len = nums.length;
        if (len == 0) {
            return 0;
        }
        int[] tmp = new int[len + 2];
        tmp[0] = 1;
        tmp[len + 1] = 1;
        for (int i = 1; i < len + 1; ++i) {
            tmp[i] = nums[i - 1];
        }
        int[][] dp = new int[len + 2][len + 2];
        for (int i = 2; i < len + 2; ++i) {
            for (int j = i; j < len + 2; ++j) {
                for (int k = 1; k < i; ++k) {
                    //System.out.printf("%d %d %d %d\n", k, dp[j][k], dp[k][j - i], nums[j] * nums[k] * nums[j - i]);
                    dp[j][i] = this.max(dp[j][i], dp[j][k] + dp[j - k][i - k] + tmp[j] * tmp[j - k] * tmp[j - i]);
                }
                //System.out.printf("dp[%d][%d]=%d\n", j, i, dp[j][i]);
            }
        }
        return dp[len + 1][len + 1];
    }
    
    private int max(int a, int b) {
        return a > b ? a : b;
    }
}

Link: https://leetcode.com/problems/burst-balloons/

这段代码是用来解决“最少数量的箭引爆气球”这个问题的经典算法实现。下面我会详细介绍它的功能和工作原理: ### 功能概述 给定一系列二维平面上表示气球位置的区间(即每一个气球对应一段水平线段),目标是找到最少的数量的垂直箭能够射爆所有的气球。 ### 代码分析 #### 1. **类及函数声明** ```cpp class Solution { public: int findMinArrowShots(vector<vector<int>>& points); }; ``` - 这里定义了一个名为Solution的类,其中包含公共成员函数findMinArrowShots。 - 此函数接收一个参数`points`,这是一个存储了多个气球区间的二维向量。 #### 2. **排序步骤** ```cpp ranges::sort(points, {}, [](auto& p) { return p[1]; }); // 按照右端点从小到大排序 ``` - 调用了C++20引入的新特性`std::ranges::sort`来对`points`进行排序。 - 排序基于每个气球区间的结束坐标(p[1]),使得数组按照这些坐标的递增次序排列。 #### 3. **初始化计数变量和前驱标记** ```cpp int ans = 0; // 计算所需最小箭矢数目 long long pre = LLONG_MIN; // 初始设置为不可能达到的小值 ``` - `ans`: 初始化为零,用来记录需要多少支箭才能击破全部气球。 - `pre`: 首先设定成非常小的一个数字(`LLONG_MIN`),以便后续比较时可以保证第一次循环一定能满足条件更新它。 #### 4. **遍历并计算最少箭数** ```cpp for (auto& p : points){ if (p[0] > pre){ // 当当前气球开始位置大于上一支箭的位置,则增加箭的数量 ans++; pre = p[1]; // 更新新的箭头放置在当前气球结尾处 } } ``` - 循环检查每个气球是否能被之前的一支箭射中。 - 条件判断:如果当前气球起始位置超过上次射击范围(pre),则需新增一只箭(即`ans++`)并将此新箭设于现气体球末端作为下次参照基准。 #### 5. **返回结果** ```cpp return ans; ``` 最后返回总所需的最少箭数量。 --- ### 示例说明 假设输入如下: ```txt [[10,16],[2,8],[1,6],[7,12]] ``` 经过上述过程处理后得到的结果将是: ```txt 2 ``` 解释:你可以分别从x=6 和 x=11两个地方发射两枚箭即可覆盖所有气球区域。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值