目录
牛客OR57 手套
解析代码
牛客OR57 手套
手套_牛客题霸_牛客网
class Gloves {
public:
int findMinimum(int n, vector<int> left, vector<int> right) {
}
};
解析代码
class Gloves {
public:
int findMinimum(int n, vector<int> left, vector<int> right) {
int ret = 0;
int left_cnt = 0, right_cnt = 0;
int left_min = INT_MAX, right_min = INT_MAX;
for (int i = 0; i < n; ++i)
{
if (left[i] == 0)
{
ret += right[i];
}
else if (right[i] == 0)
{
ret += left[i];
}
else
{
left_cnt += left[i];
right_cnt += right[i];
left_min = min(left[i], left_min);
right_min = min(right[i], right_min);
}
}
ret += min(left_cnt - left_min, right_cnt - right_min);
return ret + 2; // 全部情况拿完,再左右各拿一个
}
};