当前位置:网站首页>Detailed explanation of Niuke - Gloves

Detailed explanation of Niuke - Gloves

2022-04-23 16:42:00 Grow up, Leslie

Original link : glove __ Cattle from

Their thinking :

1. First of all, consider that there are no gloves on both sides 0 The situation of

Take the one on the left as an example : Subtract the minimum number of colors from the number of all gloves on the left, and then +1, The above figure is the corresponding figure 17-2+1=16 only , in other words , Take... On the left 16 Gloves , sure Take out all the colors , Now just take one from the other hand and you can match it . Suppose to take 15 Gloves , Then there may be such a situation : Take it 4 Only yellow ,5 Only green ,6 Only blue ; Then you may not get the orange one , So take 15 A glove cannot 100% Take out all possible colors . Do the same for the one on the right , Judge which one is smaller on the left and right , Then the last value +1 It'll match .

2. There may be... On both sides 0 The situation of

The final code implementation :

 

class Gloves {
public:
    int findMinimum(int n, vector<int> left, vector<int> right) {
        // write code here
        int sum=0;
        int left_min=INT_MAX;
        int right_min=INT_MAX;
        int left_sum=0,right_sum=0;
        for(int i=0;i<n;i++)
        {
            if(left[i]*right[i]==0)
            sum+=left[i]+right[i];// If there is one for 0, We need to find out this situation 
            else
            {
                left_sum+=left[i];
                left_min=left[i]<left_min?left[i]:left_min;
                 right_sum+=right[i];
                right_min=right[i]<right_min?right[i]:right_min;
            }
        }
        return sum+=(left_sum>right_sum?(right_sum-right_min+1):(left_sum-left_min+1))+1;
    }
};

版权声明
本文为[Grow up, Leslie]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231637283768.html