RainX_69

2518. Number of Great Partitions

Dec 26th, 2022 (edited)
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.46 KB | Software | 0 0
  1. /*
  2. You are given an array nums consisting of positive integers and an integer k.
  3.  
  4. Partition the array into two ordered groups such that each element is in exactly one group. A partition is called great if the sum of elements of each group is greater than or equal to k.
  5.  
  6. Return the number of distinct great partitions. Since the answer may be too large, return it modulo 109 + 7.
  7.  
  8. Two partitions are considered distinct if some element nums[i] is in different groups in the two partitions.
  9.  
  10.  
  11.  
  12. Example 1:
  13.  
  14. Input: nums = [1,2,3,4], k = 4
  15. Output: 6
  16. Explanation: The great partitions are: ([1,2,3], [4]), ([1,3], [2,4]), ([1,4], [2,3]), ([2,3], [1,4]), ([2,4], [1,3]) and ([4], [1,2,3]).
  17. Example 2:
  18.  
  19. Input: nums = [3,3,3], k = 4
  20. Output: 0
  21. Explanation: There are no great partitions for this array.
  22. Example 3:
  23.  
  24. Input: nums = [6,6], k = 2
  25. Output: 2
  26. Explanation: We can either put nums[0] in the first partition or in the second partition.
  27. The great partitions will be ([6], [6]) and ([6], [6]).
  28.  
  29.  
  30. Constraints:
  31.  
  32. 1 <= nums.length, k <= 1000
  33. 1 <= nums[i] <= 10^9
  34.  
  35. link- https://leetcode.com/problems/number-of-great-partitions/
  36.  
  37. */
  38.  
  39. class Solution {
  40. public:
  41.     long long int dp[1001][1001];
  42.    
  43.     long long int binaryExponentiation(long long int base, int power){
  44.         long long int res=1;
  45.         while(power>0){
  46.             if(power%2==0){
  47.                 base=(base*base)%1000000007;
  48.                 power/=2;
  49.             }
  50.             else{
  51.                 res=(res*base)%1000000007;
  52.                 power--;
  53.             }
  54.         }
  55.         return res;
  56.     }
  57.    
  58.     long long int helper(vector<int> &nums, int curr, int k){
  59.         if(curr==nums.size()){
  60.             return 1;
  61.         }
  62.         if(dp[curr][k]!=-1){
  63.             return dp[curr][k];
  64.         }
  65.         long long int res=0;
  66.         if(k-nums[curr]>=0){
  67.             res=(res+helper(nums,curr+1,k-nums[curr]))%1000000007;
  68.         }
  69.         res=(res+helper(nums,curr+1,k))%1000000007;
  70.         return dp[curr][k]=res;
  71.     }
  72.    
  73.     int countPartitions(vector<int>& nums, int k) {
  74.         // there are 2^n possible partitions, including empty subsequences
  75.         memset(dp,-1,sizeof(dp));
  76.         long long int sum=accumulate(nums.begin(),nums.end(),0LL);
  77.         if(2*k>sum){
  78.             return 0;
  79.         }
  80.         int n=nums.size();
  81.         long long int total=(binaryExponentiation(2,n))%1000000007;
  82.         long long int invalids=(helper(nums,0,k-1)*2)%1000000007; // empty subseq included
  83.         return (total-invalids+1000000007)%1000000007;
  84.     }
  85. };
  86.  
  87.  
  88.  
  89. /*
  90. Explanation-
  91.  
  92. 1) Find total partitions
  93. 2) Remove out the bad partitions, i.e atleast one group/subset less than k
  94. 3) Subtract total-bad and return
  95.  
  96. TC- O(nk)
  97.  
  98. Find all subsets of [1,2,3,4]
  99. That is 16 of them, according to formula 2^n.
  100.  
  101. [1] [2, 3, 4]
  102. [2] [1, 3, 4]
  103. [3] [1, 2, 4]
  104. [4] [1, 2, 3]
  105. [1, 2] [3, 4]
  106. [1, 3] [2, 4]
  107. [1, 4] [2, 3]
  108. [2, 3] [1, 4]
  109. [2, 4] [1, 3]
  110. [3, 4] [1, 2]
  111. [1, 2, 3] [4]
  112. [1, 2, 4] [3]
  113. [1, 3, 4] [2]
  114. [2, 3, 4] [1]
  115. [1,2.3,4] []
  116. [] [1,2,3,4]
  117.  
  118. Now, you remove all the subsets having less than k=3 sum. that is
  119.  
  120. [1, 2, 3, 4] []
  121. [1] [2, 3, 4]
  122. [2] [1, 3, 4]
  123. [3] [1, 2, 4]
  124. [1, 2] [3, 4]
  125.  
  126. [] [1, 2, 3, 4]
  127. [2, 3, 4] [1]
  128. [1, 3, 4] [2]
  129. [1, 2, 4] [3]
  130. [3, 4] [1, 2]
  131.  
  132.  
  133. Now you can clearly see there are 8, but your function will return 4 but multiply 2 to it since you can clearly see the other 2 are mirror image of one another.
  134.  
  135. Now great partition=total-bad partition.
  136. */
Advertisement
Add Comment
Please, Sign In to add comment