diff options
author | jack <[email protected]> | 2024-06-14 13:13:15 -0600 |
---|---|---|
committer | jack <[email protected]> | 2024-06-14 13:13:15 -0600 |
commit | d343a3b18dbaca9297d4f7b2280c4a16f0a5c8aa (patch) | |
tree | 96df922dae2a5073d9a50ea70c7aae6aa37e3ebe /reduce_array_size_half.cpp | |
download | leetcode-d343a3b18dbaca9297d4f7b2280c4a16f0a5c8aa.tar.gz leetcode-d343a3b18dbaca9297d4f7b2280c4a16f0a5c8aa.zip |
initial
Diffstat (limited to 'reduce_array_size_half.cpp')
-rw-r--r-- | reduce_array_size_half.cpp | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/reduce_array_size_half.cpp b/reduce_array_size_half.cpp new file mode 100644 index 0000000..fd922dc --- /dev/null +++ b/reduce_array_size_half.cpp @@ -0,0 +1,30 @@ +#include "leetcode.h" + +class Solution { +public: + int minSetSize(vector<int> &arr) { + int ans = 0, n = arr.size(), n1 = 0; + vector<int> j; + unordered_map<int, int> um; + for (int i = 0; i < n; i++) + um[arr[i]]++; + for (auto x : um) + j.push_back(x.second); + if (j.size() == 1) + return 1; + sort(j.begin(), j.end()); + for (int i = j.size() - 1; i >= 0; i--) { + if (ans >= n / 2) + return n1; + n1++; + ans = ans + j[i]; + } + return 0; + } +}; + +int main() { + Solution obj; + vector<int> arr = {3, 3, 3, 3, 5, 5, 5, 2, 2, 7}; + cout << obj.minSetSize(arr); +} |