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的更多相关文章

  1. LeetCode 312. Burst Balloons(戳气球)

    参考:LeetCode 312. Burst Balloons(戳气球) java代码如下 class Solution { //参考:https://blog.csdn.net/jmspan/art ...

  2. 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 ...

  3. [LeetCode] 312. Burst Balloons 打气球游戏

    Given n balloons, indexed from 0 to n-1. Each balloon is painted with a number on it represented by ...

  4. [LeetCode] 312. Burst Balloons 爆气球

    Given n balloons, indexed from 0 to n-1. Each balloon is painted with a number on it represented by ...

  5. 【LeetCode】312. Burst Balloons 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/burst-ba ...

  6. 【LeetCode】312. Burst Balloons

    题目: Given n balloons, indexed from 0 to n-1. Each balloon is painted with a number on it represented ...

  7. 312. Burst Balloons

    题目: Given n balloons, indexed from 0 to n-1. Each balloon is painted with a number on it represented ...

  8. 312 Burst Balloons 戳气球

    现有 n 个气球按顺序排成一排,每个气球上标有一个数字,这些数字用数组 nums 表示.现在要求你戳破所有的气球.每当你戳破一个气球 i 时,你可以获得 nums[left] * nums[i] * ...

  9. 【LeetCode】452. Minimum Number of Arrows to Burst Balloons 解题报告(Python)

    [LeetCode]452. Minimum Number of Arrows to Burst Balloons 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https ...

随机推荐

  1. c语言 相关小知识

    软件运行与内存关系(垃圾数据) 内存是在操作系统的统一管理下使用的! 1.软件在运行前需要向操作系统申请访问存储空间,在内存空闲空间足够时,操作系统将分配一段内存空间并将外存中软件拷贝一份存入该内存空 ...

  2. _CrtCheckMemory

    参考: _CrtCheckMemory MSDN 堆异常检查-MS vs stdio 编写程序经常会涉及到堆的申请,但是如果你向所申请堆里写数据,超过了你最开始申请的空间是,运行中就会发生中断. _C ...

  3. Head标签里面的dns-prefetch,preconnect,prefetch和prerender

    开始 今天突然心血来潮想起前端性能优化的问题,这基本是老生常谈的事情了,面试随便都能说上几个,但是还是有点疑问:就是Head标签了,记忆中Head可是藏龙卧虎,各种技能都有,当然这些不可能都一一记住, ...

  4. 前端面试题整理——HTML/CSS

    如何理解语义化: 对应的内容是用相应意思的标签,增加开发者和机器爬虫对代码的可读性. 块状元素和内联元素: 块状元素有:display:block/table:有div h1 h2 table ul  ...

  5. PAT B1014 福尔摩斯约会

    大侦探福尔摩斯接到一张奇怪的字条:我们约会吧! 3485djDkxh4hhGE 2984akDfkkkkggEdsb s&hgsfdk d&Hyscvnm.大侦探很快就明白了,字条上奇 ...

  6. MyBatis 及 MyBatis Plus 纯注解方式配置(Spring Boot + Postgresql)

    说明 当前的版本为 MyBatis 3.5.9 MyBatis Plus 3.5.1 Spring Boot 2.6.4 Postgresql 42.3.3 与 Spring Boot 结合使用 My ...

  7. Mybatis多表查询出现null字段

    写在前面 今天使用mybatis实现多表查询,记录一下其中遇到的坑 mybatis多表查询简介 mybatis多表查询主要有两个方式,通俗易懂的来说就是一个是查询少量属性(association),一 ...

  8. win11拖动窗口造成崩溃的问题

    问题描述 拖动窗口,随机概率出现 屏幕闪烁 屏幕黑屏 屏幕瞬间分屏 解决方法 windowes11贴吧大神给的方案 1,按下 win键+R 输入 regedit 进入注册表,进入以下路径:计算机\HK ...

  9. Python入门-异常处理

    异常处理 #try----else---- 会一起执行 #finally无论如何,最后都会执行 def main(): try: res = 10/2 print("开始执行计算:" ...

  10. 检查oracle是否是rac

    采样rac集群 [root@shfpdb02 disks]# cat /etc/redhat-release Red Hat Enterprise Linux Server release 6.4 ( ...