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. Unsafe Rust 能做什么

    在不安全的 Rust 中唯一不同的是,你可以: 对原始指针进行解引用 调用"不安全"的函数(包括 C 函数.编译器的内建指令和原始分配器. 实现"不安全"的特性 ...

  2. poj_1852_Ants(复杂问题简单化)

    原题传送门 描述 一群蚂蚁走在长度为l cm的水平细杆上,以1cm/s的匀速.当一只行走的蚂蚁到达杆的一端,它就会掉下去.当两只蚂蚁相遇,它们会掉头像反方向走去.我们知道一只蚂蚁在杆上的初始位置,然而 ...

  3. PCB基础知识(一)

    在电子行业有一个关键的部件叫做PCB(printed circuit board,印刷电路板).这是一个太基础的部件,导致很多人都很难解释到底什么是PCB.这篇文章将会详细解释PCB的构成,以及在PC ...

  4. devicePixelRatio 那些事儿

    devicePixelRatio 那些事儿 设备像素比 window.devicePixelRatio 是设备上物理像素和设备独立像素的比例,即公式表示为:window.devicePixelRati ...

  5. Day05 - Flex 实现可伸缩的图片墙 中文指南

    Day05 - Flex 实现可伸缩的图片墙 中文指南 作者:liyuechun 简介:JavaScript30 是 Wes Bos 推出的一个 30 天挑战.项目免费提供了 30 个视频教程.30 ...

  6. 关于css中选择器的小归纳(一)

    关于css中选择器的小归纳 一.基本选择器 基本选择器是我们平常用到的最多的也是最便捷的选择器,其中有元素选择器(类似于a,div,body,ul),类选择器(我们在HTML标签里面为其添加的clas ...

  7. npm权限不够(安装什么都报错)

    问题 Windows下使用npm安装任何包都报错, Windows下使用npm显示权限不够 如图: 解决方法    1. 方法一    使用管理员权限打开 命令窗口,  治标不治本!!!!不推荐    ...

  8. Jedis的基本操作

    jedis jedis 是 redis推荐的java客户端.通过Jedis我们可以很方便地使用java代码的方式,对redis进行操作.jedis使用起来比较简单,它的操作方法与redis命令相类似. ...

  9. Java报错:Error creating bean with name 'testController': Injection of resource dependencies failed

    报错如下: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'testCo ...

  10. 如何得到个性化banner

    介绍 有时候用一些脚本工具,会有一些由其他字符组成的字符.(如下面这个我还在写的) 使用 kali自带了这个工具 -- figlet. figlet AuToIP 就可以得到上面的字符啦! 另外如果想 ...