惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Cisco Talos Blog
Cisco Talos Blog
T
Threat Research - Cisco Blogs
P
Privacy International News Feed
S
Schneier on Security
P
Privacy & Cybersecurity Law Blog
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
云风的 BLOG
云风的 BLOG
P
Proofpoint News Feed
Scott Helme
Scott Helme
人人都是产品经理
人人都是产品经理
G
GRAHAM CLULEY
O
OpenAI News
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
PCI Perspectives
PCI Perspectives
GbyAI
GbyAI
宝玉的分享
宝玉的分享
Y
Y Combinator Blog
T
Troy Hunt's Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
C
CXSECURITY Database RSS Feed - CXSecurity.com
腾讯CDC
C
Check Point Blog
Spread Privacy
Spread Privacy
L
LINUX DO - 最新话题
Recent Announcements
Recent Announcements
大猫的无限游戏
大猫的无限游戏
P
Palo Alto Networks Blog
Hacker News: Ask HN
Hacker News: Ask HN
M
MIT News - Artificial intelligence
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
The Hacker News
The Hacker News
H
Hacker News: Front Page
Microsoft Azure Blog
Microsoft Azure Blog
I
InfoQ
T
Tor Project blog
Martin Fowler
Martin Fowler
博客园 - 叶小钗
罗磊的独立博客
C
Cyber Attacks, Cyber Crime and Cyber Security
H
Heimdal Security Blog
V
Vulnerabilities – Threatpost
Simon Willison's Weblog
Simon Willison's Weblog
Latest news
Latest news
WordPress大学
WordPress大学
G
Google Developers Blog
N
Netflix TechBlog - Medium
S
Security Affairs
S
Secure Thoughts
Know Your Adversary
Know Your Adversary

博客园 - 谢绝围观

LeetCode 910. Smallest Range II EAC 抓取CD为AAC文件 Binary Indexed Tree (Fenwick Tree) Lint Code 1365. Minimum Cycle Section LintCode 896. Prime Product 简明题解 UVa 1471 - Defense Lines Windows下安装配置MinGW GCC调试环境 详解LeetCode 137. Single Number II LeetCode 309. Best Time to Buy and Sell Stock with Cooldown LeetCode 84. Largest Rectangle in Histogram 修改注册表删除Windows资源管理器 “通过QQ发送” 右键菜单项 LeetCode 287. Find the Duplicate Number LeetCode 10. Regular Expression Matching LeetCode 117. Populating Next Right Pointers in Each Node II 在Windows Azure VM下搭建SSTP VPN - 谢绝围观 利用.NET Code Contracts实现运行时验证 Log4net 配置实例 解决Windows Server 2012 下利用RRAS创建VPN断线的问题 - 谢绝围观 慎用静态类static class
LeetCode 312. Burst Balloons
谢绝围观 · 2016-07-24 · via 博客园 - 谢绝围观

原题链接:https://leetcode.com/problems/burst-balloons/

这是一道比较复杂的动态规划问题,关键在于定义好独立的子问题。

原问题可以转化为求f(i, j)的最大值,当i == 0 && j == n时,就是我们要的答案。

把j逐次向后移动,依次求f(0, 1), f(0, 2)... f(0, n)的最大值。

求f(0, j)的最大值,可以转化为子问题:在0..j中找到元素k,当k是最后一个戳破时,f(1, k-1) + f(k+1, j) + num[0] * num[k] * num[j+1]值最大。

更进一步的思考:

1. 为什么最后一定是num[0] * num[k] * num[j+1]?如果是num[i]*num[k]*num[j+1]构成最大呢?其实这种情况已经被k = i-1这种case考虑过了:

2. 怎样找到max(f(1, k-1) + f(k+1, j) + num[0] * num[k] * num[j+1])?

我们可以令i = j-1..1,然后让k = j..i+1,找到使得f(i,j) = f(i+1, k-1) + f(k+1, j) + num[i] * num[k] * num[j + 1]  最大的k。因为i是从j-1向1移动的,所以每次可以利用前一轮计算的结果,也就是说f(i+1, k-1) 和 f(k+1, j)其实之前已经计算过。

因为每次j移动时,会对之前的乘积造成影响,所以需要重新计算一遍f(i, j)。

代码如下:

 1 class Solution {
 2 public:
 3     int maxCoins(vector<int>& nums) {
 4         if(nums.size() == 0){
 5             return 0;
 6         }
 7         if(nums.size() == 1){
 8             return nums[0];
 9         }
10         
11         int size = nums.size();
12         nums.push_back(1);
13         nums.insert(nums.begin(), 1);
14         vector<vector<int>> mem(size + 1, vector<int>(size + 1, 0));
15         mem[1][1] = nums[1]*nums[2];
16         mem[size][size] = nums[size-1]*nums[size];
17         for(int i = 2; i < size; i++){
18             mem[i][i] = nums[i-1]*nums[i]*nums[i+1];
19         }
20         for(int j = 2; j <= size; j++){
21             for(int i = j - 1; i >=1; i--){
22                 for(int k = j; k >= i; k--){
23                     int product = nums[k] * nums[i-1] * nums[j+1];
24                     int left = i < k ? mem[i][k-1]:0; 
25                     int right = j > k? mem[k+1][j]:0; 
26                     int sum = product + left + right;
27                     mem[i][j] = max(sum, mem[i][j]);
28                 }
29             }
30         }
31         
32         return mem[1][size];
33     }
34 };