1342. Reduce Array Size to The Half

Given an array arr.  You can choose a set of integers and remove all the occurrences of these integers in the array.

Return the minimum size of the set so that at least half of the integers of the array are removed.

Example 1:

Input: arr = [3,3,3,3,5,5,5,2,2,7]
Output: 2
Explanation: Choosing {3,7} will make the new array [5,5,5,2,2] which has size 5 (i.e equal to half of the size of the old array).
Possible sets of size 2 are {3,5},{3,2},{5,2}.
Choosing set {2,7} is not possible as it will make the new array [3,3,3,3,5,5,5] which has size greater than half of the size of the old array.

Example 2:

Input: arr = [7,7,7,7,7,7]
Output: 1
Explanation: The only possible set you can choose is {7}. This will make the new array empty.

Example 3:

Input: arr = [1,9]
Output: 1

Example 4:

Input: arr = [1000,1000,3,7]
Output: 1

Example 5:

Input: arr = [1,2,3,4,5,6,7,8,9,10]
Output: 5

Constraints:

  • 1 <= arr.length <= 10^5
  • arr.length is even.
  • 1 <= arr[i] <= 10^5
class Solution {
    public int minSetSize(int[] arr) {
        Map<Integer, Integer> map = new HashMap();
        for(int i: arr){
            map.put(i, map.getOrDefault(i, 0) + 1);
        }
        int[] help = new int[map.values().size()];
        int siz = 0;
        List<Integer> list = new ArrayList(map.values());
        for(int i = 0; i < help.length; i++){
            help[i] = list.get(i);
            siz += help[i];
        }
        Arrays.sort(help);
        int res = 0, ind = help.length - 1;
        int le = siz / 2;
        while(siz > le && ind >= 0){
            res++;
            siz -= help[ind];
            ind--;
        }
        return res;
    }
}

记录出现的次数,然后减,注意最后是>不是>=
然后还要注意,Arrays.sort()不能用lambda表达式来实现int的排序,只能对Integer这种object使用

原文地址:https://www.cnblogs.com/wentiliangkaihua/p/12254597.html

时间: 2024-08-13 12:26:07

1342. Reduce Array Size to The Half的相关文章

【leetcode】1338. Reduce Array Size to The Half

题目如下: Given an array arr.  You can choose a set of integers and remove all the occurrences of these integers in the array. Return the minimum size of the set so that at least half of the integers of the array are removed. Example 1: Input: arr = [3,3

lodash 源码解读 _chunk(array, size)

_.chunk(array, index): 拆分一个数组成两个数组,拆分位数由 index 决定, 举例 1 _.chunk(['a', 'b', 'c', 'd'], 1);// => [['a', 'b','c'], ['d']] 1 function chunk(array, size) { 2 size = Math.max(size, 0) //这里是判定传过来的 size 是否会小于0,属于边界测试 3 const length = array == null ? 0 : arra

How to reduce the size of logging database OR How to purge the old data from Logging Database

WSS logging database grows very fast and it cause the storage problem most of the time in sharepoint server 2010. To reduce the size of the logging database or to purge the old data from the logging database we can these steps. To find the Logging Da

unity, reduce android size

参考: https://www.youtube.com/watch?v=TYSmf_zgtZo http://stackoverflow.com/questions/41087220/how-to-use-stripping-level-il2cpp-option-in-android-player-settings-unity http://gamedev.stackexchange.com/questions/116027/armv7-vs-fat-in-unity-android-buil

27. Remove Element【easy】

27. Remove Element[easy] Given an array and a value, remove all instances of that value in place and return the new length. Do not allocate extra space for another array, you must do this in place with constant memory. The order of elements can be ch

[LeetCode] Search in a Sorted Array of Unknown Size 在未知大小的有序数组中搜索

Given an integer array sorted in ascending order, write a function to search target in nums.  If target exists, then return its index, otherwise return -1. However, the array size is unknown to you. You may only access the array using an ArrayReader 

高程(三)----数组Array

一.数组的创建 var arrayObj = new Array(); //创建一个数组 var arrayObj = new Array([size]); //创建一个数组并指定长度,注意不是上限,是长度. var arrayObj = new Array([element0[, element1[, ...[, elementN]]]]); 创建一个数组并赋值 要说明的是,虽然第二种方法创建数组指定了长度,但实际上所有情况下数组都是变长的,也就是说即使指定了长度为5,仍然可以将元素存储在规定

opendressinghash //use resize array

1 public class opendressinghash<Key, Value> { 2 private static final int INIT_CAPACITY = 4; 3 4 private int n; 5 private int m; 6 private Key[] keys; 7 private Value[] vals; 8 9 public opendressinghash() { 10 this(INIT_CAPACITY); 11 } 12 13 public o

js map, reduce, forEach, filter的一般实现

map(映射), reduce(规约), forEach(遍历), filter(过滤),它们都是高阶函数,都是以传入不同的函数来以不同的方式操作数组元. 1> map function map(array, func) { var res = []; for (var i = 0, len = array.length; i < len; i++) { res.push(func(array[i])); } return res; } var res = map([1, 2, 3], fun