|
| 1 | +// Source : https://leetcode.com/problems/burst-balloons/ |
| 2 | +// Author : Hao Chen |
| 3 | +// Date : 2016-01-17 |
| 4 | + |
| 5 | +/*************************************************************************************** |
| 6 | + * |
| 7 | + * Given n balloons, indexed from 0 to n-1. Each balloon is painted with a |
| 8 | + * number on it represented by array nums. |
| 9 | + * |
| 10 | + * You are asked to burst all the balloons. If the you burst |
| 11 | + * balloon i you will get nums[left] * nums[i] * nums[right] coins. Here left |
| 12 | + * and right are adjacent indices of i. After the burst, the left and right |
| 13 | + * then becomes adjacent. |
| 14 | + * |
| 15 | + * Find the maximum coins you can collect by bursting the balloons wisely. |
| 16 | + * |
| 17 | + * Note: |
| 18 | + * (1) You may imagine nums[-1] = nums[n] = 1. They are not real therefore you can |
| 19 | + * not burst them. |
| 20 | + * (2) 0 ≤ n ≤ 500, 0 ≤ nums[i] ≤ 100 |
| 21 | + * |
| 22 | + * Example: |
| 23 | + * |
| 24 | + * Given [3, 1, 5, 8] |
| 25 | + * |
| 26 | + * Return 167 |
| 27 | + * |
| 28 | + * nums = [3,1,5,8] --> [3,5,8] --> [3,8] --> [8] --> [] |
| 29 | + * coins = 3*1*5 + 3*5*8 + 1*3*8 + 1*8*1 = 167 |
| 30 | + * |
| 31 | + * Credits:Special thanks to @dietpepsi for adding this problem and creating all test |
| 32 | + * cases. |
| 33 | + ***************************************************************************************/ |
| 34 | + |
| 35 | + |
| 36 | + |
| 37 | +class Solution { |
| 38 | +public: |
| 39 | + int maxCoins(vector<int>& nums) { |
| 40 | + //remove all of zero item |
| 41 | + nums.erase(remove_if(nums.begin(), nums.end(), [](int n){return n==0;}), nums.end()); |
| 42 | + |
| 43 | + //add 1 for head and tail |
| 44 | + nums.insert(nums.begin(),1); |
| 45 | + nums.push_back(1); |
| 46 | + |
| 47 | + int n = nums.size(); |
| 48 | + vector< vector<int> > matrix(n, vector<int>(n,0)); |
| 49 | + |
| 50 | + return maxCoins_DP(nums, matrix); |
| 51 | + return maxCoins_DC(nums, matrix, 0, n-1); |
| 52 | + } |
| 53 | + |
| 54 | + |
| 55 | + //Divide and Conquer |
| 56 | + // |
| 57 | + // If we seprate the array to two part, left part and right part. |
| 58 | + // |
| 59 | + // Then, we will find in this problem the left and right become adjacent |
| 60 | + // and have effects on the maxCoins in the future. |
| 61 | + // |
| 62 | + // So, if we think reversely, if the balloon i is the last balloon of all to burst, |
| 63 | + // the left and right section now has well defined boundary and do not affect each other! |
| 64 | + // Therefore we can do either recursive method with memoization |
| 65 | + // |
| 66 | + int maxCoins_DC(vector<int>& nums, vector<vector<int>>& matrix, int low, int high) { |
| 67 | + if (low + 1 == high) return 0; |
| 68 | + if (matrix[low][high] > 0) return matrix[low][high]; |
| 69 | + int result = 0; |
| 70 | + for (int i = low + 1; i < high; ++i){ |
| 71 | + result = max(result, nums[low] * nums[i] * nums[high] |
| 72 | + + maxCoins_DC(nums, matrix, low, i) |
| 73 | + + maxCoins_DC(nums, matrix, i, high)); |
| 74 | + } |
| 75 | + matrix[low][high] = result; |
| 76 | + return result; |
| 77 | + } |
| 78 | + |
| 79 | + //Dynamic Programming |
| 80 | + // |
| 81 | + // using the same idea of above |
| 82 | + // |
| 83 | + int maxCoins_DP(vector<int>& nums, vector<vector<int>>& dp) { |
| 84 | + int n = nums.size(); |
| 85 | + for (int k = 2; k < n; ++k) { |
| 86 | + for (int low = 0; low < n - k; low++){ |
| 87 | + int high = low + k; |
| 88 | + for (int i = low + 1; i < high; ++i) |
| 89 | + dp[low][high] = max( dp[low][high], |
| 90 | + nums[low] * nums[i] * nums[high] + dp[low][i] + dp[i][high]); |
| 91 | + } |
| 92 | + } |
| 93 | + return dp[0][n - 1]; |
| 94 | + } |
| 95 | + |
| 96 | +private: |
| 97 | + void printVector(vector<int>& nums) { |
| 98 | + cout << "nums: "; |
| 99 | + for (auto n: nums) { |
| 100 | + cout << n << ' '; |
| 101 | + } |
| 102 | + cout << '\n'; |
| 103 | + } |
| 104 | +}; |
0 commit comments