当前位置:网站首页>LeetCode 312. 戳气球 每日一题
LeetCode 312. 戳气球 每日一题
2022-07-07 15:32:00 【@小红花】
问题描述
有 n 个气球,编号为0 到 n - 1,每个气球上都标有一个数字,这些数字存在数组 nums 中。
现在要求你戳破所有的气球。戳破第 i 个气球,你可以获得 nums[i - 1] * nums[i] * nums[i + 1] 枚硬币。 这里的 i - 1 和 i + 1 代表和 i 相邻的两个气球的序号。如果 i - 1或 i + 1 超出了数组的边界,那么就当它是一个数字为 1 的气球。
求所能获得硬币的最大数量。
示例 1:
输入:nums = [3,1,5,8]
输出:167
解释:
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
示例 2:输入:nums = [1,5]
输出:10
提示:
n == nums.length
1 <= n <= 300
0 <= nums[i] <= 100来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/burst-balloons
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
Java
class Solution {
public int maxCoins(int[] nums) {
int n = nums.length;
int[] arr = new int[n + 2];
arr[0] = arr[n + 1] = 1;
for(int i = 0;i < n;i++) arr[i + 1] = nums[i];
int[][] dp = new int[n + 2][n + 2];
for(int i = n - 1;i >= 0;i--){
for(int j = i + 2;j <= n + 1;j++){
//先找到两个边界i和j,然后戳他们之间的气球,开区间
for(int k = i + 1;k < j;k++){
//编号i到编号j最大得分为:编号i到编号k的得分+编号k到编号j的得分+戳编号为k的得分
dp[i][j] = Math.max(
dp[i][j],
dp[i][k] + dp[k][j] + arr[i] * arr[k] * arr[j]
);
}
}
}
//编号为1到编号为n的气球最高得分
return dp[0][n + 1];
}
}
边栏推荐
- 【医学分割】attention-unet
- 最新Android面试合集,android视频提取音频
- Opencv personal notes
- [PHP] PHP interface inheritance and interface multi inheritance principle and implementation method
- [C language] question set of X
- Binary search tree (features)
- DNS 系列(一):为什么更新了 DNS 记录不生效?
- 字节跳动Android面试,知识点总结+面试题解析
- Laravel service provider instance tutorial - create a service provider test instance
- Common training data set formats for target tracking
猜你喜欢
1亿单身男女“在线相亲”,撑起130亿IPO
【DesignMode】外观模式 (facade patterns)
The team of East China Normal University proposed the systematic molecular implementation of convolutional neural network with DNA regulation circuit
Tragedy caused by deleting the console statement
Spark Tuning (III): persistence reduces secondary queries
【Android -- 数据存储】使用 SQLite 存储数据
Sort out several important Android knowledge and advanced Android development interview questions
应用在温度检测仪中的温度传感芯片
Master this promotion path and share interview materials
网关Gateway的介绍与使用
随机推荐
【医学分割】attention-unet
ByteDance Android gold, silver and four analysis, Android interview question app
整理几个重要的Android知识,高级Android开发面试题
Advanced C language -- function pointer
Personal notes of graphics (4)
Three. JS series (2): API structure diagram-2
As an Android Developer programmer, Android advanced interview
A tour of gRPC:03 - proto序列化/反序列化
[C language] question set of X
[summary of knowledge] summary of notes on using SVN in PHP
Sort out several important Android knowledge and advanced Android development interview questions
How does laravel run composer dump autoload without emptying the classmap mapping relationship?
LeetCode 403. 青蛙过河 每日一题
使用JSON.stringify()去实现深拷贝,要小心哦,可能有巨坑
LeetCode 1043. 分隔数组以得到最大和 每日一题
Introduction to ThinkPHP URL routing
最新阿里P7技术体系,妈妈再也不用担心我找工作了
PHP realizes wechat applet face recognition and face brushing login function
【DesignMode】享元模式(Flyweight Pattern)
【DesignMode】代理模式(proxy pattern)