-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRearrangingFruits.cpp
More file actions
44 lines (39 loc) · 1.59 KB
/
Copy pathRearrangingFruits.cpp
File metadata and controls
44 lines (39 loc) · 1.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
//Optimized solution
//Time complexity: O(nlogn) using sorting
//Space Complexity:O(n) using map
class Solution {
public:
long long minCost(vector<int>& basket1, vector<int>& basket2) {
unordered_map<int,int>mp; //make a map for basket1 and subtract basket2 from it to get the extra elements
int minel=INT_MAX; //keep a track of min element
for(int &x:basket1){ //adding basket1
mp[x]++;
minel=min(minel,x);
}
for(int &x:basket2){ //subtracting basket2 to get extra
mp[x]--;
minel=min(minel,x);
}
vector<int>finallist; //-map ->extra->vector
for(auto &it:mp){
int cost=it.first; //element
int count=it.second; //no. of elements
if(count==0){
continue;
}
if(count%2!=0){ //if not divisible by 2 return -1
return -1;
}
for(int c=1;c<=abs(count)/2;c++){ //we use abs as after subtract it can be negative
finallist.push_back(cost);
}
}
sort(finallist.begin(),finallist.end()); //sorting so that we will have min cost using greedy
// nth_element(finallist.begin(),finallist.begin()+finallist.size()/2,finallist.end()); // we can also use this as we are only taking elements till half size so sort only half array
long long res=0;
for(int i=0;i<finallist.size()/2;i++){
res+=min(finallist[i],minel*2); //edge case we will swap indirectly through the min element to get min cost
}
return res;
}
};