当前位置:网站首页>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];
}
}边栏推荐
- time标准库
- AutoLISP series (3): function function 3
- [medical segmentation] attention Unet
- The difference and working principle between compiler and interpreter
- [designmode] proxy pattern
- Three. JS series (3): porting shaders in shadertoy
- 水平垂直居中 方法 和兼容
- [C language] question set of X
- Deep listening array deep listening watch
- [designmode] template method pattern
猜你喜欢
![[designmode] facade patterns](/img/79/cde2c18e2ec8b08697662ac352ff90.png)
[designmode] facade patterns

null == undefined

Interface oriented programming

Binary search tree (features)

Pycharm terminal enables virtual environment
直接上干货,100%好评

Three. JS series (2): API structure diagram-2

The latest interview experience of Android manufacturers in 2022, Android view+handler+binder
3000 words speak through HTTP cache

node:504报错
随机推荐
[medical segmentation] attention Unet
Localstorage and sessionstorage
水平垂直居中 方法 和兼容
运算符
最新阿里P7技术体系,妈妈再也不用担心我找工作了
Cesium (4): the reason why gltf model is very dark after loading
Laravel changed the session from file saving to database saving
ATM system
LeetCode-SQL第一天
[PHP] PHP interface inheritance and interface multi inheritance principle and implementation method
最新Android高级面试题汇总,Android面试题及答案
面试题 01.02. 判定是否互为字符重排-辅助数组算法
LeetCode 1986. 完成任务的最少工作时间段 每日一题
【PHP】PHP接口继承及接口多继承原理与实现方法
[Android -- data storage] use SQLite to store data
[designmode] template method pattern
ATM系统
How can laravel get the public path
模块六
Horizontal and vertical centering method and compatibility