当前位置:网站首页>LeetCode 312. Poke balloon daily
LeetCode 312. Poke balloon daily
2022-07-07 16:59:00 【@Little safflower】
Problem description
Yes n A balloon , The number is 0 To n - 1, Each balloon is marked with a number , There are arrays of these numbers nums in .
Now I ask you to prick all the balloons . Pierce the second i A balloon , You can get nums[i - 1] * nums[i] * nums[i + 1] Coin . there i - 1 and i + 1 For and on behalf of i The serial number of the two adjacent balloons . If i - 1 or i + 1 Beyond the bounds of the array , Then think of it as a number for 1 The balloon .
Find the maximum number of coins you can get .
Example 1:
Input :nums = [3,1,5,8]
Output :167
explain :
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
Example 2:Input :nums = [1,5]
Output :10
Tips :
n == nums.length
1 <= n <= 300
0 <= nums[i] <= 100source : Power button (LeetCode)
link :https://leetcode.cn/problems/burst-balloons
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
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++){
// First find two boundaries i and j, Then poke the balloon between them , Open range
for(int k = i + 1;k < j;k++){
// Number i To number j The maximum score is : Number i To number k Score of + Number k To number j Score of + The stamp number is k Score of
dp[i][j] = Math.max(
dp[i][j],
dp[i][k] + dp[k][j] + arr[i] * arr[k] * arr[j]
);
}
}
}
// The number is 1 To No n The balloon with the highest score
return dp[0][n + 1];
}
}边栏推荐
- Three. JS series (2): API structure diagram-2
- 射线与OBB相交检测
- LeetCode 1477. 找两个和为目标值且不重叠的子数组 每日一题
- 【DesignMode】模板方法模式(Template method pattern)
- ByteDance Android gold, silver and four analysis, Android interview question app
- As an Android Developer programmer, Android advanced interview
- 正在准备面试,分享面经
- [C language] question set of X
- QT picture background color pixel processing method
- Pycharm terminal enables virtual environment
猜你喜欢
最新高频Android面试题目分享,带你一起探究Android事件分发机制

【医学分割】attention-unet

字节跳动高工面试,轻松入门flutter

谈谈 SAP 系统的权限管控和事务记录功能的实现

Sort out several important Android knowledge and advanced Android development interview questions

skimage学习(1)

《产品经理必读:五种经典的创新思维模型》的读后感
最新Android高级面试题汇总,Android面试题及答案

使用JSON.stringify()去实现深拷贝,要小心哦,可能有巨坑

数据中台落地实施之法
随机推荐
模块六
Tidb cannot start after modifying the configuration file
logback. XML configure logs of different levels and set color output
LeetCode 403. 青蛙过河 每日一题
Pychart ide Download
偶然升职的内心独白
LeetCode 1986. 完成任务的最少工作时间段 每日一题
LeetCode 120. Triangle minimum path and daily question
typescript ts 基础知识之类型声明
AutoLISP series (2): function function 2
Talk about the realization of authority control and transaction record function of SAP system
Sqlserver2014+: create indexes while creating tables
01tire+ chain forward star +dfs+ greedy exercise one
【PHP】PHP接口继承及接口多继承原理与实现方法
最新阿里P7技术体系,妈妈再也不用担心我找工作了
Three. JS series (3): porting shaders in shadertoy
两类更新丢失及解决办法
【MySql进阶】索引详解(一):索引数据页结构
QT 图片背景色像素处理法
skimage学习(2)——RGB转灰度、RGB 转 HSV、直方图匹配