..
1338. Reduce Array Size to The Half
Problem Description
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
Solution
Use a HashMap to store the occurence of each digits, then sort the tuple using the occurences as key in descending order. Iterate the sorted list and check if the removed digits’ total occurences are over half of total length
Python
from collections import Counter
class Solution:
def minSetSize(self, arr: List[int]) -> int:
if not arr or len(arr) == 0:
return 0
counter = Counter(arr)
freq = [(counter[key], key) for key in counter]
freq.sort(key=lambda x: (-x[0], x[1]))
n = len(arr)
curr, res= 0, 0
for f, val in freq:
curr += f
res += 1
if curr >= n // 2:
return res
return res
Java
class Solution {
public int minSetSize(int[] arr) {
if (arr == null || arr.length == 0)
return 0;
HashMap<Integer, Integer> counter = new HashMap<Integer, Integer>();
int n = arr.length;
for (int i = 0; i < n; i++) {
if (counter.containsKey(arr[i])) {
counter.put(arr[i], counter.get(arr[i]) + 1);
} else {
counter.put(arr[i], 1);
}
}
List<Point> freq = new ArrayList<>();
for (Map.Entry<Integer, Integer> entry : counter.entrySet())
freq.add(
new Point((int) entry.getValue(), (int) entry.getKey())
);
Collections.sort(freq, new CounterComparator());
int curr = 0, res = 0;
for (int i = 0; i < n; i++) {
curr += freq.get(i).occurrence;
res += 1;
if (curr * 2 >= n) {
return res;
}
}
return res;
}
}
class Point {
int occurrence;
int value;
public Point(int occurrence, int value) {
this.occurrence = occurrence;
this.value = value;
}
}
class CounterComparator implements Comparator<Point> {
public int compare(Point point1, Point point2) {
if (point1.occurrence < point2.occurrence) {
return 1;
} else if (point1.occurrence == point2.occurrence) {
return 0;
} else {
return -1;
}
}
}
Complexity Analysis
- Time Complexity
- O(n) HashMap
- O(nlogn) Sort
- O(k) where k is the result
- Space Complexity
- O(n)