尝试是这样的(Java语法):
int max = M[0];
int sum = M[0];
for (i = 1 ; i < M.length ; i++) {
if (M[i] > max) {
max = M[i];
}
sum = sum + M[i];
}
if (2*max <= s+1) {
System.out.println("possible");
} else {
System.out.println("NOT possible");
}
时间复杂度:O(| M |)
的想法是,如果你想这些数字放到一个数组,您将从最长的序列开始,然后您选择第一个最长的序列以避免重复。
E.g.:
----------- arr: []
1, 1, 1, 1
2, 2
3
----------- arr: [1]
1, 1, 1
2, 2
3
----------- arr: [1, 2]
1, 1, 1
2
3
----------- arr: [1, 2, 1]
1, 1
2
3
----------- arr: [1, 2, 1, 2]
1, 1
3
----------- arr: [1, 2, 1, 2, 1]
1
3
----------- arr: [1, 2, 1, 2, 1, 3]
1
----------- arr: [1, 2, 1, 2, 1, 3, 1]
所以,如果从M中的最大数目是在其他大部分+ 1的总和,这是可能的,以获得阵列而不重复:
max <= sum_of_the_others + 1 | + max
这相当于
max + max <= sum_of_all_numbers + 1
相当于
2*max <= sum_of_all_numbers + 1