RainX_69

MEET DEMANDS OF NEIGBOURING ELEMENTS (IMPORTANT OA)

Jan 23rd, 2023
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.85 KB | Source Code | 0 0
  1. https://practice.geeksforgeeks.org/problems/wine-buying-and-selling/1
  2.  
  3. Given an array, Arr[] of size N represents N house built along a straight line with equal distance between adjacent houses. Each house has a certain number of wine and they want to buy/sell those wines to other houses. Transporting one bottle of wine from one house to an adjacent house results in one unit of work. The task is to find the minimum number of work is required to fulfill all the demands of those N houses.
  4.  
  5. if arr[i] < 0, then ith house wants to sell arr[i] number of a wine bottle to other houses.
  6. if arr[i] > 0, then ith house wants to buy arr[i] number of a wine bottle from other houses.
  7. Note: One have to print the minimum number such that, all the house can buy/sell wine to each other.
  8. It is guaranteed that sum of all the elements of the array will be 0.
  9.  
  10. Example 1:
  11.  
  12. Input: N = 5,
  13. Arr[] = {5, -4, 1, -3, 1}
  14. Output: 9
  15. Explanation:
  16. 1th house can sell 4 wine bottles to 0th house,
  17. total work needed 4*(1-0) = 4, new arr[] = 1,0,1,-3,1
  18. now 3rd house can sell wine to 0th, 2th and 4th.
  19. so total work needed = 1*(3-0)+1*(3-2)+1*(4-3) = 5
  20. So total work will be 4+5 = 9
  21. Example 2:
  22.  
  23. Input: N = 6,
  24. Arr[]={-1000, -1000, -1000, 1000, 1000, 1000}
  25. Output: 9000
  26. Explanation:  
  27. 0th house sell 1000 wine bottles to 3rd house,
  28. total work needed 1000*(3-0) = 3000.
  29. 1st house sell 1000 wine bottles to 4th house,
  30. total work needed 3000 + 1000*(3-0) = 6000.
  31. 2nd house sell 1000 wine bottles to 5th house,
  32. total work needed 6000 + 1000*(3-0) = 9000.
  33. So total work will be 9000 unit.
  34.  
  35. Constraints:
  36. 1 ≤ N ≤ 10^5
  37. -10^6 ≤ Arr[i]10^6
  38.  
  39. ---------------------------------------------------------------------------------------------------------------------------------------
  40.  
  41. MY SOLUTION -
  42. class Solution{
  43.   public:
  44.   long long int wineSelling(vector<int>& arr, int n){
  45.       stack<pair<int,int>> s;
  46.       long long int res=0;
  47.       for(int i=0;i<n;i++){        
  48.           while(!s.empty() && ((arr[i]<0 && s.top().first>0) || (arr[i]>0 && s.top().first<0))){
  49.              //   (arr[i]<0 && s.top().first>0) --- current index wanna sell and prev wanna buy
  50.             //    (arr[i]>0 && s.top().first<0) --- current index wanna buy and prev wanna sell
  51.               int transfer=min(abs(s.top().first),abs(arr[i]));  // this is the trade amount
  52.               res+=(transfer*(i-s.top().second)); // work done calculated between trades
  53.              
  54.               arr[i]>0 ? (arr[i]-=transfer) : (arr[i]+=transfer); // do the neccessary arithmetic
  55.  
  56.               auto prevhouse=s.top();
  57.               s.pop();
  58.              
  59.               prevhouse.first>0 ? (prevhouse.first-=transfer) : (prevhouse.first+=transfer); // do the neccessary arithmetic
  60.              
  61.               if(prevhouse.first!=0){
  62.                   s.push(prevhouse);
  63.               }
  64.           }
  65.           if(arr[i]!=0){
  66.               s.push({arr[i],i});
  67.           }
  68.       }
  69.       return res;
  70.   }
  71. };
  72.  
  73. ---------------------------------------------------------------------------------------------------------------------------------------
  74.  
  75. WEBSITE SOLUTION -
  76.  
  77. class Solution{
  78.   public:
  79.   long long int wineSelling(vector<int>& Arr, int N){
  80.      
  81.         vector<pair<int,int> > buy;
  82.         vector<pair<int,int> > sell;
  83.         for(int i=0;i<N;i++){
  84.             if(Arr[i]>0) buy.push_back({Arr[i],i});
  85.             else sell.push_back({Arr[i],i});
  86.         }
  87.         long long int ans = 0;
  88.         int i=0,j=0;
  89.         while(i<buy.size() and j<sell.size()){
  90.             long long int x = min(buy[i].first,-sell[j].first);
  91.             buy[i].first -= x;
  92.             sell[j].first += x;
  93.             long long int diff = abs(buy[i].second - sell[j].second);
  94.             ans += (x * diff);
  95.             if(buy[i].first == 0) i++;
  96.             if(sell[j].first == 0) j++;
  97.         }
  98.         return ans;
  99.   }
  100. };
Advertisement
Add Comment
Please, Sign In to add comment