当前位置:网站首页>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];
}
}
边栏推荐
- Opportunity interview experience summary
- [summary of knowledge] summary of notes on using SVN in PHP
- Module VI
- AutoLISP series (3): function function 3
- 正在准备面试,分享面经
- LocalStorage和SessionStorage
- Tidb cannot start after modifying the configuration file
- What is the difference between IP address and physical address
- Personal notes of graphics (4)
- How does laravel run composer dump autoload without emptying the classmap mapping relationship?
猜你喜欢
随机推荐
Laravel5.1 Routing - routing packets
Read PG in data warehouse in one article_ stat
第九届 蓝桥杯 决赛 交换次数
typescript ts基础知识之tsconfig.json配置选项
[hcsd celebrity live broadcast] teach the interview tips of big companies in person - brief notes
The latest interview experience of Android manufacturers in 2022, Android view+handler+binder
一文读懂数仓中的pg_stat
Common training data set formats for target tracking
23. 合并K个升序链表-c语言
Pisa-Proxy SQL 解析之 Lex & Yacc
Tidb cannot start after modifying the configuration file
dapp丨defi丨nft丨lp单双币流动性挖矿系统开发详细说明及源码
LeetCode 1155. 掷骰子的N种方法 每日一题
[Android -- data storage] use SQLite to store data
最新2022年Android大厂面试经验,安卓View+Handler+Binder
使用JSON.stringify()去实现深拷贝,要小心哦,可能有巨坑
应用在温度检测仪中的温度传感芯片
【Vulnhub靶场】THALES:1
time标准库
Introduction and use of gateway