RainX_69

Minimum Cost to Split an Array in SPECIAL WAY (IMPORTANT) HARD

Feb 4th, 2023
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.22 KB | Source Code | 0 0
  1. https://leetcode.com/problems/minimum-cost-to-split-an-array/
  2.  
  3. You are given an integer array nums and an integer k.
  4.  
  5. Split the array into some number of non-empty subarrays. The cost of a split is the sum of the importance value of each subarray in the split.
  6.  
  7. Let trimmed(subarray) be the version of the subarray where all numbers which appear only once are removed.
  8.  
  9. For example, trimmed([3,1,2,4,3,4]) = [3,4,3,4].
  10. The importance value of a subarray is k + trimmed(subarray).length.
  11.  
  12. For example, if a subarray is [1,2,3,3,3,4,4], then trimmed([1,2,3,3,3,4,4]) = [3,3,3,4,4].The importance value of this subarray will be k + 5.
  13. Return the minimum possible cost of a split of nums.
  14.  
  15. A subarray is a contiguous non-empty sequence of elements within an array.
  16.  
  17.  
  18.  
  19. Example 1:
  20. Input: nums = [1,2,1,2,1,3,3], k = 2
  21. Output: 8
  22.  
  23. Explanation: We split nums to have two subarrays: [1,2], [1,2,1,3,3].
  24. The importance value of [1,2] is 2 + (0) = 2.
  25. The importance value of [1,2,1,3,3] is 2 + (2 + 2) = 6.
  26. The cost of the split is 2 + 6 = 8. It can be shown that this is the minimum possible cost among all the possible splits.
  27.  
  28. Example 2:
  29. Input: nums = [1,2,1,2,1], k = 2
  30. Output: 6
  31.  
  32. Explanation: We split nums to have two subarrays: [1,2], [1,2,1].
  33. The importance value of [1,2] is 2 + (0) = 2.
  34. The importance value of [1,2,1] is 2 + (2) = 4.
  35. The cost of the split is 2 + 4 = 6. It can be shown that this is the minimum possible cost among all the possible splits.
  36.  
  37. Example 3:
  38. Input: nums = [1,2,1,2,1], k = 5
  39. Output: 10
  40.  
  41. Explanation: We split nums to have one subarray: [1,2,1,2,1].
  42. The importance value of [1,2,1,2,1] is 5 + (3 + 2) = 10.
  43. The cost of the split is 10. It can be shown that this is the minimum possible cost among all the possible splits.
  44.  
  45.  
  46. Constraints:
  47.  
  48. 1 <= nums.length <= 1000
  49. 0 <= nums[i] < nums.length
  50. 1 <= k <= 10^9
  51.  
  52. ---------------------------------------------------------------------------------------------------------------------------------------
  53.  
  54. class Solution {
  55. public:
  56.     vector<vector<int>> trimDP;
  57.     vector<int> dp;
  58.    
  59.     int helper(vector<int> &nums, int curr, int k){
  60.         if(curr==nums.size()){
  61.             return 0;
  62.         }
  63.         if(dp[curr]!=-1){
  64.             return dp[curr];
  65.         }
  66.         int res=INT_MAX;
  67.         for(int i=curr;i<nums.size();i++){
  68.             int trimmedLen=trimDP[curr][i];
  69.             res=min(res,k+trimmedLen+helper(nums,i+1,k));
  70.         }
  71.         return dp[curr]=res;
  72.     }
  73.    
  74.     int minCost(vector<int>& nums, int k) {
  75.         int n=nums.size();
  76.        
  77.         trimDP.resize(n,vector<int>(n,0));
  78.         dp.resize(n,-1);
  79.        
  80.         for(int i=0;i<n;i++){
  81.             int dirt=0;
  82.             int freq[1000]={0};
  83.             int unique=0;
  84.             for(int j=i;j<n;j++){
  85.                 freq[nums[j]]++;
  86.                 if(freq[nums[j]]==1){
  87.                     unique++;
  88.                 }
  89.                 if(freq[nums[j]]==2){ // this has repeated again, we cannot remove this NON UNQIUE number anymore
  90.                     dirt++;
  91.                 }
  92.                 int totalLen=j-i+1;
  93.                 trimDP[i][j]=totalLen-unique+dirt;
  94.             }
  95.         }
  96.        
  97.         return helper(nums,0,k);
  98.     }
  99. };
  100.  
  101.  
  102.  
  103.  
Advertisement
Add Comment
Please, Sign In to add comment