前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >专栏 >LeetCode 0312 - Burst Balloons

LeetCode 0312 - Burst Balloons

作者头像
Reck Zhang
发布于 2021-08-11 03:14:32
发布于 2021-08-11 03:14:32
24000
代码可运行
举报
文章被收录于专栏:Reck ZhangReck Zhang
运行总次数:0
代码可运行

Burst Balloons

Desicription

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:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
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

Solution

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
class Solution {
public:
    int maxCoins(std::vector<int>& nums) {
        auto balloons = std::vector<int>(nums.size() + 2);
        balloons[0] = balloons[balloons.size() - 1] = 1;
        for(int i = 1; i < balloons.size() - 1; i++) {
            balloons[i] = nums[i - 1];
        }
        
        auto dp = std::vector<std::vector<int>>(balloons.size(), std::vector<int>(balloons.size(), 0));
        for(int length = 2; length < balloons.size(); length++) {
            for(int left = 0; left + length < balloons.size(); left++) {
                int right = left + length;
                for(int i = left + 1; i < right; i++) {
                    dp[left][right] = std::max(dp[left][right], balloons[left] * balloons[i] * balloons[right] + dp[left][i] + dp[i][right]);
                }
            }
        }
        
        return dp[0][balloons.size() -1];
    }
};
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019-05-13,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
暂无评论
推荐阅读
编辑精选文章
换一批
LeetCode 312. 戳气球(DP,难)
有 n 个气球,编号为0 到 n-1,每个气球上都标有一个数字,这些数字存在数组 nums 中。
Michael阿明
2021/02/20
5080
LeetCode 312. 戳气球(DP,难)
戳气球
有 n 个气球,编号为0 到 n-1,每个气球上都标有一个数字,这些数字存在数组 nums 中。
你的益达
2020/08/05
1.2K0
每日算法系列【LeetCode 312】戳气球
有 n 个气球,编号为0 到 n-1,每个气球上都标有一个数字,这些数字存在数组 nums 中。
godweiyang
2020/03/24
6480
​LeetCode刷题实战452:用最少数量的箭引爆气球
算法的重要性,我就不多说了吧,想去大厂,就必须要经过基础知识和业务逻辑面试+算法面试。所以,为了提高大家的算法能力,后续每天带大家做一道算法题,题目就从LeetCode上面选 !
程序员小猿
2021/12/06
3330
一天一大 leet(戳气球)难度:困难-Day20200719
有 n 个气球,编号为 0 到 n-1,每个气球上都标有一个数字,这些数字存在数组 nums 中。
前端小书童
2020/09/24
2490
一天一大 leet(戳气球)难度:困难-Day20200719
啊?HOT 100 中最难的题是游戏厂最爱
回溯属于「爆搜」方案,时间复杂度是指数级别的,必然会 TLE(超时),因此回溯做出来的解法不算通过哈。
宫水三叶的刷题日记
2023/12/18
2180
啊?HOT 100 中最难的题是游戏厂最爱
【区间 DP】热门区间 DP 运用题
有 n 个气球,编号为 0 到 n - 1,每个气球上都标有一个数字,这些数字存在数组 nums 中。
宫水三叶的刷题日记
2023/10/25
2030
【区间 DP】热门区间 DP 运用题
LeetCode 题目解答—— 第 311 到 371 题
[Updated on 9/22/2017] 如今回头看来,里面很多做法都不是最佳的,有的从复杂度上根本就不是最优解,有的写的太啰嗦,有的则用了一些过于 tricky 的方法。我没有为了这个再更新,就让它们去吧。
四火
2022/07/19
8740
LeetCode 题目解答—— 第 311 到 371 题
算法刷题-戳气球(数组、动态规划)、Pow(x, n)(递归、数学)、编辑距离(字符串、动态规划)
有 n 个气球,编号为0 到 n - 1,每个气球上都标有一个数字,这些数字存在数组 nums 中。 现在要求你戳破所有的气球。戳破第 i 个气球,你可以获得 nums[i - 1] * nums[i] * nums[i + 1] 枚硬币。 这里的 i - 1 和 i + 1 代表和 i 相邻的两个气球的序号。如果 i - 1或 i + 1 超出了数组的边界,那么就当它是一个数字为 1 的气球。 求所能获得硬币的最大数量。
共饮一杯无
2023/02/10
2490
2021年CS保研经历(一):北邮CS夏令营、北师大AI夏令营、天津大学CS夏令营
  凡是过往,皆为序章!无论有没有去到自己想去的学校,无论有没有拿到自己满意的研究方向的offer,结果都已经注定,我们只能从新的起点出发,继续在其他地方发光发热!
