aboutsummaryrefslogtreecommitdiff
path: root/reduce_array_size_half.py
diff options
context:
space:
mode:
authorjack <0x6A73@pm.me>2024-06-14 13:13:15 -0600
committerjack <0x6A73@pm.me>2024-06-14 13:13:15 -0600
commitd343a3b18dbaca9297d4f7b2280c4a16f0a5c8aa (patch)
tree96df922dae2a5073d9a50ea70c7aae6aa37e3ebe /reduce_array_size_half.py
downloadleetcode-d343a3b18dbaca9297d4f7b2280c4a16f0a5c8aa.tar.gz
leetcode-d343a3b18dbaca9297d4f7b2280c4a16f0a5c8aa.zip
initial
Diffstat (limited to 'reduce_array_size_half.py')
-rw-r--r--reduce_array_size_half.py31
1 files changed, 31 insertions, 0 deletions
diff --git a/reduce_array_size_half.py b/reduce_array_size_half.py
new file mode 100644
index 0000000..bcbac65
--- /dev/null
+++ b/reduce_array_size_half.py
@@ -0,0 +1,31 @@
+# 1338. Reduce Array Size to The Half
+from collections import Counter
+
+"""
+you are given an integer array 'arr'. you can choose a set of integers and
+remove all the occurences of these integers in the array. return the minimum
+size of the set so that at least half of the integers in the array are
+removed.
+"""
+
+
+class Solution(object):
+ def minSetSize(self, arr):
+ """
+ :type arr: List[int]
+ :rtype: int
+ """
+ cnt = Counter(arr)
+ freq = list(cnt.values())
+ freq.sort()
+ ans, removed, half = 0, 0, len(arr) // 2
+ while removed < half:
+ ans += 1
+ removed += freq.pop()
+ return ans
+
+
+if __name__ == "__main__":
+ obj = Solution()
+ print(obj.minSetSize(arr=[3, 3, 3, 3, 5, 5, 5, 2, 2, 7]))
+ print(obj.minSetSize(arr=[7, 7, 7, 7, 7, 7]))