Java实现 LeetCode 312 戳气球
312. 戳气球
有 n 个气球,编号为0 到 n-1,每个气球上都标有一个数字,这些数字存在数组 nums 中。
现在要求你戳破所有的气球。每当你戳破一个气球 i 时,你可以获得 nums[left] * nums[i] * nums[right] 个硬币。 这里的 left 和 right 代表和 i 相邻的两个气球的序号。注意当你戳破了气球 i 后,气球 left 和气球 right 就变成了相邻的气球。
求所能获得硬币的最大数量。
说明:
你可以假设 nums[-1] = nums[n] = 1,但注意它们不是真实存在的所以并不能被戳破。
0 ≤ n ≤ 500, 0 ≤ nums[i] ≤ 100
示例:
输入: [3,1,5,8]
输出: 167
解释: nums = [3,1,5,8] --> [3,5,8] --> [3,8] --> [8] --> []
coins = 315 + 358 + 138 + 181 = 167
class Solution {
private int[][] dp;
public void fill(int[] nums, int from, int to) {
int max = 0, maxLeft, maxRight, result;
//假设第i个气球是最后被戳破的
for (int i = from; i <= to; i++) {
maxLeft = dp[from][i - 1];
maxRight = dp[i + 1][to];
result = maxLeft + maxRight + nums[from - 1] * nums[i] * nums[to + 1];
max = result > max ? result : max;
}
dp[from][to] = max;
}
public int maxCoins(int[] nums) {
int length = nums.length;
dp = new int[length + 2][length + 2];
for (int i = length + 1; i >= 0; i--) {
Arrays.fill(dp[i], 0);
}
int[] expandNums = new int[length + 2];
expandNums[0] = expandNums[length + 1] = 1;
System.arraycopy(nums, 0, expandNums, 1, length);
for (int span = 0; span < length; span++) {
//fill a diagonal
//assume the span (of array of length i) is i-1
for (int from = length - span; from > 0; from--) {
//fill dp[from][from + span]
fill(expandNums, from, from + span);
}
}
return dp[1][length];
}
}
Java实现 LeetCode 312 戳气球的更多相关文章
- Leetcode 312.戳气球
戳气球 有 n 个气球,编号为0 到 n-1,每个气球上都标有一个数字,这些数字存在数组 nums 中. 现在要求你戳破所有的气球.每当你戳破一个气球 i 时,你可以获得 nums[left] * n ...
- 312. 戳气球【困难】【区间DP】
题目链接 有 n 个气球,编号为0 到 n-1,每个气球上都标有一个数字,这些数字存在数组 nums 中. 现在要求你戳破所有的气球.每当你戳破一个气球 i 时,你可以获得 nums[left] * ...
- LeetCode 312. Burst Balloons(戳气球)
参考:LeetCode 312. Burst Balloons(戳气球) java代码如下 class Solution { //参考:https://blog.csdn.net/jmspan/art ...
- Java for LeetCode 060 Permutation Sequence
The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of the p ...
- Java for LeetCode 216 Combination Sum III
Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...
- Java for LeetCode 214 Shortest Palindrome
Given a string S, you are allowed to convert it to a palindrome by adding characters in front of it. ...
- Java for LeetCode 212 Word Search II
Given a 2D board and a list of words from the dictionary, find all words in the board. Each word mus ...
- Java for LeetCode 211 Add and Search Word - Data structure design
Design a data structure that supports the following two operations: void addWord(word)bool search(wo ...
- Java for LeetCode 210 Course Schedule II
There are a total of n courses you have to take, labeled from 0 to n - 1. Some courses may have prer ...
随机推荐
- 前端组件:支持多选,支持选项筛选的下拉框选择器(基于Jquery和Bootstrap)
效果图一:多选 效果图二:选项筛选 最后奉献源码,复制出来直接可用 <!DOCTYPE html> <html> <head> <meta charset=& ...
- Springboot中修改.properties文件为.yml文件时项目不能运行问题
可能很多小伙伴会考虑环境配置的问题,maven版本.idea版本什么的,其实没有必要 只要你之前.ies时能运行,那么环境配置就没有问题 不能运行的原因,一定是你的.yml文件的格式问题 .yml文件 ...
- git --添加多个文件
今天测试,发现之前写的auto testcase,有好多发生了改变,因此需要修改脚本重新上传至git当中. 对好几个test case script 进行了修改,之前只是一个一个的修改,这次是多个,经 ...
- 类型信息(反射,RTTI)
类型信息 1.java如何在运行时识别对象和类的信息 "传统的"RTTI run-time type identification ,假设我们在编译时已经知道了所有类型,在编译的时 ...
- PC、APP、H5三端测试的区别
一,针对同一个系统功能的测试,三端所测的业务流程是一样的 二,一般情况下手机端和PC端都对应一套后台服务,比如说笔者公司所开发的互联网金融平台,整个平台做了分布式服务架构,后台服务包括用户服务.交易服 ...
- Spring源码解析02:Spring IOC容器之XmlBeanFactory启动流程分析和源码解析
一. 前言 Spring容器主要分为两类BeanFactory和ApplicationContext,后者是基于前者的功能扩展,也就是一个基础容器和一个高级容器的区别.本篇就以BeanFactory基 ...
- Python中的时间与日期
本文简要介绍datetime,time模块的简要用法. datetime模块 datetime模块主要有四个主要的对象. date 处理年.月.日 time处理时.分.秒.微秒 datetime处理日 ...
- ESXI 6.5利用Centos7重置root密码
ESXI6.5宿主机,很久没有登录,再次登录的时候,发现忘记root密码了 1.先将刻录一个CentOS7的启动光盘或U盘,并将服务器的启动项修改为光盘 2.保存BIOS重启后,选择Troublesh ...
- C# 数据操作系列 - 9. EF Core 完结篇
0.前言 <EF Core>实际上已经可以告一段落了,但是感觉还有一点点意犹未尽.所以决定分享一下,个人在实际开发中使用EF Core的一些经验和使用的扩展包. 1. EF Core的异步 ...
- ql的python学习之路-day2
python中所有字符串操作 , )))), ))))#返回50个长度的字符串,不够左边就以0补充,可以用在十六进制补位(不常用)运行结果: My name is qinjiaxi---------- ...