RainX_69

Minimum Cost to Make Array Equal | HARD OA LEVEL

Mar 30th, 2023
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.85 KB | Source Code | 0 0
  1. https://leetcode.com/problems/minimum-cost-to-make-array-equal/
  2.  
  3. You are given two 0-indexed arrays nums and cost consisting each of n positive integers.
  4. You can do the following operation any number of times:
  5. Increase or decrease any element of the array nums by 1.
  6. The cost of doing one operation on the ith element is cost[i].
  7. Return the minimum total cost such that all the elements of the array nums become equal.
  8.  
  9. Example 1:
  10. Input: nums = [1,3,5,2], cost = [2,3,1,14]
  11. Output: 8
  12. Explanation: We can make all the elements equal to 2 in the following way:
  13. - Increase the 0th element one time. The cost is 2.
  14. - Decrease the 1st element one time. The cost is 3.
  15. - Decrease the 2nd element three times. The cost is 1 + 1 + 1 = 3.
  16. The total cost is 2 + 3 + 3 = 8.
  17. It can be shown that we cannot make the array equal with a smaller cost.
  18.  
  19. Example 2:
  20. Input: nums = [2,2,2,2,2], cost = [4,2,8,1,3]
  21. Output: 0
  22. Explanation: All the elements are already equal, so no operations are needed.
  23.  
  24. Constraints:
  25.  
  26. n == nums.length == cost.length
  27. 1 <= n <= 10^5
  28. 1 <= nums[i], cost[i] <= 10^6
  29.  
  30. =======================================================================================================================
  31.  
  32. class Solution {
  33. public:
  34.     long long minCost(vector<int>& nums, vector<int>& cost) {
  35.         int n=nums.size();
  36.        
  37.         vector<pair<int,int>> arr;
  38.         for(int i=0;i<n;i++){
  39.             arr.push_back({nums[i],cost[i]});
  40.         }
  41.         sort(arr.begin(),arr.end());
  42.        
  43.         vector<long long> prefix(n,0);
  44.         vector<long long> suffix(n,0);
  45.  
  46.         long long sum=arr[0].second;
  47.         prefix[0]=0;
  48.         for(int i=1;i<n;i++){
  49.             prefix[i]=prefix[i-1];
  50.             long long diff=arr[i].first-arr[i-1].first;
  51.             prefix[i]+=diff*sum;
  52.             sum+=arr[i].second;
  53.         }
  54.        
  55.         sum=arr[n-1].second;
  56.         suffix[n-1]=0;
  57.         for(int i=n-2;i>=0;i--){
  58.             suffix[i]=suffix[i+1];
  59.             long long diff=arr[i+1].first-arr[i].first;
  60.             suffix[i]+=diff*sum;
  61.             sum+=arr[i].second;
  62.         }
  63.        
  64.         long long res=LONG_MAX;
  65.         for(int i=0;i<n;i++){
  66.             res=min(res,(long long)prefix[i]+suffix[i]);
  67.         }
  68.         return res;      
  69.     }
  70. };
  71.  
  72.  
  73. =======================================================================================================================
  74.  
  75. BRUTE FORCE
  76.  
  77. class Solution {
  78. public:
  79.     long long minCost(vector<int>& nums, vector<int>& cost) {
  80.         long long res=LONG_MAX;
  81.         for(int i=0;i<nums.size();i++){
  82.             long long temp=0;
  83.             for(int j=0;j<nums.size();j++){
  84.                 if(i!=j){
  85.                     temp+=(long long)abs(nums[i]-nums[j])*(long long)cost[j];
  86.                 }
  87.             }
  88.             res=min(temp,res);
  89.         }
  90.         return res;
  91.     }
  92. };
Advertisement
Add Comment
Please, Sign In to add comment