leetcode_c++:Divide and Conquer:Burst Balloons(312)
来源:互联网 发布:软件系统投标书 编辑:程序博客网 时间:2024/06/05 14:30
https://leetcode.com/problems/burst-balloons/
class Solution {public: int maxCoins(vector<int>& nums) { //remove all of zero item nums.erase(remove_if(nums.begin(), nums.end(), [](int n){return n==0;}), nums.end()); //add 1 for head and tail nums.insert(nums.begin(),1); nums.push_back(1); int n = nums.size(); vector< vector<int> > matrix(n, vector<int>(n,0)); return maxCoins_DP(nums, matrix); return maxCoins_DC(nums, matrix, 0, n-1); } //Divide and Conquer // // If we seprate the array to two part, left part and right part. // // Then, we will find in this problem the left and right become adjacent // and have effects on the maxCoins in the future. // // So, if we think reversely, if the balloon i is the last balloon of all to burst, // the left and right section now has well defined boundary and do not affect each other! // Therefore we can do either recursive method with memoization // int maxCoins_DC(vector<int>& nums, vector<vector<int>>& matrix, int low, int high) { if (low + 1 == high) return 0; if (matrix[low][high] > 0) return matrix[low][high]; int result = 0; for (int i = low + 1; i < high; ++i){ result = max(result, nums[low] * nums[i] * nums[high] + maxCoins_DC(nums, matrix, low, i) + maxCoins_DC(nums, matrix, i, high)); } matrix[low][high] = result; return result; } //Dynamic Programming // // using the same idea of above // int maxCoins_DP(vector<int>& nums, vector<vector<int>>& dp) { int n = nums.size(); for (int k = 2; k < n; ++k) { for (int low = 0; low < n - k; low++){ int high = low + k; for (int i = low + 1; i < high; ++i) dp[low][high] = max( dp[low][high], nums[low] * nums[i] * nums[high] + dp[low][i] + dp[i][high]); } } return dp[0][n - 1]; }private: void printVector(vector<int>& nums) { cout << "nums: "; for (auto n: nums) { cout << n << ' '; } cout << '\n'; }};
0 0
- leetcode_c++:Divide and Conquer:Burst Balloons(312)
- leetCode刷题归纳-Divide and Conquer(312. Burst Balloons)
- leetcode_c++:Divide and Conquer:The Skyline Problem(218)
- leetcode_c++:Divide and Conquer:Expression Add Operators(282)
- leetcode_c++:Divide and Conquer:Kth Largest Element in an Array(215)
- leetcode_c++:Divide and Conquer:. Search a 2D Matrix II(240)
- leetcode_c++:Divide and Conquer:Different Ways to Add Parentheses(241)
- leetcode_c++:Divide and Conquer:Count of Smaller Numbers After Self(315)
- leetcode_c++:Divide and Conquer:Count of Range Sum(327)
- 分治算法(divide and conquer)
- 分治法(divide-and-conquer)
- 514. Freedom Trail (Divide and Conquer)
- 169. Majority Element (divide and conquer)
- 53. Maximum SubArray(divide and conquer)
- 493. Reverse Pairs(divide and conquer)
- Divide and conquer algorithm
- Divide-and-Conquer
- 分治 Divide and Conquer
- Rust: 如何运行你在Atom中写的Rust代码?
- Java中的UUID类
- 【游戏_忍者六道】状态机设计和实践之C++篇
- yii2 数据库操作sql
- Java8 Lamda表达式初体验
- leetcode_c++:Divide and Conquer:Burst Balloons(312)
- WebLogic 通过数据库的方式实现Session共享的配置
- Buildroot构建指南——工具链
- HDU 2041 超级楼梯 简单DP动态规划 递归
- 动画效果 android
- LeetCode攻关笔记——1.Two_Sum
- //HDU1848,SG函数,简单应用示例
- 阿狸的打字机
- 238. Product of Array Except Self **