[Leetcode学习-c++&java]Reduce Array Size to The Half

问题:

难度:media

说明:

给出一个数组,里面包含重复元素,将任意种同值元素移除,剩下的数组元素长度 <= 原来数组的一半,求最小移除同值元素数量。

题目连接:https://leetcode.com/problems/reduce-array-size-to-the-half/

输入范围:

  • 1 <= arr.length <= 10^5
  • arr.length is even.
  • 1 <= arr[i] <= 10^5

输入案例:

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

我的代码:

其实这个主要是审题,比如 [7,7,7,7] 就代表 4 个 7,那么我移除 7 ,数组长度就变成了 0,我移除的最小类型数量是 1,那么我返回 1,。

这个只需要把所有元素用hash处理一次,求出各种数值的统计,然后排序一次,从最大数量元素开始移除,移除到一半或以上就可以了。

用 HashMap 或者 数组也行

Java:

class Solution {
    private static int[] cache = new int[100001];
    public int minSetSize(int[] arr) {
        for(int i : arr) cache[i] ++;
        Arrays.sort(cache);
        int total = 0, count = 0, half = (arr.length & 1) == 0 ? arr.length >> 1 : (arr.length >> 1) + 1;
        for(int i = 100000; total < half && i > 0; i --) {
            total += cache[i];
            count ++;
        }
        Arrays.fill(cache, 0);
        return count;
    }
    
    public int minSetSize2(int[] arr) {
        Map<Integer, Integer> cache = new HashMap<>();
        for(int i : arr) cache.put(i, cache.getOrDefault(i, 0) + 1);
        TreeMap<Integer, Integer> tree = new TreeMap<Integer, Integer>(new Comparator<Integer>() {
            public int compare(Integer o1, Integer o2) {
                return o2 - o1;
            }
        });
        for(int i : cache.values()) tree.put(i, tree.containsKey(i) ? tree.get(i) + 1 : 1);
        int total = 0, count = 0, half = (arr.length & 1) == 0 ? arr.length >> 1 : (arr.length >> 1) + 1;
        while(!tree.isEmpty() && total < half) {
            Map.Entry<Integer, Integer> entry = tree.pollFirstEntry();
            int times = entry.getValue();
            while(total < half && times -- > 0) {
                total += entry.getKey();
                count ++;
            }
        }
        return count;
    }
}

C++:

static int cache[100001] = {0};
class Solution {
public:
    int minSetSize(vector<int>& arr) {
        for(int i : arr) cache[i] ++;
        sort(cache, cache + 100001);
        int total = 0, count = 0, half = (arr.size() & 1) == 0 ? arr.size() >> 1 : (arr.size() >> 1) + 1;
        for(int i = 100000; total < half && i > 0; i --) {
            total += cache[i];
            count ++;
        }
        memset(cache, 0, sizeof(cache));
        return count;
    }
};

上一篇:CF 1550


下一篇:X的N次幂---从o(n)时间复杂度到o(logn)~二分法之递归