题解 | #手套#
手套
https://www.nowcoder.com/practice/365d5722fff640a0b6684391153e58d8
#include <climits> #include <random> class Gloves { public: int findMinimum(int n, vector<int> left, vector<int> right) { // write code here int left_sum = 0, left_min = INT_MAX; int right_sum = 0, right_min = INT_MAX; int sum = 0; //遍历每一种颜色的左右手套序列 for (int i = 0; i < n; i++) { //对于有0存在的颜色手套,累加 if (left[i]*right[i] == 0) sum += left[i] + right[i]; //对于左右手都有的颜色手套,执行累加-最小值+1 //找到最小值和总数 else { left_sum += left[i]; right_sum += right[i]; left_min = min(left_min, left[i]); right_min = min(right_min, right[i]); } } //结果为有左右都有数量的手套序列的结果+有0存在的手套数+最后再加一肯定就能保证了 return sum + min(left_sum - left_min + 1, right_sum - right_min + 1) + 1; } };