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

原文地址:https://www.cnblogs.com/okokabcd/p/9169091.html

时间: 2024-10-08 08:18:40

312. Burst Balloons - LeetCode的相关文章

LeetCode 312. Burst Balloons(戳气球)

参考:LeetCode 312. Burst Balloons(戳气球) java代码如下 class Solution { //参考:https://blog.csdn.net/jmspan/article/details/51208865 public int maxCoins(int[] nums) { int[] balls = new int[nums.length+2]; balls[0] = 1; balls[balls.length - 1] = 1; int[][] coins

312. Burst Balloons

/* * 312. Burst Balloons * 2016-7-4 by Mingyang */ public int maxCoins(int[] iNums) { int[] nums = new int[iNums.length + 2]; int n = 1; for (int x : iNums) if (x > 0) nums[n++] = x; nums[0] = nums[n++] = 1; int[][] dp = new int[n][n]; for (int k = 2

【LeetCode】312. Burst Balloons

题目: Given n balloons, indexed from 0 to n-1. Each balloon is painted with a number on it represented by array nums. You are asked to burst all the balloons. If the you burst balloon i you will get nums[left] * nums[i] * nums[right] coins. Here left a

Leetcode 312. Burst Balloons

Given n balloons, indexed from 0 to n-1. Each balloon is painted with a number on it represented by array nums. You are asked to burst all the balloons. If the you burst balloon i you will get nums[left] * nums[i] * nums[right] coins. Here left and r

312 Burst Balloons 戳气球

现有 n 个气球按顺序排成一排,每个气球上标有一个数字,这些数字用数组 nums 表示.现在要求你戳破所有的气球.每当你戳破一个气球 i 时,你可以获得 nums[left] * nums[i] * nums[right] 个硬币. 这里的 left 和 right 代表和 i 相邻的气球的序号. 注意当你戳破了气球 i 后,气球 left 和气球 right 就变成了相邻的气球.求所能获得硬币数量的最大值.注意:(1) 你可以认为 nums[-1] = nums[n] = 1,但注意它们不是真

[LeetCode][Java]Burst Balloons

Burst Balloons Given n balloons, indexed from 0 to n-1. Each balloon is painted with a number on it represented by array nums. You are asked to burst all the balloons. If the you burst balloon i you will get nums[left] * nums[i] * nums[right] coins.

LeetCode | 0452. Minimum Number of Arrows to Burst Balloons用最少数量的箭引爆气球【Python】

LeetCode 0452. Minimum Number of Arrows to Burst Balloons用最少数量的箭引爆气球[Medium][Python][区间贪心] Problem LeetCode There are a number of spherical balloons spread in two-dimensional space. For each balloon, provided input is the start and end coordinates of

Leetcode: Burst Balloons

Given n balloons, indexed from 0 to n-1. Each balloon is painted with a number on it represented by array nums. You are asked to burst all the balloons. If the you burst balloon i you will get nums[left] * nums[i] * nums[right] coins. Here left and r

LeetCode OJ:Burst Balloons(击破气球)

Given n balloons, indexed from 0 to n-1. Each balloon is painted with a number on it represented by array nums. You are asked to burst all the balloons. If the you burst balloon i you will get nums[left] * nums[i] * nums[right] coins. Here left and r