Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- If we choose to make x bags empty,we should choose the x bags with the least amount
- of beans.so better to sort the array.so we need to find the m which will be out beans
- for non-empty bags.so possibility of m can be numbers in array because we can only
- reduce the beans,can't add any beans.so after sorting,we can make empty ith bag and make
- all bag after i to i+1th bean,in this way all arr[i] will be m once.
- */
- long long _min(long long a,long long b){
- if(a<b) return a;
- return b;
- }
- class Solution {
- public:
- long long minimumRemoval(vector<int>& beans) {
- sort(beans.begin(),beans.end());
- long long sum=0,prev_sum=0;
- for(auto itr:beans) sum+=itr;
- long long ans=sum; //This is must because you can't put INT_MAX here because if k=0,then answer will be sum not INT_MAX.
- for(long long int i=0;i<beans.size();i++){
- long long temp=prev_sum+sum-prev_sum-(beans.size()-i)*beans[i];
- ans=_min(ans, temp );
- prev_sum+=beans[i];
- }
- return ans;
- }
- };
- #Python
- class Solution:
- def minimumRemoval(self, beans: List[int]) -> int:
- arr=sorted(beans)
- total=sum(beans)
- ans=total
- n=len(beans)
- for i in range(len(arr)):
- ans=min(ans,total-((n-i)*arr[i]))
- return ans
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement