华为OD机试-阿里巴巴找黄金宝箱2
import java.util.Comparator; import java.util.HashSet; import java.util.stream.IntStream; public class Main { public static void main(String[] args) { int[] aa = new int[]{1, 1, 1, 1, 3, 3, 3, 6, 6, 8}; int sum = (IntStream.of(aa).sum() + 1) / 2; int[] array = IntStream.of(aa).boxed().sorted(Comparator.reverseOrder()).mapToInt(Integer::intValue).toArray(); int numSum = 0; HashSet<Integer> integers = new HashSet<>(); for (int j : array) { numSum += j; integers.add(j); if (numSum >= sum) { break; } } System.out.println(integers.size()); } }