[LeetCode] Burst Balloons (Medium)
这题没有做出来. 自己的思路停留在暴力的解法, 时间复杂度很高:
- 初始化
maxCount = 0. - 对于当前长度为
k的数组nums, 从0到k - 1逐个选取第i个气球扎破- 计算扎破气球得到的金币数,
count = nums[i - 1] * nums[i] * nums[i + 1]. - 从
nums中删掉nums[i]. - 查询
m[nums]是否存在, 不存在则递归调用maxCoins(nums)并插入m[nums]. count += m[nums]后就是本轮的最优解, 若count > maxCount则更新maxCount.- 恢复
nums[i]回到nums中.
- 计算扎破气球得到的金币数,
- 返回
maxCount.
class Solution {
private:
map<vector<int>, int> m;
public:
int maxCoins(vector<int>& nums) {
int len = nums.size();
if (len <= 0) return 0;
if (len == 1) return nums[0];
int maxCount = 0;
for (int i = 0; i < len; ++i) {
int val = nums[i];
int count = nums[i];
if (i - 1 >= 0) {
count *= nums[i - 1];
}
if (i + 1 < len) {
count *= nums[i + 1];
}
nums.erase(nums.begin() + i);
auto it = m.find(nums);
if (it == m.end()) {
count += (m[nums] = maxCoins(nums));
} else {
count += it->second;
}
maxCount = max(maxCount, count);
nums.insert(nums.begin() + i, val);
}
return maxCount;
}
};
这个算法如果不加memo, 在第i次选择时有n - i个选择 (i = 0 ~ n-1), 所以一共有n!次选择, 每次选择都要进行O(n)时间复杂度的删气球和回填气球的操作查找vector的时间复杂度是多少?, 故时间复杂度O(n*n!), n层递归故空间复杂度O(n).
如上加了memo之后, 对于每种输入情况只计算一次, 对于k个气球, 有C(n, k)中情况 (k = 1~n), 一共是2^n种情况, 同上要考虑增删气球的时间复杂度, 所以时间复杂度最好也是O(n*2^n). 这2^n种情况都要保存在memo中, 每种输入的平均长度是O(n)级别的, 因此空间复杂度是O(n*2^n).
总之, TLE.
看了Share some analysis and explanations之后写下了下面的算法.
class Solution {
private:
map<pair<int, int>, int> m;
int maxCoins(vector<int> &nums, int left, int right, int lv, int rv) {
if (left > right) return 0;
if (left == right) return nums[left] * lv * rv;
auto coor = make_pair(left, right);
auto it = m.find(coor);
if (it != m.end()) {
return it->second;
}
int maxCount = 0;
for (int i = left; i <= right; ++i) {
int count = nums[i] * lv * rv
+ maxCoins(nums, left, i - 1, lv, nums[i])
+ maxCoins(nums, i + 1, right, nums[i], rv);
maxCount = max(maxCount, count);
}
m[coor] = maxCount;
return maxCount;
}
public:
int maxCoins(vector<int>& nums) {
int len = nums.size();
if (len <= 0) return 0;
return maxCoins(nums, 0, len - 1, 1, 1);
}
};
// Runtime: 1428ms
时间复杂度O(n^3). 起止点共有C(n, 2)个组合, 是O(n^2)级别的. 对于每个组合要遍历一遍, 找最大. 所以整体是O(n^3).
空间复杂度O(n^2).
小优化:
- 删掉0. (Runtime: 1300ms).
- 改用数组做map (Runtime: 36ms). 没想到数组比map好用这么多, 为什么?
class Solution {
private:
int maxCoins(vector<int> &nums, int left, int right, int lv, int rv, int* memo, int n) {
if (left > right) return 0;
if (left == right) return nums[left] * lv * rv;
if (memo[left * n + right] != 0) return memo[left * n + right];
int maxCount = 0;
for (int i = left; i <= right; ++i) {
int count = nums[i] * lv * rv
+ maxCoins(nums, left, i - 1, lv, nums[i], memo, n)
+ maxCoins(nums, i + 1, right, nums[i], rv, memo, n);
maxCount = max(maxCount, count);
}
memo[left * n + right] = maxCount;
return maxCount;
}
public:
int maxCoins(vector<int>& nums) {
int len = nums.size();
if (len <= 0) return 0;
int n = 0;
for (int x : nums) if (x > 0) nums[n++] = x;
int memo[n][n] = {};
return maxCoins(nums, 0, n - 1, 1, 1, (int*)memo, n);
}
};
标准答案:
class Solution {
public:
int maxCoins(vector<int>& iNums) {
int nums[iNums.size() + 2];
int n = 1;
for (int x : iNums) if (x > 0) nums[n++] = x;
nums[0] = nums[n++] = 1;
int dp[n][n] = {};
for (int k = 2; k <= n; ++k) {
for (int L = 0; L <= n - k; ++L) {
int R = L + k;
for (int i = L + 1; i < R; ++i) {
dp[L][R] = max(dp[L][R], nums[L] * nums[i] * nums[R] + dp[L][i] + dp[i][R]);
}
}
}
return dp[0][n - 1];
}
};
// Runtime: 12ms
其中dp[i][j]表示第i个气球到第j个气球能获取的最大金币数 (i最小为-1, j最大为n).
[LeetCode] Burst Balloons (Medium)的更多相关文章
- [LeetCode] Burst Balloons 打气球游戏
Given n balloons, indexed from 0 to n-1. Each balloon is painted with a number on it represented by ...
- LeetCode Burst Balloons
原题链接在这里:https://leetcode.com/problems/burst-balloons/ 题目: Given n balloons, indexed from 0 to n-1. E ...
- LeetCode 312. Burst Balloons(戳气球)
参考:LeetCode 312. Burst Balloons(戳气球) java代码如下 class Solution { //参考:https://blog.csdn.net/jmspan/art ...
- 【LeetCode】452. Minimum Number of Arrows to Burst Balloons 解题报告(Python)
[LeetCode]452. Minimum Number of Arrows to Burst Balloons 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https ...
- 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 870. Advantage Shuffle、134. Gas Station、452. Minimum Number of Arrows to Burst Balloons、316. Remove Duplicate Letters
870. Advantage Shuffle 思路:A数组的最大值大于B的最大值,就拿这个A跟B比较:如果不大于,就拿最小值跟B比较 A可以改变顺序,但B的顺序不能改变,只能通过容器来获得由大到小的顺 ...
- 动态规划-Burst Balloons
Burst Balloons Given n balloons, indexed from 0 to n-1. Each balloon is painted with a number on it ...
- [LeetCode] Minimum Number of Arrows to Burst Balloons 最少数量的箭引爆气球
There are a number of spherical balloons spread in two-dimensional space. For each balloon, provided ...
- [LeetCode] 312. Burst Balloons 打气球游戏
Given n balloons, indexed from 0 to n-1. Each balloon is painted with a number on it represented by ...
随机推荐
- Mac下sublime text 的“package control”安装
小伙伴们好,我根据昨晚的经历写一个小总结:关于“Mac下sublime text 的“package control”安装”.本来安装package control是一个无比简单的事情,即使是初次使用 ...
- 开源控件ViewPagerIndicator学习
导航条指示器.ViewPagerIndicator 地址 https://github.com/JakeWharton/ViewPagerIndicator Style是重用控件的一种技术.类似CSS ...
- linux下源码安装软件
在linux下的很多软件都是通过源码包方式发布的,这样做对于最终用户而言,虽然相对于二进制软件包,配置和编译起来繁琐点,但是它的可移植性却好得多,针对不同的体系结构,软件开发者往往仅需发布同一份源码包 ...
- 学习笔记_Java_day13_JSP三大指令()
JSP指令 1 JSP指令概述 JSP指令的格式:<%@指令名 attr1=”” attr2=”” %>,一般都会把JSP指令放到JSP文件的最上方,但这不是必须的. JSP ...
- window环境下 node.js 游戏框架pomelo 安装与启动
一.软件准备 1.1 下载node.js 1.2 下载python 2.5 < version <3.0 1.3 下载c++编译器(一般控制面板中-->程序和功能上已有,如果没有需要 ...
- oracle 高版本导出低版本数据库并且导入到低版本数据的方法
第一步:sqlplus system/egis@orcl as sysdba; 进入sqlplus (输入管理员用户名/密码@数据库密码) 第二步: create directory dumpdir ...
- 第3章 Struts2框架--2、完整的Struts2框架应用实例
1.建立一个Dynamic Web project,项目名:UserManager,把Struts2所必需的JAR复制到项目WEB-INF/lib目录下 2.修改web.xml文件,在web.xml文 ...
- (hdu)1950 Bridging signals(最长上升子序列)
Problem Description 'Oh no, they've done it again', cries the chief designer at the Waferland chip f ...
- Parameters
Quote from: http://ss64.com/nt/syntax-args.html Parameters A parameter (or argument) is any value pa ...
- 学习笔记---C++伪函数(函数对象)
C++里面的伪函数(函数对象)其实就是一个类重载了()运算符,这样类的对象在使用()操作符时,看起来就像一个函数调用一样,这就叫做伪函数. class Hello{ public: void oper ...