Fastrail08

Burst Balloons (INITIAL APPROACH)

Jul 13th, 2025 (edited)
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.17 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. //might not work, as it generates subsequence, so in the base case after processing all the balloons, some balloons will not be burst, some will be burst.
  4. //But our answer can only depends on what order ALL the ballons should be burst, not how many or which one.
  5. // We burst all but what order to burst to maximise profit
  6. //not subsequence ..subsequence(abc) = '', a, b, c, ab..... (all valid subsequence, we don't want this, some chosen some not)
  7. // permutations ... permutations(abc) = abc, bac,cba.... (all valid permutations, the whole string is present, just the order is changed, analogy = ALL balloons MUST be BURST, not some, to maximise profit, whether stated in question or not)
  8. //If I need to rob banks a,b and c ... to maximise the profit I will rob all the banks, why leave some????
  9. int burstBalloons(int level, vector<int> &nums){
  10.     if(level >= nums.size()){
  11.         return 0;
  12.     }
  13.     int burst = 0, notBurst = 0;
  14.     //levels = each balloon
  15.     //options = burst it or not
  16.    
  17.    
  18.     //if burst, take coins of left and right balloon and burst the level balloon
  19.     int coinInBallon = nums[level];
  20.     //burst the level balloon
  21.     nums[level] = 0;
  22.    
  23.     int leftBalloon = 1, rightBalloon = 1;
  24.     for(int i = 0, j = nums.size() - 1; i < nums.size(); i++, j--){
  25.         //get the just left balloon
  26.         if(i < level && nums[i] != 0){
  27.             leftBalloon = nums[i];
  28.         }
  29.         //get the just right balloon
  30.         if(j > level && nums[j] != 0){
  31.             rightBalloon = nums[j];
  32.         }
  33.     }
  34.     cout << leftBalloon << " " << coinInBallon << " " << rightBalloon << '\n';
  35.     burst = (leftBalloon * coinInBallon * rightBalloon) + burstBalloons(level + 1, nums);
  36.     //backtrack where balloon didn't burst
  37.     nums[level] = coinInBallon;
  38.     notBurst = burstBalloons(level + 1, nums);
  39.     //cout << level << " " << burst << " " << notBurst << '\n';
  40.     return max(burst, notBurst);
  41. }
  42.  
  43.  
  44. //try breaking all balloon at each level, try to get the order in which balloons are burst and maintain how many balloons are burst
  45.  
  46. int burstBalloons2(int burst, vector<int> &nums){
  47.     //all balloons burst, we may have a potent answer(base case)
  48.     //All the potent answer would have one thing in common, all the balloons should be burst (burst == nums.size())
  49.     //We are trying every order in which they burst(every possibility), so our answer(maxCoins) would be among one of them
  50.     //base case
  51.     if(burst >= nums.size()){
  52.         //no balloons left to burst, return 0 coins
  53.         return 0;
  54.     }
  55.     //level = some anchor around which options explored(what could be a meaningful name for anchor)
  56.     //options = breach each remaining balloon
  57.     //it is not about some balloons left standing and some balloons burst (not subset/subsequence)
  58.     //as we cannot maximise profit then (profit can be maximised only when all balloons burst)
  59.     //We have to burst every balloon, just the order in which they should burst matters, try every order then(sort of generating permutations)
  60.     int maxCoins = 0;
  61.     for(int k = 0; k < nums.size(); k++){
  62.         if(nums[k] != 0){
  63.             int coinsInBalloon = nums[k];
  64.             nums[k] = 0;
  65.             int leftBalloon = 1, rightBalloon = 1;
  66.             for(int i = 0, j = nums.size() - 1; i < nums.size(); i++, j--){
  67.                 //to get the just left balloon that is not burst
  68.                 if(i < k && nums[i] != 0){
  69.                     leftBalloon = nums[i];
  70.                 }
  71.                 //to get the just right balloon that is not burst
  72.                 if(j > k && nums[j] != 0){
  73.                     rightBalloon = nums[j];
  74.                 }
  75.             }
  76.             maxCoins = max(maxCoins, (leftBalloon * coinsInBalloon * rightBalloon) + burstBalloons2(burst + 1, nums));
  77.             nums[k] = coinsInBalloon;
  78.         }
  79.     }
  80.     return maxCoins;
  81. }
  82.  
  83. int main() {
  84.     // your code goes here
  85.     int n;
  86.     cin >> n;
  87.     vector<int> nums(n);
  88.     for(int i = 0; i < n; i++){
  89.         cin >> nums[i];
  90.     }
  91.     //memo key should be both (burst , nums)
  92.     // as burst is getting used
  93.     cout << burstBalloons2(0, nums) << '\n';
  94. }
  95.  
Advertisement
Add Comment
Please, Sign In to add comment