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 getnums[left] * nums[i] * nums[right] coins. Here left and right are adjacent indices of i. After the burst, the left and right then becomes adjacent.

Find the maximum coins you can collect by bursting the balloons wisely.
- You may imagine nums[-1] = nums[n] = 1. They are not real therefore you can not burst them.
- 0 ≤ n ≤ 500, 0 ≤ nums[i] ≤ 100

Example

Given [4, 1, 5, 10]
Return 270

nums = [4, 1, 5, 10] burst 1, get coins 4 * 1 * 5 = 20
nums = [4, 5, 10]    burst 5, get coins 4 * 5 * 10 = 200
nums = [4, 10]       burst 4, get coins 1 * 4 * 10 = 40
nums = [10]          burst 10, get coins 1 * 10 * 1 = 10

Total coins 20 + 200 + 40 + 10 = 270

递归做法:
 1 public class Solution {
 2     /**
 3      * @param nums a list of integer
 4      * @return an integer, maximum coins
 5      */
 6     public int maxCoins(int[] nums) {
 7         // Write your code here
 8         if (nums == null || nums.length == 0)
 9             return 0;
10
11         ArrayList<Integer> list = new ArrayList<Integer>();
12         for (int index = 0; index < nums.length; index++) {
13             list.add(nums[index]);
14         }
15         return helper(list);
16     }
17
18     public int helper(ArrayList<Integer> list) {
19         if (list.size() == 1) {
20             return list.get(0);
21         }
22
23         int max = 0;
24         for (int i = 0; i < list.size(); i++) {
25             int value = 0;
26             if (i == 0) {
27                 value = list.get(0) * list.get(1);
28             } else if (i == list.size() - 1) {
29                 value = list.get(i - 1) * list.get(i);
30             } else {
31                 value = list.get(i - 1) * list.get(i) * list.get(i + 1);
32             }
33             ArrayList<Integer> temp = new ArrayList<Integer>(list);
34             temp.remove(i);
35
36             max = Math.max(max, value + helper(temp));
37         }
38         return max;
39     }
40 } 
 
时间: 2025-01-05 20:14:18

Burst Balloons的相关文章

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][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 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

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

【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

452. Minimum Number of Arrows to Burst Balloons——排序+贪心算法

There are a number of spherical balloons spread in two-dimensional space. For each balloon, provided input is the start and end coordinates of the horizontal diameter. Since it's horizontal, y-coordinates don't matter and hence the x-coordinates of s

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