There are r red balls, g green balls and b blue balls. Also there are infinite number of packets given to you. Each packet must be filled with only 3 balls and should contain balls of at least 2 different colors. Find the maximum number of packets that can be filled?
Here is my approach using Dynamic Programming which is straight forward
map<multiset<int>,int> store;
int packets(int r,int g,int b){
if (r<0||g<0||b<0){
return 0;
}
if (r==g&&g==b){
return r;
}
if ((r+g==0)||(g+b==0)||(b+r==0)){
return 0;
}
if (r==0&&b==0&&g==0){
return 0;
}
multiset<int> key;
key.insert(r);key.insert(g);key.insert(b);
if (store.find(key)!=store.end()){
return store[key];
}
int max_packets = packets(r-2,g-1,b)+1;
max_packets = max(max_packets,packets(r-2,g-1,b)+1);
max_packets = max(max_packets,packets(r-1,g-2,b)+1);
max_packets = max(max_packets,packets(r-2,g,b-1)+1);
max_packets = max(max_packets,packets(r-1,g,b-2)+1);
max_packets = max(max_packets,packets(r,g-2,b-1)+1);
max_packets = max(max_packets,packets(r,g-1,b-2)+1);
max_packets = max(max_packets,packets(r-1,g-1,b-1)+1);
store[key] = max_packets;
return max_packets;
}
My solution may be logically correct but is surely inefficient for large values of r,g and b. I also identified some patterns for r,g,b vs maximum packets but cannot prove them logically.
Can someone help me with their idea?.
Thank you