RainX_69

Important problems from Partition DP | MUST DO

Mar 3rd, 2023
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.97 KB | Source Code | 0 0
  1. MUST DO PROBLEMS:-
  2. 1) https://leetcode.com/problems/minimum-cost-to-cut-a-stick/
  3. 2) https://leetcode.com/problems/number-of-ways-to-divide-a-long-corridor/
  4.  
  5. 1) Given a wooden stick of length n units. The stick is labelled from 0 to n. For example, a stick of length 6 is labelled as follows:
  6. Given an integer array cuts where cuts[i] denotes a position you should perform a cut at.
  7. You should perform the cuts in order, you can change the order of the cuts as you wish.
  8. The cost of one cut is the length of the stick to be cut, the total cost is the sum of costs of all cuts. When you cut a stick, it will be split into two smaller sticks (i.e. the sum of their lengths is the length of the stick before the cut). Please refer to the first example for a better explanation.
  9. Return the minimum total cost of the cuts.
  10.  
  11. Example 1:
  12. Input: n = 7, cuts = [1,3,4,5]
  13. Output: 16
  14. Explanation: Using cuts order = [1, 3, 4, 5] as in the input leads to the following scenario:
  15. The first cut is done to a rod of length 7 so the cost is 7. The second cut is done to a rod of length 6 (i.e. the second part of the first cut), the third is done to a rod of length 4 and the last cut is to a rod of length 3. The total cost is 7 + 6 + 4 + 3 = 20.
  16. Rearranging the cuts to be [3, 5, 1, 4] for example will lead to a scenario with total cost = 16 (as shown in the example photo 7 + 4 + 3 + 2 = 16).
  17.  
  18. Example 2:
  19. Input: n = 9, cuts = [5,6,1,4,2]
  20. Output: 22
  21. Explanation: If you try the given cuts ordering the cost will be 25.
  22. There are much ordering with total cost <= 25, for example, the order [4, 6, 5, 2, 1] has total cost = 22 which is the minimum possible.
  23.  
  24. Constraints:
  25. 2 <= n <= 10^6
  26. 1 <= cuts.length <= min(n - 1, 100)
  27. 1 <= cuts[i] <= n - 1
  28. All the integers in cuts array are distinct.
  29.  
  30. ------------------------------------------------------------------------------------------------------------------------------------
  31.  
  32. 2) Along a long library corridor, there is a line of seats and decorative plants. You are given a 0-indexed string corridor of length n consisting of letters 'S' and 'P' where each 'S' represents a seat and each 'P' represents a plant.
  33. One room divider has already been installed to the left of index 0, and another to the right of index n - 1. Additional room dividers can be installed. For each position between indices i - 1 and i (1 <= i <= n - 1), at most one divider can be installed.
  34. Divide the corridor into non-overlapping sections, where each section has exactly two seats with any number of plants. There may be multiple ways to perform the division. Two ways are different if there is a position with a room divider installed in the first way but not in the second way.
  35. Return the number of ways to divide the corridor. Since the answer may be very large, return it modulo 109 + 7. If there is no way, return 0.
  36.  
  37. Example 1:
  38. Input: corridor = "SSPPSPS"
  39. Output: 3
  40. Explanation: There are 3 different ways to divide the corridor.
  41. The black bars in the above image indicate the two room dividers already installed.
  42. Note that in each of the ways, each section has exactly two seats.
  43.  
  44. Example 2:
  45. Input: corridor = "PPSPSP"
  46. Output: 1
  47. Explanation: There is only 1 way to divide the corridor, by not installing any additional dividers.
  48. Installing any would create some section that does not have exactly two seats.
  49.  
  50. Example 3:
  51. Input: corridor = "S"
  52. Output: 0
  53. Explanation: There is no way to divide the corridor because there will always be a section that does not have exactly two seats.
  54.  
  55. Constraints:
  56. n == corridor.length
  57. 1 <= n <= 10^5
  58. corridor[i] is either 'S' or 'P'.
  59.  
  60. ---------------------------------------------------------------------------------------------------------------------------------------
  61.  
  62. 1)
  63. class Solution {
  64. public:
  65.     vector<vector<int>> dp;
  66.    
  67.     int helper(vector<int> &cuts, int start, int end){
  68.         if(start+1>=end){
  69.             return 0;
  70.         }
  71.         if(dp[start][end]!=-1){
  72.             return dp[start][end];
  73.         }
  74.         int res=INT_MAX;
  75.         for(int k=start+1;k<end;k++){
  76.             int cost=abs(cuts[end]-cuts[start])+helper(cuts,start,k)+helper(cuts,k,end); // when cut up at index k
  77.             res=min(res,cost);
  78.         }
  79.         return dp[start][end]=res;
  80.     }
  81.    
  82.     int minCost(int n, vector<int>& cuts) {
  83.         sort(cuts.begin(),cuts.end());  
  84.        
  85.         cuts.insert(cuts.begin(),0);
  86.         cuts.push_back(n);
  87.        
  88.         dp.resize(cuts.size()+1,vector<int>(cuts.size()+1,-1));
  89.        
  90.         return helper(cuts,0,cuts.size()-1);
  91.     }
  92. };
  93.  
  94. ------------------------------------------------------------------------------------------------------------------------------------
  95.  
  96. 2)
  97.  
  98. class Solution {
  99. public:
  100.     long long dp[100001][3];
  101.    
  102.     int helper(string &corridor, int curr, int seats){
  103.         int n=corridor.size();
  104.         if(curr==n){
  105.             return seats==2;
  106.         }
  107.         if(dp[curr][seats]!=-1){
  108.             return dp[curr][seats];
  109.         }
  110.         if(seats==2){
  111.             if(corridor[curr]=='P'){
  112.                 int TAKE_PLANT=helper(corridor,curr+1,seats)%1000000007;  // DO NOT PARTITION YET
  113.                 int DO_NOT_TAKE_PLANT=helper(corridor,curr+1,0)%1000000007; // PARTITION RIGHT HERE
  114.                 return dp[curr][seats]=(TAKE_PLANT+DO_NOT_TAKE_PLANT)%1000000007;
  115.             }
  116.             else{
  117.                 return dp[curr][seats]=helper(corridor,curr+1,1)%1000000007; // PARTITION NOW, seat will be 1, current seat part of next segment
  118.             }
  119.         }
  120.         else{
  121.             if(corridor[curr]=='S'){
  122.                 seats++;
  123.             }
  124.             return dp[curr][seats]=helper(corridor,curr+1,seats)%1000000007; // maintain seats
  125.         }
  126.     }
  127.     int numberOfWays(string corridor) {
  128.         memset(dp,-1,sizeof(dp));
  129.         return helper(corridor,0,0);
  130.     }
  131. };  //RECURSION+MEMO(ACCEPTED BUT SLOW)
  132.  
  133.  
  134. /*
  135. class Solution {
  136. public:
  137.     int numberOfWays(string corridor) {
  138.         vector<int> seats;
  139.         int n = corridor.size();
  140.         long long ans = 1, mod = 1e9+7;
  141.         for(int i = 0; i < n; i++) {
  142.             if(corridor[i]=='S'){
  143.                 seats.push_back(i);
  144.             }
  145.         }
  146.        
  147.         if(seats.size() % 2 == 1 or seats.size() == 0) {
  148.             return 0;
  149.         }
  150.        
  151.         for(int i = 2; i <= seats.size()-2; i += 2) {
  152. //leaving first two seats and last two seats as they are neccessary and no partition between them
  153.             ans = (ans % mod * (seats[i] - seats[i-1]) % mod) % mod;
  154.         }
  155.        
  156.         return ans;
  157.     }
  158. };  //GREEDY BUT O(N)SPACE
  159. */
  160.  
  161. /*
  162. class Solution {
  163. public:
  164.     int numberOfWays(string s) {
  165.         long ways=1;
  166.         int seats=0;
  167.         int plants=0;
  168.         for(auto e: s){
  169.             if(e=='S'){
  170.                 if(seats==2){
  171.                     ways= ways*(plants+1)%1000000007;
  172.                     seats=plants=0;
  173.                 }
  174.                 seats++;
  175.             }
  176.             else if(seats==2) plants++;    
  177.         }
  178.         return seats==2 ? ways : 0;
  179.     }
  180. };*/  //GREEDY BUT O(1)SPACE
  181.  
Advertisement
Add Comment
Please, Sign In to add comment