RainX_69

Maximise the minimum height of the flower | Binary Search | Hard | MUST DO

Mar 5th, 2023
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.81 KB | Source Code | 0 0
  1. https://practice.geeksforgeeks.org/problems/899540d741547e2d75d1c5c03a4161ab53affd13/1?page=1&difficulty[]=1&difficulty[]=2&status[]=unsolved&category[]=Dynamic%20Programming&category[]=Binary%20Search&category[]=Trie&category[]=union-find&sortBy=latest
  2.  
  3. It is also on codeforces
  4.  
  5. You have a garden with n flowers lined up in a row. The height of ith flower is ai units. You will water them for k days. In one day you can water w continuous flowers (you can do this only once in a single day). Whenever you water a flower its height increases by 1 unit. You need to maximize the height of the smallest flower.
  6.  
  7. Example 1:
  8.  
  9. Input:
  10. N=6
  11. K=2
  12. W=3
  13. a[]={2,2,2,2,1,1}
  14. Output:
  15. 2
  16. Explanation:
  17. Water last three flowers for two days.The new heights
  18. will be {2,2,2,3,2,2}
  19.  
  20. Example 2:
  21. Input:
  22. N=2
  23. K=5
  24. W=1
  25. a[]={5,8}
  26. Output:
  27. 9
  28. Explanation:
  29. For the first four days water the first flower then
  30. water the last flower once.
  31.  
  32. Constraints:
  33.  
  34. 1 <= N <= 10^5
  35. 1<=w<=N
  36. 1<=K<=10^5
  37. 1 <= a[i] <= 10^9
  38.  
  39. ---------------------------------------------------------------------------------------------------------------------------------------
  40. class Solution{
  41.     public:
  42.         bool isOK(vector<int> &arr, int days, int cs, long long int h){
  43.             int n=arr.size();
  44.            
  45.             vector<long long> waterSupply(n,0);
  46.            
  47.             if(arr[0]<h){
  48.                 waterSupply[0]=h-arr[0];
  49.                 days-=(h-arr[0]);
  50.             }
  51.            
  52.             if(days<0){
  53.                 return false;
  54.             }
  55.            
  56.             for(int i=1;i<arr.size();i++){
  57.                 waterSupply[i]=waterSupply[i-1];
  58.                
  59.                 int actualHeight=arr[i];
  60.                 if(i>=cs){
  61.                     actualHeight+=(waterSupply[i]-waterSupply[i-cs]);
  62.                 }
  63.                 else{
  64.                     actualHeight+=waterSupply[i];
  65.                 }
  66.                
  67.                 if(actualHeight<h){
  68.                     waterSupply[i]+=(h-actualHeight);
  69.                     days-=(h-actualHeight);
  70.                 }
  71.                 if(days<0){
  72.                     return false;
  73.                 }
  74.             }
  75.             return true;
  76.         }
  77.        
  78.         long long int maximizeMinHeight(vector<int> &a,int n,int k,int w){
  79.             long long int res=-1;
  80.             long long int mnHeight=*min_element(a.begin(),a.end());
  81.             long long int mxHeight=INT_MAX/2;
  82.             while(mnHeight<=mxHeight){
  83.                 int guessHeight=(mxHeight+mnHeight)/2;
  84.                 if(isOK(a,k,w,guessHeight)==true){
  85.                     res=guessHeight;
  86.                     mnHeight=guessHeight+1;
  87.                 }
  88.                 else{
  89.                     mxHeight=guessHeight-1;
  90.                 }
  91.             }
  92.             return res;
  93.         }
  94. };
  95.  
  96.  
Advertisement
Add Comment
Please, Sign In to add comment