312. Burst Balloons - LeetCode
Question
https://leetcode.com/problems/burst-balloons/description/

Solution
题目大意是,有4个气球,每个气球上有个数字,现在依次打这4个气球(可以看成两边还各有一个气球即1,3,1,5,8,1),第一次打5这处气球,你的得分是左边气球上的数乘右边气球上的数再乘被打气球上的数。按任意顺序打这4个气球,求最终得分最高的值。
思考:正向思考

反向思考:

public int maxCoins(int[] nums) {
// 假设输入是[3,1,5,8]
// 构造一个新的气球数组[1,3,1,5,8,1]
int[] fullNums = new int[nums.length + 2];
fullNums[0] = 1;
fullNums[fullNums.length - 1] = 1;
for (int i = 1; i < fullNums.length - 1; i++) fullNums[i] = nums[i - 1];
// 存储从a气球到b气球的最高得分,初始化为-1,由于b>=a所以存储一半就可以了
int[][] result = new int[fullNums.length][fullNums.length];
for (int i = 1; i < result.length; i++) {
for (int j = i; j < result[0].length; j++) {
result[i][j] = -1;
}
}
// 相对于构造的数组 从1到4号气球才是我们要打的气球
return getMax(fullNums, result, 1, fullNums.length - 2);
}
private int getMax(int[] fullNums, int[][] result, int begin, int end) {
if (begin > end) return 0;
// 如果不是初始值,说明已经计算过该值了,直接返回结果
if (result[begin][end] != -1) return result[begin][end];
// 最后结果有4种,最后打3或1或5或8这四种可能,比较取最大值
for (int pos = begin; pos <= end; pos++) {
int left = getMax(fullNums, result, begin, pos - 1);
int mid = fullNums[begin - 1] * fullNums[pos] * fullNums[end + 1];
int right = getMax(fullNums, result, pos + 1, end);
int coin = left + mid + right;
if (coin > result[begin][end]) result[begin][end] = coin;
}
return result[begin][end];
}
Reference
312. Burst Balloons - LeetCode的更多相关文章
- LeetCode 312. Burst Balloons(戳气球)
参考:LeetCode 312. Burst Balloons(戳气球) java代码如下 class Solution { //参考:https://blog.csdn.net/jmspan/art ...
- LN : leetcode 312 Burst Balloons
lc 312 Burst Balloons 312 Burst Balloons Given n balloons, indexed from 0 to n-1. Each balloon is pa ...
- [LeetCode] 312. Burst Balloons 打气球游戏
Given n balloons, indexed from 0 to n-1. Each balloon is painted with a number on it represented by ...
- [LeetCode] 312. Burst Balloons 爆气球
Given n balloons, indexed from 0 to n-1. Each balloon is painted with a number on it represented by ...
- 【LeetCode】312. Burst Balloons 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/burst-ba ...
- 【LeetCode】312. Burst Balloons
题目: Given n balloons, indexed from 0 to n-1. Each balloon is painted with a number on it represented ...
- 312. Burst Balloons
题目: Given n balloons, indexed from 0 to n-1. Each balloon is painted with a number on it represented ...
- 312 Burst Balloons 戳气球
现有 n 个气球按顺序排成一排,每个气球上标有一个数字,这些数字用数组 nums 表示.现在要求你戳破所有的气球.每当你戳破一个气球 i 时,你可以获得 nums[left] * nums[i] * ...
- 【LeetCode】452. Minimum Number of Arrows to Burst Balloons 解题报告(Python)
[LeetCode]452. Minimum Number of Arrows to Burst Balloons 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https ...
随机推荐
- Python中对象、类型、元类之间的关系
Python里的对象.类型和元类的关系很微妙也很有意思. 1989年圣诞节期间,上帝很无聊,于是创造了一个世界. 对象 在这个世界的运转有几条定律. 1.一切都是对象 对象(object)是这个世界的 ...
- 002.MEMS应用在开关电源上,实现大功率超小型化
设计任务书 1.有关MEMS还有待具体了解 2.有关开关电源的目前难题也需要了解
- 论文解读( N2N)《Node Representation Learning in Graph via Node-to-Neighbourhood Mutual Information Maximization》
论文信息 论文标题:Node Representation Learning in Graph via Node-to-Neighbourhood Mutual Information Maximiz ...
- JS 中的日期时间操作计算实例
实例 一:已知日期格式为 "YYYY/MM/DD",计算相对于今天的天数差. function fromNow(date){ var mTimes = new Date(date) ...
- python大佬养成计划----flask_bootstrap装饰网页
flask_bootstrap Bootstrap 是 Twitter 开发的一个开源框架,它提供的用户界面组件可用于创建整洁且具有吸引力的网页,而且这些网页还能兼容所有现代 Web 浏览器. Boo ...
- python-人物风云榜(实现排名)
Description 又到了云之国一年一度的任务风云榜更新的大日子了.给出每个人风云力数值,需要你给出每个人的排名.注意,排名存在并列的情况. Input 一共有 22 行.第一行一个整数 n ,表 ...
- Python入门-多进程
1.获取本机CPU # 早期的CPU是单核:实现多个程序并行,在某一时间点,其实只有一个进程 # 后来硬件多核CPU:多个进程是并行执行. from multiprocessing import cp ...
- Java线程内存模型-JVM-底层原理
public class Demo1 { private static boolean initFlag=false; public static void main(String[] args) t ...
- 服务器的cpu 核心、线程
此版本有大范围改动,因为cpu作为一个大脑,所以更细致的进行了,相关的分析和阐述. 1.版本1. 2022.1.242.版本2: 2022.3.2 采集数据: ht2机器为物理机,cpu是4颗cpu, ...
- JetBrains Rider C# 学习②
前言 C#从入门到精通 链接:https://pan.baidu.com/s/1UveJI_f-c5Dul3GLIICRHg 提取码:1314 C#入门课程 刘铁猛 链接:https://pan.ba ...