Cyril-KI
2022/07/29
6170
2021年CS保研经历(一):北邮CS夏令营、北师大AI夏令营、天津大学CS夏令营
leetcode452. Minimum Number of Arrows to Burst Balloons
There are a number of spherical balloons spread in two-dimensional space. For each balloon, provided input is the start and end coordinates of the horizontal diameter. Since it's horizontal, y-coordinates don't matter and hence the x-coordinates of start and end of the diameter suffice. Start is always smaller than end. There will be at most 104balloons.
眯眯眼的猫头鹰
2019/11/12
4390
LeetCode 题目解答—— 第 416 到 460 题
从第 416 到第 460 题,跳过了需要付费的题目。付费的题目会单独放在一篇里面。
四火
2022/07/19
9500
LeetCode 题目解答—— 第 416 到 460 题
Leetcode 169 Majority Element
Given an array of size n, find the majority element. The majority element is the element that appears more than ⌊ n/2 ⌋ times. You may assume that the array is non-empty and the majority element always exist in the array. Credits: Special thanks to @t
triplebee
2018/01/12
4110
LeetCode Weekly Contest 44解题思路
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u014688145/article/details/77019115
用户1147447
2019/05/26
4220
牛顿法-LeetCode 319、322、324、331、332、389
初始时有 n 个灯泡关闭。第 1 轮,你打开所有的灯泡。第 2 轮,每两个灯泡你关闭一次。第 3 轮,每三个灯泡切换一次开关(如果关闭则开启,如果开启则关闭)。第 i 轮,每 i 个灯泡切换一次开关。对于第 n 轮,你只切换最后一个灯泡的开关。找出 n 轮后有多少个亮着的灯泡。
算法工程师之路
2019/12/24
5370
LeetCode 312. Burst Balloons(DP)
题解:区间DP dp[i][j] 表示i-j的所有灯泡都熄灭了之后,能获得最大价值
ShenduCC
2020/03/17
4130
leetcode 198 House Robber
You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping you from robbing each of them is that adjacent houses have security system connected and it will automatically contact the police if two adjacent houses were broken into on the same night.
流川疯
2019/01/18
4690
数字问题-LeetCode 452、453、454、455、456、459(KMP算法)
在二维空间中有许多球形的气球。对于每个气球,提供的输入是水平方向上,气球直径的开始和结束坐标。由于它是水平的,所以y坐标并不重要,因此只要知道开始和结束的x坐标就足够了。开始坐标总是小于结束坐标。平面内最多存在104个气球。
算法工程师之路
2020/02/13
7580
经典动态规划:戳气球问题
今天我们要聊的这道题「Burst Balloon」和之前我们写过的那篇 经典动态规划:高楼扔鸡蛋问题 分析过的高楼扔鸡蛋问题类似,知名度很高,但难度确实也很大。因此 labuladong 公众号就给这道题赐个座,来看一看这道题目到底有多难。
labuladong
2021/09/23
9560
leetcode刷题记录
该方法简单但是时间复杂度为O(n^2)。空间复杂度为O(1)。运行速度慢且内存空间消耗大。
Andromeda
2023/10/21
2550
相关推荐
LeetCode 312. 戳气球(DP,难)
更多 >
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
查看详情【社区公告】 技术创作特训营有奖征文