编程练习(第十二周)
来源:互联网 发布:淘宝宝贝详情文字编辑 编辑:程序博客网 时间:2024/05/23 22:00
题目来源于:https://leetcode.com
312. Burst Balloons
DescriptionHintsSubmissionsSolutions
- Total Accepted: 24690
- Total Submissions: 58469
- Difficulty: Hard
- Contributor: LeetCode
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 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.
Note:
(1) You may imagine nums[-1] = nums[n] = 1
. They are not real therefore you can not burst them.
(2) 0 ≤ n
≤ 500, 0 ≤ nums[i]
≤ 100
Example:
Given [3, 1, 5, 8]
Return 167
nums = [3,1,5,8] --> [3,5,8] --> [3,8] --> [8] --> [] coins = 3*1*5 + 3*5*8 + 1*3*8 + 1*8*1 = 167
题解:
class Solution {public: vector<vector<int>> d; int maxCoins(vector<int>& nums) { if(nums.empty()){ return 0; } int size = nums.size(); nums.insert(nums.begin(), 1); nums.insert(nums.end(), 1); d = vector<vector<int>>(size + 2, vector<int>(size + 2, 0)); for(int len = 1; len <= size; ++len){ for(int start = 1;start + len - 1 <= size; ++start){ int end = start + len - 1; for(int k = start;k <= end; ++k){// k is the last one to be popped d[start][end] = max(d[start][end], d[start][k - 1] + d[k + 1][end] + nums[start - 1] * nums[k] * nums[end + 1]); } } } return d[1][size]; }};
0 0
- 编程练习(第十二周)
- (一三五)第十二章编程练习
- 第十二章编程练习(4)
- 第十二章 编程练习
- 第十二章编程练习(1)
- 第十二章编程练习(2)
- 第十二章编程练习(3)
- 第十二章编程练习(5)
- 第十二章编程练习(6)
- C++ primer plus 第十二章编程练习
- c primer plus第十二章编程练习答案(纯手打欢迎指错)
- 记录——《C Primer Plus (第五版)》第十章编程练习第十二题
- 【学习C++】C++ Primer Plus (第六版)第十二章编程练习1-6
- C primer plus(第五版)编程练习第十二章
- Android编程权威指南:第十二章挑战练习第二个
- 《Erlang 程序设计》练习答案 -- 第十二章 并发编程
- C++ Primer Plus第六版 第十二章 编程练习答案
- C Primer Plus 第十二章 编程练习 1-8题
- Oracle12C--变量的声明与赋值(二十五)
- c++通过jni调用java ,调用cmd执行脚本等类
- chrome开发模式下清除缓存问题
- hadoop基础之mapreduce
- 最大公约数最小公倍数
- 编程练习(第十二周)
- 如何在App中打开设置、appstore或其他App(区别iOS 10及之前系统)
- linux下生成core dump文件方法及设置
- ajax的简单用法发起对服务器的请求
- mysql数据库锁定机制
- h264 ES流文件通过计算first_mb_in_slice区分帧边界
- 剖析C/C++二维数组与指针的关系
- Java面向对象设计模式(十六)——迭代子模式(Iterator)
- Oracle12C--运算符(二十六)