当前位置:网站首页>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];
}
}边栏推荐
- QT 图片背景色像素处理法
- DAPP defi NFT LP single and dual currency liquidity mining system development details and source code
- 面试题 01.02. 判定是否互为字符重排-辅助数组算法
- ATM system
- [C language] question set of X
- 一文读懂数仓中的pg_stat
- 水平垂直居中 方法 和兼容
- 作为Android开发程序员,android高级面试
- Three. JS series (2): API structure diagram-2
- 【Seaborn】组合图表、多子图的实现
猜你喜欢

最新2022年Android大厂面试经验,安卓View+Handler+Binder

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

Pycharm terminal enables virtual environment

【图像传感器】相关双采样CDS

Binary search tree (basic operation)

Horizontal and vertical centering method and compatibility
![[designmode] proxy pattern](/img/ed/642aebc7b49cbf4d30b517665b2438.png)
[designmode] proxy pattern

低代码(lowcode)帮助运输公司增强供应链管理的4种方式

Vs2019 configuration matrix library eigen

使用JSON.stringify()去实现深拷贝,要小心哦,可能有巨坑
随机推荐
偶然升职的内心独白
Personal notes of graphics (2)
Personal notes of graphics (3)
Build an all in one application development platform, light flow, and establish a code free industry benchmark
skimage学习(3)——Gamma 和 log对比度调整、直方图均衡、为灰度图像着色
Vs2019 configuration matrix library eigen
全网“追杀”钟薛高
Sort out several important Android knowledge and advanced Android development interview questions
AutoLISP series (1): function function 1
skimage学习(2)——RGB转灰度、RGB 转 HSV、直方图匹配
Personal notes of graphics (4)
Three. JS series (1): API structure diagram-1
typescript ts基础知识之tsconfig.json配置选项
值得一看,面试考点与面试技巧
作为Android开发程序员,android高级面试
logback. XML configure logs of different levels and set color output
[designmode] facade patterns
DAPP defi NFT LP single and dual currency liquidity mining system development details and source code
Cesium (4): the reason why gltf model is very dark after loading
QT 图片背景色像素处理法