题目链接:
https://www.nowcoder.com/questionTerminal/365d5722fff640a0b6684391153e58d8?orderByHotValue=0&commentTags=C/C++
解题思路:
左手拿手套的最小数: 该数组和 - 该数组里除 0 外最小的数 + 1
右手是同样的道理
例如:
[0 , 7, 1, 6]拿手套的最小数: 14 - 1 + 1 = 14
import java.util.*;public class Gloves {public int findMinimum(int n, int[] left, int[] right) {int leftSum = 0;int rightSum = 0;int leftMin = Integer.MAX_VALUE;int rightMin = Integer.MAX_VALUE;int sum = 0;for(int i = 0; i < n; i++){if(left[i] * right[i] == 0){//表示左右手数组里有 0 这一项sum += left[i] + right[i];}else{leftSum += left[i];rightSum += right[i];if(leftMin > left[i]){leftMin = left[i];//更新leftMin}if(rightMin > right[i]){rightMin = right[i];}}}return Math.min(leftSum - leftMin + 1, rightSum - rightMin + 1) + 1 + sum;//Math.min(leftSum - leftMin + 1, rightSum - rightMin + 1)比较大小, 找出能拿最少手套的那个数组//括号外的 1 则表示从另外一个数组取出一只手套即可}
}