id
int64
1
3.58k
problem_description
stringlengths
516
21.8k
instruction
int64
0
3
solution_c
dict
1,457
<p>You want to schedule a list of jobs in <code>d</code> days. Jobs are dependent (i.e To work on the <code>i<sup>th</sup></code> job, you have to finish all the jobs <code>j</code> where <code>0 &lt;= j &lt; i</code>).</p> <p>You have to finish <strong>at least</strong> one task every day. The difficulty of a job schedule is the sum of difficulties of each day of the <code>d</code> days. The difficulty of a day is the maximum difficulty of a job done on that day.</p> <p>You are given an integer array <code>jobDifficulty</code> and an integer <code>d</code>. The difficulty of the <code>i<sup>th</sup></code> job is <code>jobDifficulty[i]</code>.</p> <p>Return <em>the minimum difficulty of a job schedule</em>. If you cannot find a schedule for the jobs return <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/01/16/untitled.png" style="width: 365px; height: 370px;" /> <pre> <strong>Input:</strong> jobDifficulty = [6,5,4,3,2,1], d = 2 <strong>Output:</strong> 7 <strong>Explanation:</strong> First day you can finish the first 5 jobs, total difficulty = 6. Second day you can finish the last job, total difficulty = 1. The difficulty of the schedule = 6 + 1 = 7 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> jobDifficulty = [9,9,9], d = 4 <strong>Output:</strong> -1 <strong>Explanation:</strong> If you finish a job per day you will still have a free day. you cannot find a schedule for the given jobs. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> jobDifficulty = [1,1,1], d = 3 <strong>Output:</strong> 3 <strong>Explanation:</strong> The schedule is one job per day. total difficulty will be 3. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= jobDifficulty.length &lt;= 300</code></li> <li><code>0 &lt;= jobDifficulty[i] &lt;= 1000</code></li> <li><code>1 &lt;= d &lt;= 10</code></li> </ul>
3
{ "code": "/*\n n is number of jobs\n\n max jobDifficulty table: mjt[i][j] means max jobDifficulty in range[i , j]\n\n dp[i][j]: means the minimum difficulty of a job schedule, if we finished 0 to j-th jobs on 0 to i-th day. (j >= i)\n \n dp[i][j] = dp[i-1][j-k] + max one in jobDifficulty [j-k+1, j] (1 <= k <= j-i+1)\n = dp[i-1][j-k] + mjt[j-k+1][j];\n \n pick a k, and try to minimize dp[i][j], takes o(j) time for dp[i][j], worst case O(n)\n\n total time complexity: O(n^3) -> O(n^2) or O(longn * n^2) ?\n\n final result: dp[d][n]\n*/\nclass Solution {\npublic:\n int minDifficulty(vector<int>& jobDifficulty, int d) {\n int n = jobDifficulty.size();\n\n //Max jobDifficulty table: mjt[i][j] means max jobDifficulty in range[i , j]\n vector<vector<int>> mjt(n+1, vector<int>(n+1, 0));\n for(int i = 1; i <= n; i++){\n for(int j = i; j <= n; j++){\n mjt[i][j] = max(mjt[i][j-1], jobDifficulty[j-1]);\n }\n }\n\n //dp[i][j]: means the minimum difficulty of a job schedule, if we finished 0 to j-th jobs on 0 to i-th day. (j >= i)\n vector<vector<int>> dp(d+1, vector<int>(n+1, 0));\n for(int j = 0; j<=n; j++){\n dp[0][j] = mjt[1][j];\n }\n dp[d][n] = -1;\n\n for(int i = 1; i <= d; i++){\n for(int j = i; j <= n; j++){//j = 2\n int minJobDifficultyToday = INT_MAX;\n for(int k = 1; k <= j-i+1; k++){//k = 1, 2\n minJobDifficultyToday = min(minJobDifficultyToday, dp[i-1][j-k] + mjt[j-k+1][j]);//dp[0][1]+mjt[2][2], dp[0][0]+mjt[1][2]\n }\n dp[i][j] = minJobDifficultyToday;\n }\n }\n\n return dp[d][n];\n\n }\n};", "memory": "15700" }
1,457
<p>You want to schedule a list of jobs in <code>d</code> days. Jobs are dependent (i.e To work on the <code>i<sup>th</sup></code> job, you have to finish all the jobs <code>j</code> where <code>0 &lt;= j &lt; i</code>).</p> <p>You have to finish <strong>at least</strong> one task every day. The difficulty of a job schedule is the sum of difficulties of each day of the <code>d</code> days. The difficulty of a day is the maximum difficulty of a job done on that day.</p> <p>You are given an integer array <code>jobDifficulty</code> and an integer <code>d</code>. The difficulty of the <code>i<sup>th</sup></code> job is <code>jobDifficulty[i]</code>.</p> <p>Return <em>the minimum difficulty of a job schedule</em>. If you cannot find a schedule for the jobs return <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/01/16/untitled.png" style="width: 365px; height: 370px;" /> <pre> <strong>Input:</strong> jobDifficulty = [6,5,4,3,2,1], d = 2 <strong>Output:</strong> 7 <strong>Explanation:</strong> First day you can finish the first 5 jobs, total difficulty = 6. Second day you can finish the last job, total difficulty = 1. The difficulty of the schedule = 6 + 1 = 7 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> jobDifficulty = [9,9,9], d = 4 <strong>Output:</strong> -1 <strong>Explanation:</strong> If you finish a job per day you will still have a free day. you cannot find a schedule for the given jobs. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> jobDifficulty = [1,1,1], d = 3 <strong>Output:</strong> 3 <strong>Explanation:</strong> The schedule is one job per day. total difficulty will be 3. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= jobDifficulty.length &lt;= 300</code></li> <li><code>0 &lt;= jobDifficulty[i] &lt;= 1000</code></li> <li><code>1 &lt;= d &lt;= 10</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int minDifficulty(vector<int>& jobDifficulty, int d) {\n int n=jobDifficulty.size();\n if(d>n) return -1;\n vector<vector<int>> dp (n, vector<int> (d+1,0));\n vector<vector<int>> maxV (n, vector<int> (n,0));\n for(int i=0;i<n;i++){\n int curr_max = jobDifficulty[i];\n maxV[i][i] = jobDifficulty[i];\n for(int j=i+1;j<n;j++){\n curr_max = max(curr_max, jobDifficulty[j]);\n maxV[i][j] = curr_max;\n }\n }\n for(int i=0;i<n;i++){\n dp[i][1] = maxV[0][i];\n }\n for(int j=2;j<d+1;j++){\n for(int i=j-1;i<n;i++){\n int currMin=dp[j-2][j-1]+maxV[j-1][i];\n for(int k=j-1;k<i;k++){\n currMin = min(currMin, dp[k][j-1]+maxV[k+1][i]);\n }\n dp[i][j] = currMin;\n }\n }\n return dp[n-1][d];\n }\n};", "memory": "15800" }
1,457
<p>You want to schedule a list of jobs in <code>d</code> days. Jobs are dependent (i.e To work on the <code>i<sup>th</sup></code> job, you have to finish all the jobs <code>j</code> where <code>0 &lt;= j &lt; i</code>).</p> <p>You have to finish <strong>at least</strong> one task every day. The difficulty of a job schedule is the sum of difficulties of each day of the <code>d</code> days. The difficulty of a day is the maximum difficulty of a job done on that day.</p> <p>You are given an integer array <code>jobDifficulty</code> and an integer <code>d</code>. The difficulty of the <code>i<sup>th</sup></code> job is <code>jobDifficulty[i]</code>.</p> <p>Return <em>the minimum difficulty of a job schedule</em>. If you cannot find a schedule for the jobs return <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/01/16/untitled.png" style="width: 365px; height: 370px;" /> <pre> <strong>Input:</strong> jobDifficulty = [6,5,4,3,2,1], d = 2 <strong>Output:</strong> 7 <strong>Explanation:</strong> First day you can finish the first 5 jobs, total difficulty = 6. Second day you can finish the last job, total difficulty = 1. The difficulty of the schedule = 6 + 1 = 7 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> jobDifficulty = [9,9,9], d = 4 <strong>Output:</strong> -1 <strong>Explanation:</strong> If you finish a job per day you will still have a free day. you cannot find a schedule for the given jobs. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> jobDifficulty = [1,1,1], d = 3 <strong>Output:</strong> 3 <strong>Explanation:</strong> The schedule is one job per day. total difficulty will be 3. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= jobDifficulty.length &lt;= 300</code></li> <li><code>0 &lt;= jobDifficulty[i] &lt;= 1000</code></li> <li><code>1 &lt;= d &lt;= 10</code></li> </ul>
3
{ "code": "/*\n n is number of jobs\n\n max jobDifficulty table: mjt[i][j] means max jobDifficulty in range[i , j]\n\n dp[i][j]: means the minimum difficulty of a job schedule, if we finished 0 to j-th jobs on 0 to i-th day. (j >= i)\n \n dp[i][j] = dp[i-1][j-k] + max one in jobDifficulty [j-k+1, j] (1 <= k <= j-i+1)\n = dp[i-1][j-k] + mjt[j-k+1][j];\n \n pick a k, and try to minimize dp[i][j], takes o(j) time for dp[i][j], worst case O(n)\n\n total time complexity: O(n^3) -> O(n^2) or O(longn * n^2) ?\n\n final result: dp[d][n]\n*/\nclass Solution {\npublic:\n int minDifficulty(vector<int>& jobDifficulty, int d) {\n int n = jobDifficulty.size();\n\n //Max jobDifficulty table: mjt[i][j] means max jobDifficulty in range[i , j]\n vector<vector<int>> mjt(n+1, vector<int>(n+1, 0));\n for(int i = 1; i <= n; i++){\n for(int j = i; j <= n; j++){\n mjt[i][j] = max(mjt[i][j-1], jobDifficulty[j-1]);\n }\n }\n\n //dp[i][j]: means the minimum difficulty of a job schedule, if we finished 0 to j-th jobs on 0 to i-th day. (j >= i)\n vector<vector<int>> dp(d+1, vector<int>(n+1, 0));\n for(int j = 1; j<=n; j++){\n dp[0][j] = mjt[1][j];\n }\n dp[d][n] = -1;\n\n for(int i = 1; i <= d; i++){\n for(int j = i; j <= n; j++){//j = 2\n int minJobDifficultyToday = INT_MAX;\n for(int k = 1; k <= j-i+1; k++){//k = 1, 2\n minJobDifficultyToday = min(minJobDifficultyToday, dp[i-1][j-k] + mjt[j-k+1][j]);//dp[0][1]+mjt[2][2], dp[0][0]+mjt[1][2]\n }\n dp[i][j] = minJobDifficultyToday;\n }\n }\n\n return dp[d][n];\n\n }\n};", "memory": "15900" }
1,457
<p>You want to schedule a list of jobs in <code>d</code> days. Jobs are dependent (i.e To work on the <code>i<sup>th</sup></code> job, you have to finish all the jobs <code>j</code> where <code>0 &lt;= j &lt; i</code>).</p> <p>You have to finish <strong>at least</strong> one task every day. The difficulty of a job schedule is the sum of difficulties of each day of the <code>d</code> days. The difficulty of a day is the maximum difficulty of a job done on that day.</p> <p>You are given an integer array <code>jobDifficulty</code> and an integer <code>d</code>. The difficulty of the <code>i<sup>th</sup></code> job is <code>jobDifficulty[i]</code>.</p> <p>Return <em>the minimum difficulty of a job schedule</em>. If you cannot find a schedule for the jobs return <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/01/16/untitled.png" style="width: 365px; height: 370px;" /> <pre> <strong>Input:</strong> jobDifficulty = [6,5,4,3,2,1], d = 2 <strong>Output:</strong> 7 <strong>Explanation:</strong> First day you can finish the first 5 jobs, total difficulty = 6. Second day you can finish the last job, total difficulty = 1. The difficulty of the schedule = 6 + 1 = 7 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> jobDifficulty = [9,9,9], d = 4 <strong>Output:</strong> -1 <strong>Explanation:</strong> If you finish a job per day you will still have a free day. you cannot find a schedule for the given jobs. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> jobDifficulty = [1,1,1], d = 3 <strong>Output:</strong> 3 <strong>Explanation:</strong> The schedule is one job per day. total difficulty will be 3. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= jobDifficulty.length &lt;= 300</code></li> <li><code>0 &lt;= jobDifficulty[i] &lt;= 1000</code></li> <li><code>1 &lt;= d &lt;= 10</code></li> </ul>
3
{ "code": "#include <bits/stdc++.h>\nusing namespace std;\n\n/* Macros {{{ */\n/* A lot of this is from some of Benq's submissions\n [https://codeforces.com/profile/Benq]\n Ugly af to the eyes, but with vim fold its barable\n Hopefully c++20 concepts can make all this stuff must cleaner */\n\n/* Basics {{{ */\nusing ll = long long;\nusing ld = long double;\nusing str = string;\n\nusing pi = pair<int, int>;\nusing pll = pair<ll, ll>;\nusing pld = pair<ld, ld>;\n#define mp make_pair\n#define fi first\n#define se second\n\n#define arr array\n#define ve vector\nusing vi = vector<int>;\nusing vll = vector<ll>;\nusing vld = vector<ld>;\n\nusing vpi = vector<pi>;\nusing vpll = vector<pll>;\nusing vpld = vector<pld>;\n\nusing vvi = vector<vi>;\nusing vvll = vector<vll>;\nusing vvld = vector<vld>;\n\nusing vvpi = vector<vpi>;\nusing vvpll = vector<vpll>;\nusing vvpld = vector<vpld>;\n\n#define pb push_back\n#define lb lower_bound\n#define ub upper_bound\n#define sz size()\n#define rsz(a) resize(a)\n#define all(x) x.begin(), x.end()\n#define rall(x) x.rbegin(), x.rend()\n\n#define For(i, a, b) for (int i = a; i < b; ++i)\n#define Rof(i, a, b) for (int i = (b)-1; i >= (a); --i)\n#define rep(a) For(_, 0, a)\n#define each(a, x) for (auto &a : x)\n#define reach(a, x) for (auto a = x.rbegin(); a != x.rend(); ++a)\n\ntemplate <typename T, typename U>\ninline void cmin(T &x, U y) {\n if (y < x) x = y;\n}\ntemplate <typename T, typename U>\ninline void cmax(T &x, U y) {\n if (x < y) x = y;\n}\n/*}}}*/\n\n/* IO {{{ */\n\n/* Template Macros {{{ */\n#define tcT template <class T\n#define tcTU tcT, class U\n#define tcTUU tcT, class... U\n/*}}}*/\n\ninline namespace Helpers { /*{{{*/\ntcT, class = void > struct is_iterable : false_type {};\ntcT > struct is_iterable<\n T, void_t<decltype(begin(declval<T>())), decltype(end(declval<T>()))>>\n : true_type {};\ntcT > constexpr bool is_iterable_v = is_iterable<T>::value;\n\ntcT, class = void > struct is_readable : false_type {};\ntcT > struct is_readable<T, typename std::enable_if_t<is_same_v<\n decltype(cin >> declval<T &>()), istream &>>>\n : true_type {};\ntcT > constexpr bool is_readable_v = is_readable<T>::value;\n\ntcT, class = void > struct is_printable : false_type {};\ntcT > struct is_printable<T, typename std::enable_if_t<is_same_v<\n decltype(cout << declval<T>()), ostream &>>>\n : true_type {};\ntcT > constexpr bool is_printable_v = is_printable<T>::value;\n} /* namespace Helpers */\n/*}}}*/\n\ninline namespace Input { /*{{{*/\ntcT > constexpr bool needs_input_v = !is_readable_v<T> && is_iterable_v<T>;\ntcTUU > void re(T &t, U &...u);\ntcTU > void re(pair<T, U> &p); /* pairs */\n\n/* re: read{{{ */\ntcT > typename enable_if<is_readable_v<T>, void>::type re(T &x) {\n cin >> x;\n} /* default */\ntcT > typename enable_if<needs_input_v<T>, void>::type re(\n T &i); // vectors, arrays, etc...\ntcTU > void re(pair<T, U> &p) { re(p.fi, p.se); } // pairs\ntcT > typename enable_if<needs_input_v<T>, void>::type re(T &i) {\n each(x, i) re(x);\n}\ntcTUU > void re(T &t, U &...u) {\n re(t);\n re(u...);\n} /* read multiple}}} */\n\n/* rv: resize and read vectors{{{ */\nvoid rv(size_t) {}\ntcTUU > void rv(size_t N, ve<T> &t, U &...u);\ntemplate <class... U>\nvoid rv(size_t, size_t N2, U &...u);\ntcTUU > void rv(size_t N, ve<T> &t, U &...u) {\n t.rsz(N);\n re(t);\n rv(N, u...);\n}\ntemplate <class... U>\nvoid rv(size_t, size_t N2, U &...u) {\n rv(N2, u...);\n} /*}}}*/\n\n/* dumb shortcuts to read in ints{{{ */\nvoid decrement() {} /* subtract one from each */\ntcTUU > void decrement(T &t, U &...u) {\n --t;\n decrement(u...);\n}\n#define ints(...) \\\n int __VA_ARGS__; \\\n re(__VA_ARGS__);\n#define int1(...) \\\n ints(__VA_ARGS__); \\\n decrement(__VA_ARGS__); /*}}}*/\n} /* namespace Input */\n/*}}}*/\n\ninline namespace ToString { /*{{{*/\ntcT > constexpr bool needs_output_v = !is_printable_v<T> && is_iterable_v<T>;\n\n/* ts: string representation to print */\ntcT > typename enable_if<is_printable_v<T>, str>::type ts(T v) {\n stringstream ss;\n ss << fixed << setprecision(15) << v;\n return ss.str();\n} /* default */\ntcT > str bit_vec(T t) { /* bit vector to string */\n str res = \"{\";\n For(i, 0, t.sz) res += ts(t[i]);\n res += \"}\";\n return res;\n}\nstr ts(ve<bool> v) { return bit_vec(v); }\ntemplate <size_t SZ>\nstr ts(bitset<SZ> b) {\n return bit_vec(b);\n} /* bit vector */\ntcTU > str ts(pair<T, U> p); /* pairs */\ntcT > typename enable_if<needs_output_v<T>, str>::type ts(\n T v); /* vectors, arrays */\ntcTU > str ts(pair<T, U> p) { return \"(\" + ts(p.fi) + \", \" + ts(p.se) + \")\"; }\ntcT > typename enable_if<is_iterable_v<T>, str>::type ts_sep(T v, str sep) {\n /* convert container to string w/ separator sep */\n bool fst = 1;\n str res = \"\";\n for (const auto &x : v) {\n if (!fst) res += sep;\n fst = 0;\n res += ts(x);\n }\n return res;\n}\ntcT > typename enable_if<needs_output_v<T>, str>::type ts(T v) {\n return \"{\" + ts_sep(v, \", \") + \"}\";\n}\n\n/* for nested DS */\ntemplate <int, class T>\ntypename enable_if<!needs_output_v<T>, ve<str>>::type ts_lev(const T &v) {\n return {ts(v)};\n}\ntemplate <int lev, class T>\ntypename enable_if<needs_output_v<T>, ve<str>>::type ts_lev(const T &v) {\n if (lev == 0 || !v.sz) return {ts(v)};\n ve<str> res;\n for (const auto &t : v) {\n if (res.sz) res.back() += \",\";\n ve<str> tmp = ts_lev<lev - 1>(t);\n res.insert(end(res), all(tmp));\n }\n For(i, 0, res.sz) {\n str bef = \" \";\n if (i == 0) bef = \"{\";\n res[i] = bef + res[i];\n }\n res.back() += \"}\";\n return res;\n}\n} /* namespace ToString */\n/*}}}*/\n\ninline namespace Output { /*{{{*/\ntemplate <class T>\nvoid pr_sep(ostream &os, str, const T &t) {\n os << ts(t);\n}\ntemplate <class T, class... U>\nvoid pr_sep(ostream &os, str sep, const T &t, const U &...u) {\n pr_sep(os, sep, t);\n os << sep;\n pr_sep(os, sep, u...);\n}\n/* print w/ no spaces */\ntemplate <class... T>\nvoid pr(const T &...t) {\n pr_sep(cout, \"\", t...);\n}\n/* print w/ spaces, end with newline */\nvoid ps() { cout << \"\\n\"; }\ntemplate <class... T>\nvoid ps(const T &...t) {\n pr_sep(cout, \" \", t...);\n ps();\n}\n/* debug to cerr */\ntemplate <class... T>\nvoid dbg_out(const T &...t) {\n pr_sep(cerr, \" | \", t...);\n cerr << endl;\n}\nvoid loc_info(int line, str names) {\n cerr << \"Line(\" << line << \") -> [\" << names << \"]: \";\n}\ntemplate <int lev, class T>\nvoid dbgl_out(const T &t) {\n cerr << \"\\n\\n\" << ts_sep(ts_lev<lev>(t), \"\\n\") << \"\\n\" << endl;\n}\n} /* namespace Output */\n/*}}}}}}}}}*/\n\n\nclass Solution {\npublic:\n int minDifficulty(vector<int>& jobDifficulty, int d) {\n\n // let dp[i][d] be the solution to jobDifficulty[i:] with d days\n // dp[i][d] = min{j=i+1; j<n; ++j}( dp[j][d-1] + max([i:j]) )\n\n int n = jobDifficulty.size();\n if(n < d) return -1;\n\n vvi mxThing(n+1, vi(n+1, 0));\n for(int j=0; j<n; ++j) for(int i=j; i>=0; --i) {\n mxThing[i][j] = max(jobDifficulty[i], mxThing[i+1][j]);\n }\n\n vvi dp(n+1, vi(d+1, INT_MAX/2));\n dp[n][d] = 0;\n\n // base case will be dp[i<n][cd=d] = INT_MAX/2 -- I have not done all the jobs\n // base case will be dp[i=n][cd<d] = INT_MAX/2 -- I finish the jobs too early\n // base case will be dp[i=n][cd=d] = 0 perfect\n\n for(int cd=d-1; cd>=0; --cd) {\n // interval to be done on day cd\n for(int i=0; i<n; ++i) for(int j=i; j<n; ++j) {\n cmin(dp[i][cd], dp[j+1][cd+1] + mxThing[i][j]/* + max(diff[i:j+1]) */ );\n }\n }\n\n return dp[0][0];\n }\n};\n", "memory": "16000" }
1,457
<p>You want to schedule a list of jobs in <code>d</code> days. Jobs are dependent (i.e To work on the <code>i<sup>th</sup></code> job, you have to finish all the jobs <code>j</code> where <code>0 &lt;= j &lt; i</code>).</p> <p>You have to finish <strong>at least</strong> one task every day. The difficulty of a job schedule is the sum of difficulties of each day of the <code>d</code> days. The difficulty of a day is the maximum difficulty of a job done on that day.</p> <p>You are given an integer array <code>jobDifficulty</code> and an integer <code>d</code>. The difficulty of the <code>i<sup>th</sup></code> job is <code>jobDifficulty[i]</code>.</p> <p>Return <em>the minimum difficulty of a job schedule</em>. If you cannot find a schedule for the jobs return <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/01/16/untitled.png" style="width: 365px; height: 370px;" /> <pre> <strong>Input:</strong> jobDifficulty = [6,5,4,3,2,1], d = 2 <strong>Output:</strong> 7 <strong>Explanation:</strong> First day you can finish the first 5 jobs, total difficulty = 6. Second day you can finish the last job, total difficulty = 1. The difficulty of the schedule = 6 + 1 = 7 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> jobDifficulty = [9,9,9], d = 4 <strong>Output:</strong> -1 <strong>Explanation:</strong> If you finish a job per day you will still have a free day. you cannot find a schedule for the given jobs. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> jobDifficulty = [1,1,1], d = 3 <strong>Output:</strong> 3 <strong>Explanation:</strong> The schedule is one job per day. total difficulty will be 3. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= jobDifficulty.length &lt;= 300</code></li> <li><code>0 &lt;= jobDifficulty[i] &lt;= 1000</code></li> <li><code>1 &lt;= d &lt;= 10</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n long long dp[1000][1000];\n int solve(int index , vector<int> &nums , int n , int cnt , int d)\n {\n // base case\n if(index >= n)\n {\n if(cnt == d)\n {\n return 0;\n }\n else\n {\n return 1e9;\n }\n }\n\n if(dp[index][cnt] != -1)\n {\n return dp[index][cnt];\n }\n\n int ans = 1e9;\n int maxi = -1e9;\n for(int i = index ; i < n ; i++)\n {\n maxi = max(maxi , nums[i]);\n int val = maxi + solve(i+1 , nums , n , cnt+1 , d);\n ans = min(ans , val);\n }\n\n return dp[index][cnt] = ans;\n }\n int minDifficulty(vector<int>& nums, int d) {\n int n = nums.size();\n memset(dp , -1 , sizeof(dp));\n int cnt = 0;\n\n int ans = solve(0 , nums , n , cnt , d);\n\n if(ans == 1e9)\n {\n return -1;\n }\n else\n {\n return ans;\n }\n }\n};", "memory": "18100" }
1,457
<p>You want to schedule a list of jobs in <code>d</code> days. Jobs are dependent (i.e To work on the <code>i<sup>th</sup></code> job, you have to finish all the jobs <code>j</code> where <code>0 &lt;= j &lt; i</code>).</p> <p>You have to finish <strong>at least</strong> one task every day. The difficulty of a job schedule is the sum of difficulties of each day of the <code>d</code> days. The difficulty of a day is the maximum difficulty of a job done on that day.</p> <p>You are given an integer array <code>jobDifficulty</code> and an integer <code>d</code>. The difficulty of the <code>i<sup>th</sup></code> job is <code>jobDifficulty[i]</code>.</p> <p>Return <em>the minimum difficulty of a job schedule</em>. If you cannot find a schedule for the jobs return <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/01/16/untitled.png" style="width: 365px; height: 370px;" /> <pre> <strong>Input:</strong> jobDifficulty = [6,5,4,3,2,1], d = 2 <strong>Output:</strong> 7 <strong>Explanation:</strong> First day you can finish the first 5 jobs, total difficulty = 6. Second day you can finish the last job, total difficulty = 1. The difficulty of the schedule = 6 + 1 = 7 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> jobDifficulty = [9,9,9], d = 4 <strong>Output:</strong> -1 <strong>Explanation:</strong> If you finish a job per day you will still have a free day. you cannot find a schedule for the given jobs. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> jobDifficulty = [1,1,1], d = 3 <strong>Output:</strong> 3 <strong>Explanation:</strong> The schedule is one job per day. total difficulty will be 3. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= jobDifficulty.length &lt;= 300</code></li> <li><code>0 &lt;= jobDifficulty[i] &lt;= 1000</code></li> <li><code>1 &lt;= d &lt;= 10</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int minDifficulty(vector<int>& jobDifficulty, int d) {\n\n if( d > jobDifficulty.size() )\n return -1;\n\n vector< vector<long int> > maxCost( jobDifficulty.size() + 1, vector<long int>( jobDifficulty.size() + 1 , 0 ) );\n\n for( int i = 1 ; i <= jobDifficulty.size() ; i++ ){\n for( int j = i ; j <= jobDifficulty.size() ; j++ ){\n\n maxCost[i][j] = max( maxCost[i][j-1], (long) jobDifficulty[j-1] );\n\n }\n }\n\n vector<long int> miniCost( jobDifficulty.size()+1, INT_MAX );\n\n miniCost[ 0 ] = 0;\n\n long int val;\n\n for( int day = 1 ; day <= d ; day++ ){\n\n for( int i = jobDifficulty.size() ; i >= day; i-- ){\n\n val = INT_MAX;\n\n for( int j = day ; j <= i ; j++ ){\n\n val = min( val, miniCost[j-1] + maxCost[j][i] );\n }\n\n miniCost[i] = val;\n }\n }\n\n\n return miniCost.back();\n \n }\n\n\n \n};", "memory": "20200" }
1,457
<p>You want to schedule a list of jobs in <code>d</code> days. Jobs are dependent (i.e To work on the <code>i<sup>th</sup></code> job, you have to finish all the jobs <code>j</code> where <code>0 &lt;= j &lt; i</code>).</p> <p>You have to finish <strong>at least</strong> one task every day. The difficulty of a job schedule is the sum of difficulties of each day of the <code>d</code> days. The difficulty of a day is the maximum difficulty of a job done on that day.</p> <p>You are given an integer array <code>jobDifficulty</code> and an integer <code>d</code>. The difficulty of the <code>i<sup>th</sup></code> job is <code>jobDifficulty[i]</code>.</p> <p>Return <em>the minimum difficulty of a job schedule</em>. If you cannot find a schedule for the jobs return <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/01/16/untitled.png" style="width: 365px; height: 370px;" /> <pre> <strong>Input:</strong> jobDifficulty = [6,5,4,3,2,1], d = 2 <strong>Output:</strong> 7 <strong>Explanation:</strong> First day you can finish the first 5 jobs, total difficulty = 6. Second day you can finish the last job, total difficulty = 1. The difficulty of the schedule = 6 + 1 = 7 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> jobDifficulty = [9,9,9], d = 4 <strong>Output:</strong> -1 <strong>Explanation:</strong> If you finish a job per day you will still have a free day. you cannot find a schedule for the given jobs. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> jobDifficulty = [1,1,1], d = 3 <strong>Output:</strong> 3 <strong>Explanation:</strong> The schedule is one job per day. total difficulty will be 3. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= jobDifficulty.length &lt;= 300</code></li> <li><code>0 &lt;= jobDifficulty[i] &lt;= 1000</code></li> <li><code>1 &lt;= d &lt;= 10</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int minDifficulty(vector<int>& jobDifficulty, int d) {\n // find a d partition of the jobDifficulty array to minimize the sum of the max of each subarray\n vector<int>& jd = jobDifficulty;\n int N = jd.size();\n if(N<d)\n return -1;\n vector<vector<map<int, int>>> res(N, vector<map<int,int>>(d+1));\n res[0][1][jd[0]] = jd[0];\n for(int i=1; i<N; ++i) {\n //jd[i] attach to existing subarray:\n for(int k=1; k<=d; ++k) {\n for(auto [x, y]: res[i-1][k]) {\n if(x < jd[i]) {\n auto it = res[i][k].find(jd[i]);\n if(it == res[i][k].end())\n res[i][k][jd[i]] = res[i-1][k][x] + (jd[i] - x);\n else\n it->second = min(it->second, res[i-1][k][x] + (jd[i] - x));\n }\n else {\n res[i][k][x] = y;\n }\n }\n }\n for(int k=2; k<=d; ++k) { // starting a new subarray:\n int mini = INT_MAX;\n for(auto [z,w]: res[i-1][k-1]) {\n mini = min(w, mini);\n }\n if(mini != INT_MAX) {\n auto it = res[i][k].find(jd[i]);\n \n if(it == res[i][k].end()) {\n res[i][k][jd[i]] = mini + jd[i];\n }\n \n else {\n \n it->second = min(it->second, mini+jd[i]);\n \n }\n }\n \n }\n }\n #if 0\n for(int i=0; i<N; ++i) {\n cout << \"i= \" << i << \"---- \";\n for(int j=1; j<=d; ++j) {\n for(auto [x,y]: res[i][j])\n cout << x <<\",\" <<j<<\" \";\n }\n cout << \"\\n\";\n }\n #endif \n int ans = INT_MAX;\n for(auto [x,y]: res[N-1][d]) {\n ans = min(y, ans);\n }\n return ans;\n }\n};", "memory": "21000" }
1,463
<p>You are given an <code>m x n</code> binary matrix <code>mat</code> of <code>1</code>&#39;s (representing soldiers) and <code>0</code>&#39;s (representing civilians). The soldiers are positioned <strong>in front</strong> of the civilians. That is, all the <code>1</code>&#39;s will appear to the <strong>left</strong> of all the <code>0</code>&#39;s in each row.</p> <p>A row <code>i</code> is <strong>weaker</strong> than a row <code>j</code> if one of the following is true:</p> <ul> <li>The number of soldiers in row <code>i</code> is less than the number of soldiers in row <code>j</code>.</li> <li>Both rows have the same number of soldiers and <code>i &lt; j</code>.</li> </ul> <p>Return <em>the indices of the </em><code>k</code><em> <strong>weakest</strong> rows in the matrix ordered from weakest to strongest</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> mat = [[1,1,0,0,0], [1,1,1,1,0], [1,0,0,0,0], [1,1,0,0,0], [1,1,1,1,1]], k = 3 <strong>Output:</strong> [2,0,3] <strong>Explanation:</strong> The number of soldiers in each row is: - Row 0: 2 - Row 1: 4 - Row 2: 1 - Row 3: 2 - Row 4: 5 The rows ordered from weakest to strongest are [2,0,3,1,4]. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> mat = [[1,0,0,0], [1,1,1,1], [1,0,0,0], [1,0,0,0]], k = 2 <strong>Output:</strong> [0,2] <strong>Explanation:</strong> The number of soldiers in each row is: - Row 0: 1 - Row 1: 4 - Row 2: 1 - Row 3: 1 The rows ordered from weakest to strongest are [0,2,3,1]. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == mat.length</code></li> <li><code>n == mat[i].length</code></li> <li><code>2 &lt;= n, m &lt;= 100</code></li> <li><code>1 &lt;= k &lt;= m</code></li> <li><code>matrix[i][j]</code> is either 0 or 1.</li> </ul>
0
{ "code": "class Solution {\npublic:\n int searchForSoldiers(vector<int>& m) {\n int l = 0;\n int h = m.size() - 1;\n while (l <= h) {\n int mid = l + (h - l) / 2;\n if (m[mid] == 0)\n h = mid - 1;\n else\n l = mid + 1;\n }\n return l;\n }\n\n vector<int> kWeakestRows(vector<vector<int>>& mat, int k) {\n vector<pair<int, int>> p;\n vector<int> res;\n for (int i = 0; i < mat.size(); i++) {\n int count = searchForSoldiers(mat[i]);\n p.push_back({count, i});\n }\n sort(p.begin(), p.end());\n for (int i = 0; i < k; i++) {\n res.push_back(p[i].second);\n }\n return res;\n }\n};", "memory": "13100" }
1,463
<p>You are given an <code>m x n</code> binary matrix <code>mat</code> of <code>1</code>&#39;s (representing soldiers) and <code>0</code>&#39;s (representing civilians). The soldiers are positioned <strong>in front</strong> of the civilians. That is, all the <code>1</code>&#39;s will appear to the <strong>left</strong> of all the <code>0</code>&#39;s in each row.</p> <p>A row <code>i</code> is <strong>weaker</strong> than a row <code>j</code> if one of the following is true:</p> <ul> <li>The number of soldiers in row <code>i</code> is less than the number of soldiers in row <code>j</code>.</li> <li>Both rows have the same number of soldiers and <code>i &lt; j</code>.</li> </ul> <p>Return <em>the indices of the </em><code>k</code><em> <strong>weakest</strong> rows in the matrix ordered from weakest to strongest</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> mat = [[1,1,0,0,0], [1,1,1,1,0], [1,0,0,0,0], [1,1,0,0,0], [1,1,1,1,1]], k = 3 <strong>Output:</strong> [2,0,3] <strong>Explanation:</strong> The number of soldiers in each row is: - Row 0: 2 - Row 1: 4 - Row 2: 1 - Row 3: 2 - Row 4: 5 The rows ordered from weakest to strongest are [2,0,3,1,4]. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> mat = [[1,0,0,0], [1,1,1,1], [1,0,0,0], [1,0,0,0]], k = 2 <strong>Output:</strong> [0,2] <strong>Explanation:</strong> The number of soldiers in each row is: - Row 0: 1 - Row 1: 4 - Row 2: 1 - Row 3: 1 The rows ordered from weakest to strongest are [0,2,3,1]. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == mat.length</code></li> <li><code>n == mat[i].length</code></li> <li><code>2 &lt;= n, m &lt;= 100</code></li> <li><code>1 &lt;= k &lt;= m</code></li> <li><code>matrix[i][j]</code> is either 0 or 1.</li> </ul>
0
{ "code": "class Solution {\npublic:\n vector<int> kWeakestRows(vector<vector<int>>& mat, int k) {\n vector<pair<int,int>> soldiersCountRow;\n vector<int> ans;\n int row = mat.size(), col = mat[0].size();\n\n for(int i=0; i<row; i++){\n int cnt = -1, start=0, end=col-1;\n\n while(start<=end){\n int mid = start + (end-start)/2;\n \n if(mat[i][mid] == 1){\n cnt = mid;\n start = mid+1;\n }\n else\n end = mid-1;\n }\n soldiersCountRow.push_back({cnt+1, i});\n }\n\n sort(soldiersCountRow.begin(), soldiersCountRow.end());\n\n for(int i=0; i<k; i++)\n ans.push_back(soldiersCountRow[i].second);\n\n return ans;\n }\n};", "memory": "13100" }
1,463
<p>You are given an <code>m x n</code> binary matrix <code>mat</code> of <code>1</code>&#39;s (representing soldiers) and <code>0</code>&#39;s (representing civilians). The soldiers are positioned <strong>in front</strong> of the civilians. That is, all the <code>1</code>&#39;s will appear to the <strong>left</strong> of all the <code>0</code>&#39;s in each row.</p> <p>A row <code>i</code> is <strong>weaker</strong> than a row <code>j</code> if one of the following is true:</p> <ul> <li>The number of soldiers in row <code>i</code> is less than the number of soldiers in row <code>j</code>.</li> <li>Both rows have the same number of soldiers and <code>i &lt; j</code>.</li> </ul> <p>Return <em>the indices of the </em><code>k</code><em> <strong>weakest</strong> rows in the matrix ordered from weakest to strongest</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> mat = [[1,1,0,0,0], [1,1,1,1,0], [1,0,0,0,0], [1,1,0,0,0], [1,1,1,1,1]], k = 3 <strong>Output:</strong> [2,0,3] <strong>Explanation:</strong> The number of soldiers in each row is: - Row 0: 2 - Row 1: 4 - Row 2: 1 - Row 3: 2 - Row 4: 5 The rows ordered from weakest to strongest are [2,0,3,1,4]. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> mat = [[1,0,0,0], [1,1,1,1], [1,0,0,0], [1,0,0,0]], k = 2 <strong>Output:</strong> [0,2] <strong>Explanation:</strong> The number of soldiers in each row is: - Row 0: 1 - Row 1: 4 - Row 2: 1 - Row 3: 1 The rows ordered from weakest to strongest are [0,2,3,1]. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == mat.length</code></li> <li><code>n == mat[i].length</code></li> <li><code>2 &lt;= n, m &lt;= 100</code></li> <li><code>1 &lt;= k &lt;= m</code></li> <li><code>matrix[i][j]</code> is either 0 or 1.</li> </ul>
0
{ "code": "class Solution {\npublic:\n vector<int> kWeakestRows(vector<vector<int>>& mat, int k) {\n vector<pair<int,int>>v;\n vector<int>ans;\n for(int i=0;i<mat.size();i++)\n {\n int sum=0;\n for(int j=0;j<mat[i].size();j++)\n {\n sum=sum+mat[i][j];\n }\n v.push_back({sum,i});\n }\n sort(v.begin(),v.end());\n for(int i=0;i<k;i++)\n {\n ans.push_back(v[i].second);\n }\n return ans;\n }\n};", "memory": "13200" }
1,463
<p>You are given an <code>m x n</code> binary matrix <code>mat</code> of <code>1</code>&#39;s (representing soldiers) and <code>0</code>&#39;s (representing civilians). The soldiers are positioned <strong>in front</strong> of the civilians. That is, all the <code>1</code>&#39;s will appear to the <strong>left</strong> of all the <code>0</code>&#39;s in each row.</p> <p>A row <code>i</code> is <strong>weaker</strong> than a row <code>j</code> if one of the following is true:</p> <ul> <li>The number of soldiers in row <code>i</code> is less than the number of soldiers in row <code>j</code>.</li> <li>Both rows have the same number of soldiers and <code>i &lt; j</code>.</li> </ul> <p>Return <em>the indices of the </em><code>k</code><em> <strong>weakest</strong> rows in the matrix ordered from weakest to strongest</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> mat = [[1,1,0,0,0], [1,1,1,1,0], [1,0,0,0,0], [1,1,0,0,0], [1,1,1,1,1]], k = 3 <strong>Output:</strong> [2,0,3] <strong>Explanation:</strong> The number of soldiers in each row is: - Row 0: 2 - Row 1: 4 - Row 2: 1 - Row 3: 2 - Row 4: 5 The rows ordered from weakest to strongest are [2,0,3,1,4]. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> mat = [[1,0,0,0], [1,1,1,1], [1,0,0,0], [1,0,0,0]], k = 2 <strong>Output:</strong> [0,2] <strong>Explanation:</strong> The number of soldiers in each row is: - Row 0: 1 - Row 1: 4 - Row 2: 1 - Row 3: 1 The rows ordered from weakest to strongest are [0,2,3,1]. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == mat.length</code></li> <li><code>n == mat[i].length</code></li> <li><code>2 &lt;= n, m &lt;= 100</code></li> <li><code>1 &lt;= k &lt;= m</code></li> <li><code>matrix[i][j]</code> is either 0 or 1.</li> </ul>
0
{ "code": "class Solution {\npublic:\n vector<int> kWeakestRows(vector<vector<int>>& mat, int k) {\n // Vector to store the number of soldiers and the corresponding row index\n vector<pair<int, int>> soldierCount;\n \n // Iterate through each row of the matrix\n for (int i = 0; i < mat.size(); ++i) {\n // Count the number of 1's in the row using the inbuilt count function\n int cnt = count(mat[i].begin(), mat[i].end(), 1);\n // Store the count of soldiers and the row index\n soldierCount.push_back({cnt, i});\n }\n \n // Sort the soldierCount vector by the number of soldiers and row index\n sort(soldierCount.begin(), soldierCount.end(), [](const pair<int, int>& a, const pair<int, int>& b) {\n // First compare the number of soldiers\n if (a.first == b.first) \n return a.second < b.second; // If soldiers are the same, compare by row index\n return a.first < b.first; // Otherwise, sort by number of soldiers\n });\n \n // Vector to store the result\n vector<int> result;\n \n // Get the first k rows\n for (int i = 0; i < k; ++i) {\n result.push_back(soldierCount[i].second);\n }\n \n return result;\n }\n \n};", "memory": "13200" }
1,463
<p>You are given an <code>m x n</code> binary matrix <code>mat</code> of <code>1</code>&#39;s (representing soldiers) and <code>0</code>&#39;s (representing civilians). The soldiers are positioned <strong>in front</strong> of the civilians. That is, all the <code>1</code>&#39;s will appear to the <strong>left</strong> of all the <code>0</code>&#39;s in each row.</p> <p>A row <code>i</code> is <strong>weaker</strong> than a row <code>j</code> if one of the following is true:</p> <ul> <li>The number of soldiers in row <code>i</code> is less than the number of soldiers in row <code>j</code>.</li> <li>Both rows have the same number of soldiers and <code>i &lt; j</code>.</li> </ul> <p>Return <em>the indices of the </em><code>k</code><em> <strong>weakest</strong> rows in the matrix ordered from weakest to strongest</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> mat = [[1,1,0,0,0], [1,1,1,1,0], [1,0,0,0,0], [1,1,0,0,0], [1,1,1,1,1]], k = 3 <strong>Output:</strong> [2,0,3] <strong>Explanation:</strong> The number of soldiers in each row is: - Row 0: 2 - Row 1: 4 - Row 2: 1 - Row 3: 2 - Row 4: 5 The rows ordered from weakest to strongest are [2,0,3,1,4]. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> mat = [[1,0,0,0], [1,1,1,1], [1,0,0,0], [1,0,0,0]], k = 2 <strong>Output:</strong> [0,2] <strong>Explanation:</strong> The number of soldiers in each row is: - Row 0: 1 - Row 1: 4 - Row 2: 1 - Row 3: 1 The rows ordered from weakest to strongest are [0,2,3,1]. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == mat.length</code></li> <li><code>n == mat[i].length</code></li> <li><code>2 &lt;= n, m &lt;= 100</code></li> <li><code>1 &lt;= k &lt;= m</code></li> <li><code>matrix[i][j]</code> is either 0 or 1.</li> </ul>
1
{ "code": "class row\n{\n public:\n int idx;\n int count;\n\n row(int count,int idx)\n {\n this->idx = idx;\n this->count = count;\n } \n\n bool operator < (const row& obj) const\n {\n if(this->count == obj.count)\n {\n return this->idx >obj.idx;\n }\n return this->count>obj.count;\n }\n};\n\nclass Solution {\npublic:\n vector<int> kWeakestRows(vector<vector<int>>& mat, int k) {\n vector<row> rows;\n\n for(int i=0;i<mat.size();i++)\n {\n int c =0;\n\n for(int j=0 ; j<mat[i].size() && mat[i][j] == 1; j++)\n {\n c++;\n }\n\n rows.push_back(row(c,i));\n }\n\n priority_queue<row> pq(rows.begin(), rows.end());\n vector<int> ans;\n while(k>0)\n {\n ans.push_back(pq.top().idx);\n pq.pop();\n k--;\n }\n return ans;\n }\n};", "memory": "13300" }
1,463
<p>You are given an <code>m x n</code> binary matrix <code>mat</code> of <code>1</code>&#39;s (representing soldiers) and <code>0</code>&#39;s (representing civilians). The soldiers are positioned <strong>in front</strong> of the civilians. That is, all the <code>1</code>&#39;s will appear to the <strong>left</strong> of all the <code>0</code>&#39;s in each row.</p> <p>A row <code>i</code> is <strong>weaker</strong> than a row <code>j</code> if one of the following is true:</p> <ul> <li>The number of soldiers in row <code>i</code> is less than the number of soldiers in row <code>j</code>.</li> <li>Both rows have the same number of soldiers and <code>i &lt; j</code>.</li> </ul> <p>Return <em>the indices of the </em><code>k</code><em> <strong>weakest</strong> rows in the matrix ordered from weakest to strongest</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> mat = [[1,1,0,0,0], [1,1,1,1,0], [1,0,0,0,0], [1,1,0,0,0], [1,1,1,1,1]], k = 3 <strong>Output:</strong> [2,0,3] <strong>Explanation:</strong> The number of soldiers in each row is: - Row 0: 2 - Row 1: 4 - Row 2: 1 - Row 3: 2 - Row 4: 5 The rows ordered from weakest to strongest are [2,0,3,1,4]. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> mat = [[1,0,0,0], [1,1,1,1], [1,0,0,0], [1,0,0,0]], k = 2 <strong>Output:</strong> [0,2] <strong>Explanation:</strong> The number of soldiers in each row is: - Row 0: 1 - Row 1: 4 - Row 2: 1 - Row 3: 1 The rows ordered from weakest to strongest are [0,2,3,1]. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == mat.length</code></li> <li><code>n == mat[i].length</code></li> <li><code>2 &lt;= n, m &lt;= 100</code></li> <li><code>1 &lt;= k &lt;= m</code></li> <li><code>matrix[i][j]</code> is either 0 or 1.</li> </ul>
1
{ "code": "class Solution {\npublic:\n #define pii pair<int,int>\n vector<int> kWeakestRows(vector<vector<int>>& mat, int k) {\n \n \tpriority_queue<pii, vector<pii>, greater<pii>> pq;\n\n\tint n = mat.size();\n\tint m = mat[0].size();\n\n\tint count = 0;\n\tfor (int i = 0; i < n; i++)\n\t{\n\t\tcount = 0;\n\t\tfor (int j = 0; j < m; j++)\n\t\t{\n\t\t\tif (mat[i][j])\n\t\t\t\tcount++;\n\t\t}\n\t\t//if (count)\n\t\t\tpq.push({ count, i });\n\t}\n\tvector<int>res;\n\twhile (k--)\n\t{\n\t\tres.push_back(pq.top().second);\n\t\tpq.pop();\n\t}\n\treturn res;\n \n }\n};", "memory": "13300" }
1,463
<p>You are given an <code>m x n</code> binary matrix <code>mat</code> of <code>1</code>&#39;s (representing soldiers) and <code>0</code>&#39;s (representing civilians). The soldiers are positioned <strong>in front</strong> of the civilians. That is, all the <code>1</code>&#39;s will appear to the <strong>left</strong> of all the <code>0</code>&#39;s in each row.</p> <p>A row <code>i</code> is <strong>weaker</strong> than a row <code>j</code> if one of the following is true:</p> <ul> <li>The number of soldiers in row <code>i</code> is less than the number of soldiers in row <code>j</code>.</li> <li>Both rows have the same number of soldiers and <code>i &lt; j</code>.</li> </ul> <p>Return <em>the indices of the </em><code>k</code><em> <strong>weakest</strong> rows in the matrix ordered from weakest to strongest</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> mat = [[1,1,0,0,0], [1,1,1,1,0], [1,0,0,0,0], [1,1,0,0,0], [1,1,1,1,1]], k = 3 <strong>Output:</strong> [2,0,3] <strong>Explanation:</strong> The number of soldiers in each row is: - Row 0: 2 - Row 1: 4 - Row 2: 1 - Row 3: 2 - Row 4: 5 The rows ordered from weakest to strongest are [2,0,3,1,4]. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> mat = [[1,0,0,0], [1,1,1,1], [1,0,0,0], [1,0,0,0]], k = 2 <strong>Output:</strong> [0,2] <strong>Explanation:</strong> The number of soldiers in each row is: - Row 0: 1 - Row 1: 4 - Row 2: 1 - Row 3: 1 The rows ordered from weakest to strongest are [0,2,3,1]. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == mat.length</code></li> <li><code>n == mat[i].length</code></li> <li><code>2 &lt;= n, m &lt;= 100</code></li> <li><code>1 &lt;= k &lt;= m</code></li> <li><code>matrix[i][j]</code> is either 0 or 1.</li> </ul>
2
{ "code": "class Solution {\npublic:\n vector<int> kWeakestRows(vector<vector<int>>& mat, int k) {\n priority_queue<pair<int,int>> pq;\n int len = mat.size();\n for(int i=0;i<len;i++){\n int cnt = count(mat[i].begin(),mat[i].end(),1);\n pq.push({cnt,i});\n if(pq.size()>k) pq.pop();\n }\n vector<int> ans;\n while(!pq.empty()){\n ans.push_back(pq.top().second);\n pq.pop();\n }\n reverse(ans.begin(),ans.end());\n return ans;\n }\n};", "memory": "13400" }
1,463
<p>You are given an <code>m x n</code> binary matrix <code>mat</code> of <code>1</code>&#39;s (representing soldiers) and <code>0</code>&#39;s (representing civilians). The soldiers are positioned <strong>in front</strong> of the civilians. That is, all the <code>1</code>&#39;s will appear to the <strong>left</strong> of all the <code>0</code>&#39;s in each row.</p> <p>A row <code>i</code> is <strong>weaker</strong> than a row <code>j</code> if one of the following is true:</p> <ul> <li>The number of soldiers in row <code>i</code> is less than the number of soldiers in row <code>j</code>.</li> <li>Both rows have the same number of soldiers and <code>i &lt; j</code>.</li> </ul> <p>Return <em>the indices of the </em><code>k</code><em> <strong>weakest</strong> rows in the matrix ordered from weakest to strongest</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> mat = [[1,1,0,0,0], [1,1,1,1,0], [1,0,0,0,0], [1,1,0,0,0], [1,1,1,1,1]], k = 3 <strong>Output:</strong> [2,0,3] <strong>Explanation:</strong> The number of soldiers in each row is: - Row 0: 2 - Row 1: 4 - Row 2: 1 - Row 3: 2 - Row 4: 5 The rows ordered from weakest to strongest are [2,0,3,1,4]. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> mat = [[1,0,0,0], [1,1,1,1], [1,0,0,0], [1,0,0,0]], k = 2 <strong>Output:</strong> [0,2] <strong>Explanation:</strong> The number of soldiers in each row is: - Row 0: 1 - Row 1: 4 - Row 2: 1 - Row 3: 1 The rows ordered from weakest to strongest are [0,2,3,1]. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == mat.length</code></li> <li><code>n == mat[i].length</code></li> <li><code>2 &lt;= n, m &lt;= 100</code></li> <li><code>1 &lt;= k &lt;= m</code></li> <li><code>matrix[i][j]</code> is either 0 or 1.</li> </ul>
2
{ "code": "class Solution {\npublic:\n vector<int> kWeakestRows(vector<vector<int>>& mat, int k) {\n set<pair<int,int>>s;\n for(int i=0;i<mat.size();i++)\n {\n int count=0;\n for(int j=0;j<mat[i].size();j++)\n {\n if(mat[i][j]==1)\n {\n count++;\n }\n }\n s.insert({count,i});\n }\n vector<int>v;\n for(auto x=begin(s);k>0;k--,x++)\n {\n v.push_back(x->second);\n }\n return v;\n \n }\n};", "memory": "13400" }
1,463
<p>You are given an <code>m x n</code> binary matrix <code>mat</code> of <code>1</code>&#39;s (representing soldiers) and <code>0</code>&#39;s (representing civilians). The soldiers are positioned <strong>in front</strong> of the civilians. That is, all the <code>1</code>&#39;s will appear to the <strong>left</strong> of all the <code>0</code>&#39;s in each row.</p> <p>A row <code>i</code> is <strong>weaker</strong> than a row <code>j</code> if one of the following is true:</p> <ul> <li>The number of soldiers in row <code>i</code> is less than the number of soldiers in row <code>j</code>.</li> <li>Both rows have the same number of soldiers and <code>i &lt; j</code>.</li> </ul> <p>Return <em>the indices of the </em><code>k</code><em> <strong>weakest</strong> rows in the matrix ordered from weakest to strongest</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> mat = [[1,1,0,0,0], [1,1,1,1,0], [1,0,0,0,0], [1,1,0,0,0], [1,1,1,1,1]], k = 3 <strong>Output:</strong> [2,0,3] <strong>Explanation:</strong> The number of soldiers in each row is: - Row 0: 2 - Row 1: 4 - Row 2: 1 - Row 3: 2 - Row 4: 5 The rows ordered from weakest to strongest are [2,0,3,1,4]. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> mat = [[1,0,0,0], [1,1,1,1], [1,0,0,0], [1,0,0,0]], k = 2 <strong>Output:</strong> [0,2] <strong>Explanation:</strong> The number of soldiers in each row is: - Row 0: 1 - Row 1: 4 - Row 2: 1 - Row 3: 1 The rows ordered from weakest to strongest are [0,2,3,1]. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == mat.length</code></li> <li><code>n == mat[i].length</code></li> <li><code>2 &lt;= n, m &lt;= 100</code></li> <li><code>1 &lt;= k &lt;= m</code></li> <li><code>matrix[i][j]</code> is either 0 or 1.</li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<int> kWeakestRows(vector<vector<int>>& mat, int k) {\n map<int,vector<int>> m;\n int ct=0;\n for(int i=0;i<mat.size();i++)\n {\n int ct=0;\n for(int j=0;j<mat[i].size();j++)\n {\n if(mat[i][j]==1) ct++;\n else break;\n }\n m[ct].push_back(i);\n }\n vector<int> v;\n for(auto &it: m)\n {\n if(ct==k) break;\n for(auto &h: it.second)\n {\n v.push_back(h);\n ct++;\n if(ct==k) break;\n }\n }\n return v;\n }\n};", "memory": "13500" }
1,463
<p>You are given an <code>m x n</code> binary matrix <code>mat</code> of <code>1</code>&#39;s (representing soldiers) and <code>0</code>&#39;s (representing civilians). The soldiers are positioned <strong>in front</strong> of the civilians. That is, all the <code>1</code>&#39;s will appear to the <strong>left</strong> of all the <code>0</code>&#39;s in each row.</p> <p>A row <code>i</code> is <strong>weaker</strong> than a row <code>j</code> if one of the following is true:</p> <ul> <li>The number of soldiers in row <code>i</code> is less than the number of soldiers in row <code>j</code>.</li> <li>Both rows have the same number of soldiers and <code>i &lt; j</code>.</li> </ul> <p>Return <em>the indices of the </em><code>k</code><em> <strong>weakest</strong> rows in the matrix ordered from weakest to strongest</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> mat = [[1,1,0,0,0], [1,1,1,1,0], [1,0,0,0,0], [1,1,0,0,0], [1,1,1,1,1]], k = 3 <strong>Output:</strong> [2,0,3] <strong>Explanation:</strong> The number of soldiers in each row is: - Row 0: 2 - Row 1: 4 - Row 2: 1 - Row 3: 2 - Row 4: 5 The rows ordered from weakest to strongest are [2,0,3,1,4]. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> mat = [[1,0,0,0], [1,1,1,1], [1,0,0,0], [1,0,0,0]], k = 2 <strong>Output:</strong> [0,2] <strong>Explanation:</strong> The number of soldiers in each row is: - Row 0: 1 - Row 1: 4 - Row 2: 1 - Row 3: 1 The rows ordered from weakest to strongest are [0,2,3,1]. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == mat.length</code></li> <li><code>n == mat[i].length</code></li> <li><code>2 &lt;= n, m &lt;= 100</code></li> <li><code>1 &lt;= k &lt;= m</code></li> <li><code>matrix[i][j]</code> is either 0 or 1.</li> </ul>
3
{ "code": "class Solution {\npublic:\nbool cmp(pair<int, int>& a, pair<int, int>& b) {\n if(a.second==b.second)\n {\n return a.first < b.first;\n }\n return a.second < b.second;\n }\n\n //sort by value\n vector<pair<int, int>> sort(map<int, int>& M) {\n // Declare vector of pairs\n vector<pair<int, int>> A;\n\n // Copy key-value pair from Map to vector of pairs\n for (auto& it : M) {\n A.push_back(it);\n }\n\n // Sort using a lambda that calls cmp\n std::sort(A.begin(), A.end(), [this](pair<int, int>& a, pair<int, int>& b) {\n return cmp(a, b);\n });\n return A;\n }\n \n \n vector<int> kWeakestRows(vector<vector<int>>& mat, int k) {\n\n \n\n map<int,int>m;\n for(int i=0;i<mat.size();i++){\n int x=0;\n for(int j=0;j<mat[i].size();j++){\n if(mat[i][j]==1)\n {\n x++;\n }\n \n }\n m[i]=x;\n }\n\n vector<pair<int,int>> v = sort(m);\n \n vector<int>i;\n int c=0;\n for (auto& it : v) { \n if(c>=k)\n {\n break;\n }\n i.push_back(it.first);\n c++;\n \n } \n\n\n\n return i;\n }\n};", "memory": "13500" }
1,463
<p>You are given an <code>m x n</code> binary matrix <code>mat</code> of <code>1</code>&#39;s (representing soldiers) and <code>0</code>&#39;s (representing civilians). The soldiers are positioned <strong>in front</strong> of the civilians. That is, all the <code>1</code>&#39;s will appear to the <strong>left</strong> of all the <code>0</code>&#39;s in each row.</p> <p>A row <code>i</code> is <strong>weaker</strong> than a row <code>j</code> if one of the following is true:</p> <ul> <li>The number of soldiers in row <code>i</code> is less than the number of soldiers in row <code>j</code>.</li> <li>Both rows have the same number of soldiers and <code>i &lt; j</code>.</li> </ul> <p>Return <em>the indices of the </em><code>k</code><em> <strong>weakest</strong> rows in the matrix ordered from weakest to strongest</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> mat = [[1,1,0,0,0], [1,1,1,1,0], [1,0,0,0,0], [1,1,0,0,0], [1,1,1,1,1]], k = 3 <strong>Output:</strong> [2,0,3] <strong>Explanation:</strong> The number of soldiers in each row is: - Row 0: 2 - Row 1: 4 - Row 2: 1 - Row 3: 2 - Row 4: 5 The rows ordered from weakest to strongest are [2,0,3,1,4]. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> mat = [[1,0,0,0], [1,1,1,1], [1,0,0,0], [1,0,0,0]], k = 2 <strong>Output:</strong> [0,2] <strong>Explanation:</strong> The number of soldiers in each row is: - Row 0: 1 - Row 1: 4 - Row 2: 1 - Row 3: 1 The rows ordered from weakest to strongest are [0,2,3,1]. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == mat.length</code></li> <li><code>n == mat[i].length</code></li> <li><code>2 &lt;= n, m &lt;= 100</code></li> <li><code>1 &lt;= k &lt;= m</code></li> <li><code>matrix[i][j]</code> is either 0 or 1.</li> </ul>
3
{ "code": "class Solution {\npublic:\n \n int binary(vector<int> m)\n {\n int l = 0;\n int h = m.size()-1;\n while(l <= h)\n {\n int mid = l + (h-l)/2;\n if(m[mid] == 0)\n h = mid - 1;\n else\n l = mid + 1;\n }\n return l;\n }\n \n vector<int> kWeakestRows(vector<vector<int>>& mat, int k) {\n \n priority_queue<pair<int,int> > pq;\n vector<int> ans;\n for(int i = 0; i<mat.size(); ++i)\n {\n int cnt = binary(mat[i]);\n\t\t\t\n\t\t\t\n pq.push(make_pair(cnt,i));\n \n if(pq.size() > k)\n pq.pop();\n }\n \n for(int i = 0; i<k; ++i)\n {\n ans.push_back(pq.top().second);\n pq.pop();\n }\n \n reverse(ans.begin(),ans.end());\n return ans;\n }\n};", "memory": "13600" }
1,463
<p>You are given an <code>m x n</code> binary matrix <code>mat</code> of <code>1</code>&#39;s (representing soldiers) and <code>0</code>&#39;s (representing civilians). The soldiers are positioned <strong>in front</strong> of the civilians. That is, all the <code>1</code>&#39;s will appear to the <strong>left</strong> of all the <code>0</code>&#39;s in each row.</p> <p>A row <code>i</code> is <strong>weaker</strong> than a row <code>j</code> if one of the following is true:</p> <ul> <li>The number of soldiers in row <code>i</code> is less than the number of soldiers in row <code>j</code>.</li> <li>Both rows have the same number of soldiers and <code>i &lt; j</code>.</li> </ul> <p>Return <em>the indices of the </em><code>k</code><em> <strong>weakest</strong> rows in the matrix ordered from weakest to strongest</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> mat = [[1,1,0,0,0], [1,1,1,1,0], [1,0,0,0,0], [1,1,0,0,0], [1,1,1,1,1]], k = 3 <strong>Output:</strong> [2,0,3] <strong>Explanation:</strong> The number of soldiers in each row is: - Row 0: 2 - Row 1: 4 - Row 2: 1 - Row 3: 2 - Row 4: 5 The rows ordered from weakest to strongest are [2,0,3,1,4]. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> mat = [[1,0,0,0], [1,1,1,1], [1,0,0,0], [1,0,0,0]], k = 2 <strong>Output:</strong> [0,2] <strong>Explanation:</strong> The number of soldiers in each row is: - Row 0: 1 - Row 1: 4 - Row 2: 1 - Row 3: 1 The rows ordered from weakest to strongest are [0,2,3,1]. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == mat.length</code></li> <li><code>n == mat[i].length</code></li> <li><code>2 &lt;= n, m &lt;= 100</code></li> <li><code>1 &lt;= k &lt;= m</code></li> <li><code>matrix[i][j]</code> is either 0 or 1.</li> </ul>
3
{ "code": "class Solution {\npublic:\nstatic bool comp(pair<int,int>a,pair<int,int>b){\n return a.first==b.first?a.second<b.second:a.first<b.first;\n}\nstatic int bs(vector<int>arr){\n int low=0;\n int high=arr.size()-1;\n int ans=-1;\n while(low<=high){\n int mid=(low+high)/2;\n if (arr[mid] == 1) {\n low = mid + 1; \n } else {\n high = mid - 1; \n }\n }\n return low;\n\n}\n vector<int> kWeakestRows(vector<vector<int>>& mat, int k) {\n vector<pair<int,int>>v;\n int n,m;\n n=mat.size();\n m=mat[0].size();\n for(int i=0;i<n;i++){\n int l=bs(mat[i]);\n v.push_back({l,i}); \n }\n vector<int>ans;\n\n sort(v.begin(),v.end(),comp);\n for (int i = 0; i < k; i++) {\n ans.push_back(v[i].second);\n }\n \n return ans;\n \n }\n};", "memory": "13700" }
1,463
<p>You are given an <code>m x n</code> binary matrix <code>mat</code> of <code>1</code>&#39;s (representing soldiers) and <code>0</code>&#39;s (representing civilians). The soldiers are positioned <strong>in front</strong> of the civilians. That is, all the <code>1</code>&#39;s will appear to the <strong>left</strong> of all the <code>0</code>&#39;s in each row.</p> <p>A row <code>i</code> is <strong>weaker</strong> than a row <code>j</code> if one of the following is true:</p> <ul> <li>The number of soldiers in row <code>i</code> is less than the number of soldiers in row <code>j</code>.</li> <li>Both rows have the same number of soldiers and <code>i &lt; j</code>.</li> </ul> <p>Return <em>the indices of the </em><code>k</code><em> <strong>weakest</strong> rows in the matrix ordered from weakest to strongest</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> mat = [[1,1,0,0,0], [1,1,1,1,0], [1,0,0,0,0], [1,1,0,0,0], [1,1,1,1,1]], k = 3 <strong>Output:</strong> [2,0,3] <strong>Explanation:</strong> The number of soldiers in each row is: - Row 0: 2 - Row 1: 4 - Row 2: 1 - Row 3: 2 - Row 4: 5 The rows ordered from weakest to strongest are [2,0,3,1,4]. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> mat = [[1,0,0,0], [1,1,1,1], [1,0,0,0], [1,0,0,0]], k = 2 <strong>Output:</strong> [0,2] <strong>Explanation:</strong> The number of soldiers in each row is: - Row 0: 1 - Row 1: 4 - Row 2: 1 - Row 3: 1 The rows ordered from weakest to strongest are [0,2,3,1]. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == mat.length</code></li> <li><code>n == mat[i].length</code></li> <li><code>2 &lt;= n, m &lt;= 100</code></li> <li><code>1 &lt;= k &lt;= m</code></li> <li><code>matrix[i][j]</code> is either 0 or 1.</li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<int> kWeakestRows(vector<vector<int>>& mat, int k) {\n int n = mat.size();\n int m = mat[0].size();\n \n vector<pair<int,int>> soldiers(n);\n \n int i = 0;\n for(auto it : mat){\n for(int j = 0; j<m; j++){\n if(it[j] == 0){\n soldiers[i] = {j,i};\n break;\n }\n else if(j == m-1)\n soldiers[i] = {m,i};\n }\n i++;\n }\n \n sort(soldiers.begin(), soldiers.end());\n \n vector<int> ans(k);\n for(int i = 0; i<k; i++){\n ans[i] = soldiers[i].second;\n }\n \n return ans;\n }\n};", "memory": "13700" }
1,464
<p>You are given an integer array <code>arr</code>. You can choose a set of integers and remove all the occurrences of these integers in the array.</p> <p>Return <em>the minimum size of the set so that <strong>at least</strong> half of the integers of the array are removed</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> arr = [3,3,3,3,5,5,5,2,2,7] <strong>Output:</strong> 2 <strong>Explanation:</strong> 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 a size greater than half of the size of the old array. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> arr = [7,7,7,7,7,7] <strong>Output:</strong> 1 <strong>Explanation:</strong> The only possible set you can choose is {7}. This will make the new array empty. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= arr.length &lt;= 10<sup>5</sup></code></li> <li><code>arr.length</code> is even.</li> <li><code>1 &lt;= arr[i] &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n int minSetSize(vector<int>& arr) {\n int mx = *max_element(arr.begin(), arr.end());\n int cnt[mx + 1];\n memset(cnt, 0, sizeof(cnt));\n for (int& x : arr) {\n ++cnt[x];\n }\n sort(cnt, cnt + mx + 1, greater<int>());\n int ans = 0;\n int m = 0;\n for (int& x : cnt) {\n if (x) {\n m += x;\n ++ans;\n if (m * 2 >= arr.size()) {\n break;\n }\n }\n }\n return ans;\n }\n};//rtsfherhethdf65465", "memory": "59293" }
1,464
<p>You are given an integer array <code>arr</code>. You can choose a set of integers and remove all the occurrences of these integers in the array.</p> <p>Return <em>the minimum size of the set so that <strong>at least</strong> half of the integers of the array are removed</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> arr = [3,3,3,3,5,5,5,2,2,7] <strong>Output:</strong> 2 <strong>Explanation:</strong> 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 a size greater than half of the size of the old array. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> arr = [7,7,7,7,7,7] <strong>Output:</strong> 1 <strong>Explanation:</strong> The only possible set you can choose is {7}. This will make the new array empty. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= arr.length &lt;= 10<sup>5</sup></code></li> <li><code>arr.length</code> is even.</li> <li><code>1 &lt;= arr[i] &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n int minSetSize(vector<int>& arr) {\n int mx = *max_element(arr.begin(), arr.end());\n int cnt[mx + 1];\n memset(cnt, 0, sizeof(cnt));\n for (int& x : arr) {\n ++cnt[x];\n }\n sort(cnt, cnt + mx + 1, greater<int>());\n int ans = 0;\n int m = 0;\n for (int& x : cnt) {\n if (x) {\n m += x;\n ++ans;\n if (m * 2 >= arr.size()) {\n break;\n }\n }\n }\n return ans;\n }\n};//rtsfherhethdf65465greuyg", "memory": "59293" }
1,464
<p>You are given an integer array <code>arr</code>. You can choose a set of integers and remove all the occurrences of these integers in the array.</p> <p>Return <em>the minimum size of the set so that <strong>at least</strong> half of the integers of the array are removed</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> arr = [3,3,3,3,5,5,5,2,2,7] <strong>Output:</strong> 2 <strong>Explanation:</strong> 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 a size greater than half of the size of the old array. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> arr = [7,7,7,7,7,7] <strong>Output:</strong> 1 <strong>Explanation:</strong> The only possible set you can choose is {7}. This will make the new array empty. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= arr.length &lt;= 10<sup>5</sup></code></li> <li><code>arr.length</code> is even.</li> <li><code>1 &lt;= arr[i] &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n int minSetSize(vector<int>& arr) {\n int n = arr.size();\n int res = 0;\n sort(arr.begin(), arr.end());\n vector<int> buf = vector<int>();\n int count = 1;\n for (int i = 0; i < n - 1; i++) {\n if (arr[i + 1] == arr[i]) {\n count++;\n }\n else {\n buf.push_back(count);\n count = 1;\n }\n }\n buf.push_back(count);\n sort(buf.begin(), buf.end());\n int sum = n / 2;\n int i = buf.size() - 1;\n while (sum > 0) {\n sum -= buf[i];\n i--;\n res++;\n }\n return res;\n }\n};", "memory": "59681" }
1,464
<p>You are given an integer array <code>arr</code>. You can choose a set of integers and remove all the occurrences of these integers in the array.</p> <p>Return <em>the minimum size of the set so that <strong>at least</strong> half of the integers of the array are removed</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> arr = [3,3,3,3,5,5,5,2,2,7] <strong>Output:</strong> 2 <strong>Explanation:</strong> 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 a size greater than half of the size of the old array. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> arr = [7,7,7,7,7,7] <strong>Output:</strong> 1 <strong>Explanation:</strong> The only possible set you can choose is {7}. This will make the new array empty. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= arr.length &lt;= 10<sup>5</sup></code></li> <li><code>arr.length</code> is even.</li> <li><code>1 &lt;= arr[i] &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n int minSetSize(vector<int>& arr) {\n int n = arr.size();\n sort(arr.begin(), arr.end());\n int ct = 1;\n priority_queue<int> pq;\n for (int i = 1; i < n; i++) {\n if (arr[i] != arr[i-1]) {\n pq.push(ct);\n ct = 1;\n } else {\n ct++;\n if (i == n-1) pq.push(ct);\n }\n }\n int sum = 0;\n int ret = 0;\n while (sum < n/2) {\n sum += pq.top();\n pq.pop();\n ret++;\n }\n return ret;\n }\n};", "memory": "60068" }
1,464
<p>You are given an integer array <code>arr</code>. You can choose a set of integers and remove all the occurrences of these integers in the array.</p> <p>Return <em>the minimum size of the set so that <strong>at least</strong> half of the integers of the array are removed</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> arr = [3,3,3,3,5,5,5,2,2,7] <strong>Output:</strong> 2 <strong>Explanation:</strong> 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 a size greater than half of the size of the old array. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> arr = [7,7,7,7,7,7] <strong>Output:</strong> 1 <strong>Explanation:</strong> The only possible set you can choose is {7}. This will make the new array empty. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= arr.length &lt;= 10<sup>5</sup></code></li> <li><code>arr.length</code> is even.</li> <li><code>1 &lt;= arr[i] &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n int minSetSize(vector<int>& arr) {\n array<int, 100001> freq{0};\n auto [mn_it, mx_it] = minmax_element(arr.begin(), arr.end());\n for (auto num : arr) {\n ++freq[num];\n }\n vector<int> amount;\n for (int num = *mn_it; num <= *mx_it; ++num) {\n if (freq[num] > 0) {\n amount.push_back(freq[num]);\n }\n }\n sort(amount.begin(), amount.end(), greater<int>());\n int sum{0}, ans{0}, half = (arr.size() + 1) / 2;\n for (int count : amount) {\n sum += count;\n ++ans;\n if (sum >= half) {\n return ans;\n }\n }\n return ans;\n }\n};", "memory": "60456" }
1,464
<p>You are given an integer array <code>arr</code>. You can choose a set of integers and remove all the occurrences of these integers in the array.</p> <p>Return <em>the minimum size of the set so that <strong>at least</strong> half of the integers of the array are removed</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> arr = [3,3,3,3,5,5,5,2,2,7] <strong>Output:</strong> 2 <strong>Explanation:</strong> 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 a size greater than half of the size of the old array. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> arr = [7,7,7,7,7,7] <strong>Output:</strong> 1 <strong>Explanation:</strong> The only possible set you can choose is {7}. This will make the new array empty. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= arr.length &lt;= 10<sup>5</sup></code></li> <li><code>arr.length</code> is even.</li> <li><code>1 &lt;= arr[i] &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n int minSetSize(vector<int>& arr) {\n static constexpr const size_t theSize{100'001};\n const auto n=int(size(arr));\n if(n<3)return 1;\n using tuple_t=typename std::tuple<int,int>;\n #define g0 get<0>\n #define g1 get<1>\n #define mt make_tuple\n int a[theSize]={0};\n for(int i: arr)++a[i];\n vector<int> fr{};\n for(int i: a){\n if(i)\n fr.push_back(i);\n }\n sort(begin(fr),end(fr),greater<int>());\n int taken=0;\n int i=0;\n while(taken<n/2){\n taken+=fr[i++];\n }\n return i;\n }\n};", "memory": "60456" }
1,464
<p>You are given an integer array <code>arr</code>. You can choose a set of integers and remove all the occurrences of these integers in the array.</p> <p>Return <em>the minimum size of the set so that <strong>at least</strong> half of the integers of the array are removed</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> arr = [3,3,3,3,5,5,5,2,2,7] <strong>Output:</strong> 2 <strong>Explanation:</strong> 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 a size greater than half of the size of the old array. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> arr = [7,7,7,7,7,7] <strong>Output:</strong> 1 <strong>Explanation:</strong> The only possible set you can choose is {7}. This will make the new array empty. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= arr.length &lt;= 10<sup>5</sup></code></li> <li><code>arr.length</code> is even.</li> <li><code>1 &lt;= arr[i] &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n int minSetSize(vector<int>& arr) {\n array<int, 100001> freq{0};\n auto [mn_it, mx_it] = minmax_element(arr.begin(), arr.end());\n for (auto num : arr) {\n ++freq[num];\n }\n vector<int> amount;\n for (int num = *mn_it; num <= *mx_it; ++num) {\n if (freq[num] > 0) {\n amount.push_back(freq[num]);\n }\n }\n sort(amount.begin(), amount.end(), greater<int>());\n int sum{0}, ans{0}, half = (arr.size() + 1) / 2;\n for (int count : amount) {\n sum += count;\n ++ans;\n if (sum >= half) {\n return ans;\n }\n }\n return ans;\n }\n};", "memory": "60843" }
1,464
<p>You are given an integer array <code>arr</code>. You can choose a set of integers and remove all the occurrences of these integers in the array.</p> <p>Return <em>the minimum size of the set so that <strong>at least</strong> half of the integers of the array are removed</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> arr = [3,3,3,3,5,5,5,2,2,7] <strong>Output:</strong> 2 <strong>Explanation:</strong> 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 a size greater than half of the size of the old array. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> arr = [7,7,7,7,7,7] <strong>Output:</strong> 1 <strong>Explanation:</strong> The only possible set you can choose is {7}. This will make the new array empty. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= arr.length &lt;= 10<sup>5</sup></code></li> <li><code>arr.length</code> is even.</li> <li><code>1 &lt;= arr[i] &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n int minSetSize(vector<int>& arr) {\n int n=arr.size();\n int f[100000+1]={0};\n for(int&i:arr){\n f[i]++;\n }\n priority_queue<int> pq; \n for(int& i:f){\n if(i!=0)pq.push(i);\n }\n int res=0;\n int s=0;\n while(pq.size()>0){\n res++;\n int t=pq.top();\n pq.pop();\n s+=t;\n if(s>=n/2)return res;\n }\n return res;\n }\n};", "memory": "60843" }
1,464
<p>You are given an integer array <code>arr</code>. You can choose a set of integers and remove all the occurrences of these integers in the array.</p> <p>Return <em>the minimum size of the set so that <strong>at least</strong> half of the integers of the array are removed</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> arr = [3,3,3,3,5,5,5,2,2,7] <strong>Output:</strong> 2 <strong>Explanation:</strong> 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 a size greater than half of the size of the old array. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> arr = [7,7,7,7,7,7] <strong>Output:</strong> 1 <strong>Explanation:</strong> The only possible set you can choose is {7}. This will make the new array empty. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= arr.length &lt;= 10<sup>5</sup></code></li> <li><code>arr.length</code> is even.</li> <li><code>1 &lt;= arr[i] &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n int minSetSize(vector<int>& arr) {\n int n_half = arr.size()/2;\n int min_value = arr[0], max_value = arr[0];\n for (auto const& val: arr) {\n min_value = min(min_value, val);\n max_value = max(max_value, val);\n }\n\n vector<int> counts(max_value-min_value+1);\n \n for (auto const& val: arr) {\n ++counts[val-min_value];\n }\n\n sort(counts.begin(), counts.end(), greater<int>());\n\n int answer = 0;\n int total = 0;\n for (auto const& count : counts) {\n total += count;\n ++answer;\n if (total >= n_half) break;\n }\n return answer;\n }\n};", "memory": "61231" }
1,464
<p>You are given an integer array <code>arr</code>. You can choose a set of integers and remove all the occurrences of these integers in the array.</p> <p>Return <em>the minimum size of the set so that <strong>at least</strong> half of the integers of the array are removed</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> arr = [3,3,3,3,5,5,5,2,2,7] <strong>Output:</strong> 2 <strong>Explanation:</strong> 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 a size greater than half of the size of the old array. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> arr = [7,7,7,7,7,7] <strong>Output:</strong> 1 <strong>Explanation:</strong> The only possible set you can choose is {7}. This will make the new array empty. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= arr.length &lt;= 10<sup>5</sup></code></li> <li><code>arr.length</code> is even.</li> <li><code>1 &lt;= arr[i] &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n int minSetSize(vector<int>& arr) {\n int n=arr.size();\n int maxi=0;\n for(int i=0;i<n;i++){\n if(arr[i]>maxi){\n maxi=arr[i];\n }\n }\n vector<int>frequency(maxi+1,0);\n for(int i=0;i<n;i++){\n frequency[arr[i]]=frequency[arr[i]]+1;\n }\n sort(frequency.begin(),frequency.end());\n int m=frequency.size();\n int remove=n/2;\n int ans=0;\n for(int i=m-1;i>=0;i--){\n if(remove>0){\n remove=remove-frequency[i];\n ans=ans+1;\n }\n else{\n break;\n }\n }\n return ans;\n }\n};", "memory": "61618" }
1,464
<p>You are given an integer array <code>arr</code>. You can choose a set of integers and remove all the occurrences of these integers in the array.</p> <p>Return <em>the minimum size of the set so that <strong>at least</strong> half of the integers of the array are removed</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> arr = [3,3,3,3,5,5,5,2,2,7] <strong>Output:</strong> 2 <strong>Explanation:</strong> 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 a size greater than half of the size of the old array. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> arr = [7,7,7,7,7,7] <strong>Output:</strong> 1 <strong>Explanation:</strong> The only possible set you can choose is {7}. This will make the new array empty. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= arr.length &lt;= 10<sup>5</sup></code></li> <li><code>arr.length</code> is even.</li> <li><code>1 &lt;= arr[i] &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n int minSetSize(vector<int>& arr) {\n int n = arr.size();\n\n int M = 0;\n\n for (int i = 0; i < arr.size(); i++)\n M = max(M, arr[i]);\n\n vector<int> cnt(M + 1, 0);\n\n for (int i = 0; i < arr.size(); i++)\n cnt[arr[i]]++;\n\n sort(cnt.begin(),cnt.end(), greater<int>());\n int res = 0;\n int removed = 0;\n for(int i = 0; i < cnt.size(); i++){\n if(removed < n/2){\n res += 1; \n removed += cnt[i];\n }\n }\n return res;\n\n\n }\n};", "memory": "62006" }
1,464
<p>You are given an integer array <code>arr</code>. You can choose a set of integers and remove all the occurrences of these integers in the array.</p> <p>Return <em>the minimum size of the set so that <strong>at least</strong> half of the integers of the array are removed</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> arr = [3,3,3,3,5,5,5,2,2,7] <strong>Output:</strong> 2 <strong>Explanation:</strong> 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 a size greater than half of the size of the old array. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> arr = [7,7,7,7,7,7] <strong>Output:</strong> 1 <strong>Explanation:</strong> The only possible set you can choose is {7}. This will make the new array empty. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= arr.length &lt;= 10<sup>5</sup></code></li> <li><code>arr.length</code> is even.</li> <li><code>1 &lt;= arr[i] &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n int minSetSize(vector<int>& arr) {\n int ans = 0, rc = 0;\n int maxval = *max_element(arr.begin(), arr.end());\n vector<int> freq(maxval + 1);\n for (int a: arr)\n ++freq[a];\n sort(freq.begin(), freq.end(), greater<int>());\n\n for (int f: freq) {\n if (f > 0) {\n rc += f;\n ++ans;\n if (rc * 2 >= arr.size())\n break;\n }\n }\n\n return ans;\n }\n};", "memory": "62393" }
1,464
<p>You are given an integer array <code>arr</code>. You can choose a set of integers and remove all the occurrences of these integers in the array.</p> <p>Return <em>the minimum size of the set so that <strong>at least</strong> half of the integers of the array are removed</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> arr = [3,3,3,3,5,5,5,2,2,7] <strong>Output:</strong> 2 <strong>Explanation:</strong> 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 a size greater than half of the size of the old array. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> arr = [7,7,7,7,7,7] <strong>Output:</strong> 1 <strong>Explanation:</strong> The only possible set you can choose is {7}. This will make the new array empty. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= arr.length &lt;= 10<sup>5</sup></code></li> <li><code>arr.length</code> is even.</li> <li><code>1 &lt;= arr[i] &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n int minSetSize(vector<int>& arr) {\n int maxElem = *max_element(arr.begin(), arr.end());\n\n vector<int>count(maxElem);\n\n for (int n : arr) {\n count[n-1]++;\n }\n\n sort(count.begin(), count.end(), greater<int>());\n int check = 0;\n\n int res = 0;\n for (int n : count) {\n ++res;\n check += n;\n if (check >= arr.size()/2) return res;\n }\n return -1;\n }\n};", "memory": "62781" }
1,464
<p>You are given an integer array <code>arr</code>. You can choose a set of integers and remove all the occurrences of these integers in the array.</p> <p>Return <em>the minimum size of the set so that <strong>at least</strong> half of the integers of the array are removed</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> arr = [3,3,3,3,5,5,5,2,2,7] <strong>Output:</strong> 2 <strong>Explanation:</strong> 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 a size greater than half of the size of the old array. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> arr = [7,7,7,7,7,7] <strong>Output:</strong> 1 <strong>Explanation:</strong> The only possible set you can choose is {7}. This will make the new array empty. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= arr.length &lt;= 10<sup>5</sup></code></li> <li><code>arr.length</code> is even.</li> <li><code>1 &lt;= arr[i] &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n int minSetSize(vector<int>& arr) {\n sort(arr.begin(),arr.end());\n priority_queue<pair<int,int>>pq;\n int c=0,n_size=arr.size();\n int i=0,j=0;\n while(i<n_size){\n c=0,j=i;\n while(j<n_size && arr[i]==arr[j]){\n j++;\n c++;\n }\n pq.push({c,arr[i]});\n i=j;\n }\n int ans=0,a=0;\n while(!pq.empty()){\n auto it=pq.top();\n pq.pop();\n ans+=it.first;\n a++;\n if(ans>=(n_size/2))return a;\n }\n return 1;\n }\n};", "memory": "63168" }
1,464
<p>You are given an integer array <code>arr</code>. You can choose a set of integers and remove all the occurrences of these integers in the array.</p> <p>Return <em>the minimum size of the set so that <strong>at least</strong> half of the integers of the array are removed</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> arr = [3,3,3,3,5,5,5,2,2,7] <strong>Output:</strong> 2 <strong>Explanation:</strong> 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 a size greater than half of the size of the old array. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> arr = [7,7,7,7,7,7] <strong>Output:</strong> 1 <strong>Explanation:</strong> The only possible set you can choose is {7}. This will make the new array empty. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= arr.length &lt;= 10<sup>5</sup></code></li> <li><code>arr.length</code> is even.</li> <li><code>1 &lt;= arr[i] &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n static bool comp(pair<int,int> &a, pair<int,int> &b){\n return a.second>b.second;\n }\n\n int minSetSize(vector<int>& arr) {\n int size=arr.size();\n vector<pair<int,int>> ans;\n sort(arr.begin(),arr.end());\n int count=0;\n \n int element=0;\n for(int j=0;j<size;j++){\n if(arr[element]==arr[j]){\n count++;\n }\n else{\n ans.push_back({arr[element],count});\n element=j;\n count=1;\n }\n }\n ans.push_back({arr[element],count});\n\n int compa = size/2;\n \n \n sort(ans.begin(),ans.end(),comp);\n int count1=0;\n for(auto it : ans){\n // cout<<it.first<<' '<<it.second<<endl;\n int dec = it.second;\n if(size>compa){\n count1++;\n size-=dec;\n }\n }\n\n return count1;\n }\n};", "memory": "63556" }
1,464
<p>You are given an integer array <code>arr</code>. You can choose a set of integers and remove all the occurrences of these integers in the array.</p> <p>Return <em>the minimum size of the set so that <strong>at least</strong> half of the integers of the array are removed</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> arr = [3,3,3,3,5,5,5,2,2,7] <strong>Output:</strong> 2 <strong>Explanation:</strong> 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 a size greater than half of the size of the old array. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> arr = [7,7,7,7,7,7] <strong>Output:</strong> 1 <strong>Explanation:</strong> The only possible set you can choose is {7}. This will make the new array empty. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= arr.length &lt;= 10<sup>5</sup></code></li> <li><code>arr.length</code> is even.</li> <li><code>1 &lt;= arr[i] &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n int minSetSize(vector<int>& arr) {\n sort(arr.begin(), arr.end());\n vector<int> counts;\n\n int currentRuns = 1;\n\n for (int i = 1; i < arr.size(); i++) {\n if (arr[i] == arr[i - 1]) {\n currentRuns += 1;\n continue;\n }\n counts.push_back(currentRuns);\n currentRuns = 1;\n }\n counts.push_back(currentRuns);\n\n sort(counts.rbegin(), counts.rend());\n\n int numRemovedFromArr = 0;\n int setSize = 0;\n\n for (auto count : counts) {\n numRemovedFromArr += count;\n setSize += 1;\n if (numRemovedFromArr >= arr.size() / 2) break;\n }\n \n return setSize;\n\n\n }\n};", "memory": "63943" }
1,464
<p>You are given an integer array <code>arr</code>. You can choose a set of integers and remove all the occurrences of these integers in the array.</p> <p>Return <em>the minimum size of the set so that <strong>at least</strong> half of the integers of the array are removed</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> arr = [3,3,3,3,5,5,5,2,2,7] <strong>Output:</strong> 2 <strong>Explanation:</strong> 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 a size greater than half of the size of the old array. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> arr = [7,7,7,7,7,7] <strong>Output:</strong> 1 <strong>Explanation:</strong> The only possible set you can choose is {7}. This will make the new array empty. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= arr.length &lt;= 10<sup>5</sup></code></li> <li><code>arr.length</code> is even.</li> <li><code>1 &lt;= arr[i] &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n int minSetSize(vector<int>& arr) {\n sort(arr.begin(), arr.end());\n vector<int> counts;\n\n int currentRuns = 1;\n\n for (int i = 1; i < arr.size(); i++) {\n if (arr[i] == arr[i - 1]) {\n currentRuns += 1;\n continue;\n }\n counts.push_back(currentRuns);\n currentRuns = 1;\n }\n counts.push_back(currentRuns);\n\n sort(counts.rbegin(), counts.rend());\n\n int numRemovedFromArr = 0;\n int setSize = 0;\n\n for (auto count : counts) {\n numRemovedFromArr += count;\n setSize += 1;\n if (numRemovedFromArr >= arr.size() / 2) break;\n }\n \n return setSize;\n\n\n }\n};", "memory": "63943" }
1,464
<p>You are given an integer array <code>arr</code>. You can choose a set of integers and remove all the occurrences of these integers in the array.</p> <p>Return <em>the minimum size of the set so that <strong>at least</strong> half of the integers of the array are removed</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> arr = [3,3,3,3,5,5,5,2,2,7] <strong>Output:</strong> 2 <strong>Explanation:</strong> 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 a size greater than half of the size of the old array. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> arr = [7,7,7,7,7,7] <strong>Output:</strong> 1 <strong>Explanation:</strong> The only possible set you can choose is {7}. This will make the new array empty. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= arr.length &lt;= 10<sup>5</sup></code></li> <li><code>arr.length</code> is even.</li> <li><code>1 &lt;= arr[i] &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n int minSetSize(vector<int>& arr) {\n std::sort(arr.begin(), arr.end());\n\n std::vector<int> counts;\n int count = 1;\n for (size_t i = 1; i < arr.size(); i++) {\n if (arr[i - 1] == arr[i]) {\n count++;\n } else {\n counts.push_back(count);\n count = 1;\n }\n }\n counts.push_back(count);\n\n std::sort(counts.rbegin(), counts.rend());\n int current_size = arr.size(), i = 0; \n for (; i < counts.size() && current_size > arr.size() / 2; i++) {\n current_size -= counts[i];\n }\n\n return i;\n }\n};", "memory": "64331" }
1,464
<p>You are given an integer array <code>arr</code>. You can choose a set of integers and remove all the occurrences of these integers in the array.</p> <p>Return <em>the minimum size of the set so that <strong>at least</strong> half of the integers of the array are removed</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> arr = [3,3,3,3,5,5,5,2,2,7] <strong>Output:</strong> 2 <strong>Explanation:</strong> 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 a size greater than half of the size of the old array. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> arr = [7,7,7,7,7,7] <strong>Output:</strong> 1 <strong>Explanation:</strong> The only possible set you can choose is {7}. This will make the new array empty. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= arr.length &lt;= 10<sup>5</sup></code></li> <li><code>arr.length</code> is even.</li> <li><code>1 &lt;= arr[i] &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n int minSetSize(vector<int>& arr) {\n int n = arr.size();\n int ans = 0;\n int mx = arr[0];\n\n for(int i=1;i<arr.size();i++){\n mx = max(mx,arr[i]);\n }\n\n vector<int> f(mx+1);\n\n for(int i=0;i<arr.size();i++){\n f[arr[i]]++;\n }\n\n sort(f.rbegin(),f.rend());\n\n int sum = 0;\n\n for(int i=0;i<f.size();i++){\n sum = sum + f[i];\n if(sum >= n/2){\n ans = (i+1);\n break;\n }\n }\n\n return ans;\n }\n};", "memory": "65493" }
1,464
<p>You are given an integer array <code>arr</code>. You can choose a set of integers and remove all the occurrences of these integers in the array.</p> <p>Return <em>the minimum size of the set so that <strong>at least</strong> half of the integers of the array are removed</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> arr = [3,3,3,3,5,5,5,2,2,7] <strong>Output:</strong> 2 <strong>Explanation:</strong> 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 a size greater than half of the size of the old array. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> arr = [7,7,7,7,7,7] <strong>Output:</strong> 1 <strong>Explanation:</strong> The only possible set you can choose is {7}. This will make the new array empty. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= arr.length &lt;= 10<sup>5</sup></code></li> <li><code>arr.length</code> is even.</li> <li><code>1 &lt;= arr[i] &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "code": "#pragma GCC optimize (\"O3,unroll-loops\")\n#pragma GCC target (\"avx2,bmi,bmi2,lzcnt,popcnt\")\n\n#define pii pair<int,int>\n\nclass Solution {\npublic:\n int minSetSize(vector<int>& arr) {\n ios_base::sync_with_stdio(false);\n cin.tie(0);\n\n vector<int> counts (*max_element(arr.begin(),arr.end())+1);\n for (const int &x : arr) counts[x]++;\n\n vector<pii> pairs;\n for (int i = 0; i < counts.size(); i++)\n if (counts[i]) pairs.push_back(pair(i,counts[i]));\n sort(pairs.begin(), pairs.end(), [] (const pii &a, const pii &b) {\n return a.second > b.second;\n });\n\n int len = arr.size(), currSize = 0, ans = 0;\n for (const pii &x : pairs) {\n currSize += x.second;\n ans++;\n if (currSize*2 >= len) return ans;\n }\n\n return 0;\n }\n};", "memory": "65881" }
1,464
<p>You are given an integer array <code>arr</code>. You can choose a set of integers and remove all the occurrences of these integers in the array.</p> <p>Return <em>the minimum size of the set so that <strong>at least</strong> half of the integers of the array are removed</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> arr = [3,3,3,3,5,5,5,2,2,7] <strong>Output:</strong> 2 <strong>Explanation:</strong> 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 a size greater than half of the size of the old array. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> arr = [7,7,7,7,7,7] <strong>Output:</strong> 1 <strong>Explanation:</strong> The only possible set you can choose is {7}. This will make the new array empty. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= arr.length &lt;= 10<sup>5</sup></code></li> <li><code>arr.length</code> is even.</li> <li><code>1 &lt;= arr[i] &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:bool static comp(int a, int b)\n{\n return a>b;\n}\n int minSetSize(vector<int>& arr) {\n int n=arr.size();\n vector<int>freq(100001,0);\n for(int i=0;i<arr.size();i++)\n {\n freq[arr[i]]++;\n }\n sort(freq.begin(),freq.end(),comp);\n int sum=0;\n for(int i=0;i<freq.size();i++)\n {\n sum+=freq[i];\n if(sum>=n/2)\n return i+1;\n \n }\n return 0;\n\n }\n};", "memory": "66268" }
1,464
<p>You are given an integer array <code>arr</code>. You can choose a set of integers and remove all the occurrences of these integers in the array.</p> <p>Return <em>the minimum size of the set so that <strong>at least</strong> half of the integers of the array are removed</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> arr = [3,3,3,3,5,5,5,2,2,7] <strong>Output:</strong> 2 <strong>Explanation:</strong> 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 a size greater than half of the size of the old array. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> arr = [7,7,7,7,7,7] <strong>Output:</strong> 1 <strong>Explanation:</strong> The only possible set you can choose is {7}. This will make the new array empty. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= arr.length &lt;= 10<sup>5</sup></code></li> <li><code>arr.length</code> is even.</li> <li><code>1 &lt;= arr[i] &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n int minSetSize(vector<int>& arr) {\n int n=arr.size();\n sort(arr.begin(),arr.end());\n int st=arr[0];\n int end=arr[arr.size()-1];\n vector<int> hash(end+1,0);\n for(int i=0;i<arr.size();i++){\n hash[arr[i]]++;\n }\n vector<pair<int,int>>nfreq;\n for(int i=st;i<=end;i++){\n if(hash[i]>0){\n nfreq.push_back({hash[i],i});\n }\n }\n sort(nfreq.rbegin(),nfreq.rend());\n n=n/2;\n int sum=0;\n int cnt=0;\n int it=0;\n while(sum<n){\n sum+=nfreq[it].first;\n ++it;\n cnt++;\n }\n return cnt;\n }\n};", "memory": "66656" }
1,464
<p>You are given an integer array <code>arr</code>. You can choose a set of integers and remove all the occurrences of these integers in the array.</p> <p>Return <em>the minimum size of the set so that <strong>at least</strong> half of the integers of the array are removed</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> arr = [3,3,3,3,5,5,5,2,2,7] <strong>Output:</strong> 2 <strong>Explanation:</strong> 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 a size greater than half of the size of the old array. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> arr = [7,7,7,7,7,7] <strong>Output:</strong> 1 <strong>Explanation:</strong> The only possible set you can choose is {7}. This will make the new array empty. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= arr.length &lt;= 10<sup>5</sup></code></li> <li><code>arr.length</code> is even.</li> <li><code>1 &lt;= arr[i] &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n int minSetSize(vector<int>& arr) {\n int n=arr.size();\n vector<int> freq(1e5+5,0);\n for(auto x:arr) freq[x]++;\n sort(freq.begin(),freq.end(),greater<int>());\n int sum=0;\n int i=0;\n while(sum<(n+1)/2) {sum+=freq[i];i++;}\n return i;\n \n }\n};", "memory": "67043" }
1,464
<p>You are given an integer array <code>arr</code>. You can choose a set of integers and remove all the occurrences of these integers in the array.</p> <p>Return <em>the minimum size of the set so that <strong>at least</strong> half of the integers of the array are removed</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> arr = [3,3,3,3,5,5,5,2,2,7] <strong>Output:</strong> 2 <strong>Explanation:</strong> 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 a size greater than half of the size of the old array. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> arr = [7,7,7,7,7,7] <strong>Output:</strong> 1 <strong>Explanation:</strong> The only possible set you can choose is {7}. This will make the new array empty. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= arr.length &lt;= 10<sup>5</sup></code></li> <li><code>arr.length</code> is even.</li> <li><code>1 &lt;= arr[i] &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n int minSetSize(vector<int>& arr) {\n\nint n=arr.size()/2;\n vector<int> vis(100001,0);\n for(int i=0;i<arr.size();i++)\n {\n vis[arr[i]]++;\n }\n\n sort(vis.begin(),vis.end(),greater<int>());\n\nint sum=0;\nint a=0;\n for(int i=0;i<vis.size();i++)\n {\n if(sum>=n)\n break;\n sum+=vis[i];\n a++;\n }\n return a;\n \n }\n};", "memory": "67431" }
1,464
<p>You are given an integer array <code>arr</code>. You can choose a set of integers and remove all the occurrences of these integers in the array.</p> <p>Return <em>the minimum size of the set so that <strong>at least</strong> half of the integers of the array are removed</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> arr = [3,3,3,3,5,5,5,2,2,7] <strong>Output:</strong> 2 <strong>Explanation:</strong> 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 a size greater than half of the size of the old array. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> arr = [7,7,7,7,7,7] <strong>Output:</strong> 1 <strong>Explanation:</strong> The only possible set you can choose is {7}. This will make the new array empty. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= arr.length &lt;= 10<sup>5</sup></code></li> <li><code>arr.length</code> is even.</li> <li><code>1 &lt;= arr[i] &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n int minSetSize(vector<int>& arr) {\n vector<int> count(100001, 0);\n for(int& i : arr) {count[i]++;}\n sort(count.begin(), count.end());\n int n = arr.size()/2;\n int removed{0};\n int retVal{0};\n int i = count.size()-1;\n while(removed < n){\n while(count[i] == 0) i--;\n removed+=count[i]; i--;\n retVal++;\n }\n return retVal;\n }\n};", "memory": "67818" }
1,464
<p>You are given an integer array <code>arr</code>. You can choose a set of integers and remove all the occurrences of these integers in the array.</p> <p>Return <em>the minimum size of the set so that <strong>at least</strong> half of the integers of the array are removed</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> arr = [3,3,3,3,5,5,5,2,2,7] <strong>Output:</strong> 2 <strong>Explanation:</strong> 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 a size greater than half of the size of the old array. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> arr = [7,7,7,7,7,7] <strong>Output:</strong> 1 <strong>Explanation:</strong> The only possible set you can choose is {7}. This will make the new array empty. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= arr.length &lt;= 10<sup>5</sup></code></li> <li><code>arr.length</code> is even.</li> <li><code>1 &lt;= arr[i] &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n int minSetSize(vector<int>& arr) {\n ios_base::sync_with_stdio(false);\n cin.tie(0);\n vector<int>v(100001, 0);\n int half = ceil(arr.size() / 2.0);\n int ans = 0;\n int count = 0;\n for (int& x : arr)v[x]++;\n sort(v.begin(), v.end());\n for (int x = v.size() - 1; x >= 0&&count<half; x--)\n count += v[x], ans++;\n return ans;\n }\n};", "memory": "68206" }
1,464
<p>You are given an integer array <code>arr</code>. You can choose a set of integers and remove all the occurrences of these integers in the array.</p> <p>Return <em>the minimum size of the set so that <strong>at least</strong> half of the integers of the array are removed</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> arr = [3,3,3,3,5,5,5,2,2,7] <strong>Output:</strong> 2 <strong>Explanation:</strong> 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 a size greater than half of the size of the old array. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> arr = [7,7,7,7,7,7] <strong>Output:</strong> 1 <strong>Explanation:</strong> The only possible set you can choose is {7}. This will make the new array empty. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= arr.length &lt;= 10<sup>5</sup></code></li> <li><code>arr.length</code> is even.</li> <li><code>1 &lt;= arr[i] &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n int minSetSize(vector<int>& arr) {\n vector <int> a(100005);\n int n=arr.size();\n for(int i=0;i<n;i++){\n a[arr[i]]++;\n }\n sort(a.rbegin(),a.rend());\n int total=0;\n for(int i=0;i<n;i++){\n total+=a[i];\n if(total>=n/2){\n return (i+1);\n }\n }\n return n;\n }\n};", "memory": "68593" }
1,464
<p>You are given an integer array <code>arr</code>. You can choose a set of integers and remove all the occurrences of these integers in the array.</p> <p>Return <em>the minimum size of the set so that <strong>at least</strong> half of the integers of the array are removed</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> arr = [3,3,3,3,5,5,5,2,2,7] <strong>Output:</strong> 2 <strong>Explanation:</strong> 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 a size greater than half of the size of the old array. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> arr = [7,7,7,7,7,7] <strong>Output:</strong> 1 <strong>Explanation:</strong> The only possible set you can choose is {7}. This will make the new array empty. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= arr.length &lt;= 10<sup>5</sup></code></li> <li><code>arr.length</code> is even.</li> <li><code>1 &lt;= arr[i] &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n int minSetSize(vector<int>& arr) {\n int n = arr.size();\n vector<int> freq(100001,0);\n for(int i : arr){\n freq[i]++;\n }\n sort(freq.rbegin(),freq.rend());\n int s = n;\n int i = 0 ;\n while(s > n/2){\n s-=freq[i++];\n }\n return i;\n }\n};", "memory": "68981" }
1,464
<p>You are given an integer array <code>arr</code>. You can choose a set of integers and remove all the occurrences of these integers in the array.</p> <p>Return <em>the minimum size of the set so that <strong>at least</strong> half of the integers of the array are removed</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> arr = [3,3,3,3,5,5,5,2,2,7] <strong>Output:</strong> 2 <strong>Explanation:</strong> 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 a size greater than half of the size of the old array. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> arr = [7,7,7,7,7,7] <strong>Output:</strong> 1 <strong>Explanation:</strong> The only possible set you can choose is {7}. This will make the new array empty. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= arr.length &lt;= 10<sup>5</sup></code></li> <li><code>arr.length</code> is even.</li> <li><code>1 &lt;= arr[i] &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n int minSetSize(vector<int>& arr) {\n int n = arr.size();\n\n vector<int>a(100005);\n for(int i=0; i<n; i++){\n a[arr[i]]++;\n }\n sort(a.rbegin(), a.rend());\n int total = 0;\n\n for(int i=0; i<n; i++){\n total += a[i];\n\n if(total >= (n/2))\n return (i+1);\n }\n return n;\n }\n};", "memory": "69368" }
1,464
<p>You are given an integer array <code>arr</code>. You can choose a set of integers and remove all the occurrences of these integers in the array.</p> <p>Return <em>the minimum size of the set so that <strong>at least</strong> half of the integers of the array are removed</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> arr = [3,3,3,3,5,5,5,2,2,7] <strong>Output:</strong> 2 <strong>Explanation:</strong> 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 a size greater than half of the size of the old array. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> arr = [7,7,7,7,7,7] <strong>Output:</strong> 1 <strong>Explanation:</strong> The only possible set you can choose is {7}. This will make the new array empty. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= arr.length &lt;= 10<sup>5</sup></code></li> <li><code>arr.length</code> is even.</li> <li><code>1 &lt;= arr[i] &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n int minSetSize(vector<int>& arr) {\n vector<int> f(100001, 0);\n for(auto u : arr)\n f[u]++;\n sort(f.rbegin(), f.rend());\n int s = 0 , a = 0;\n for(int i=0;i<f.size();i++) {\n s+=f[i];\n if(s*2 >= arr.size())\n return i+1;\n }\n return -1;\n }\n};", "memory": "69756" }
1,464
<p>You are given an integer array <code>arr</code>. You can choose a set of integers and remove all the occurrences of these integers in the array.</p> <p>Return <em>the minimum size of the set so that <strong>at least</strong> half of the integers of the array are removed</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> arr = [3,3,3,3,5,5,5,2,2,7] <strong>Output:</strong> 2 <strong>Explanation:</strong> 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 a size greater than half of the size of the old array. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> arr = [7,7,7,7,7,7] <strong>Output:</strong> 1 <strong>Explanation:</strong> The only possible set you can choose is {7}. This will make the new array empty. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= arr.length &lt;= 10<sup>5</sup></code></li> <li><code>arr.length</code> is even.</li> <li><code>1 &lt;= arr[i] &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n Solution() {\n // ios::sync_with_stdio(0);\n // cin.tie(0);\n // cout.tie(0);\n }\n\n int minSetSize(vector<int>& arr) {\n // here we can use hash map instead\n vector<int> temp(100001);\n\n for (int i = 0; i < arr.size(); i++) {\n temp[arr[i]]++;\n }\n\n int count = 0;\n int update = arr.size();\n\n priority_queue<int> q;\n\n for (int i = 0; i < 100001; i++) {\n if (temp[i] > 0) {\n count++;\n q.push(temp[i]);\n }\n }\n\n if (count == 1)\n return 1;\n int count2 = 0;\n\n int size = arr.size();\n\n while (update > size / 2 && !q.empty()) {\n int a = q.top();\n q.pop();\n update = update - a;\n count2++;\n }\n\n return count2;\n }\n};\n", "memory": "70143" }
1,464
<p>You are given an integer array <code>arr</code>. You can choose a set of integers and remove all the occurrences of these integers in the array.</p> <p>Return <em>the minimum size of the set so that <strong>at least</strong> half of the integers of the array are removed</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> arr = [3,3,3,3,5,5,5,2,2,7] <strong>Output:</strong> 2 <strong>Explanation:</strong> 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 a size greater than half of the size of the old array. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> arr = [7,7,7,7,7,7] <strong>Output:</strong> 1 <strong>Explanation:</strong> The only possible set you can choose is {7}. This will make the new array empty. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= arr.length &lt;= 10<sup>5</sup></code></li> <li><code>arr.length</code> is even.</li> <li><code>1 &lt;= arr[i] &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n\n Solution(){\n ios::sync_with_stdio(0);\n cin.tie(0);\n cout.tie(0);\n }\n\n\n int minSetSize(vector<int>& arr) {\n\n vector<int>temp(100001);\n\n for(int i = 0;i < arr.size();i++){\n temp[arr[i]]++;\n }\n\n int count = 0;\n int update = arr.size();\n\n priority_queue<int>q;\n\n for(int i = 0;i < 100001;i++){\n if(temp[i]>0){\n count++;\n q.push(temp[i]);\n }\n }\n\n if(count==1) return 1;\n int count2 = 0;\n\n int size = arr.size();\n\n while(update > size/2 && !q.empty()){\n int a = q.top();\n q.pop();\n update = update - a;\n count2++;\n }\n\n return count2;\n \n }\n};", "memory": "70531" }
1,464
<p>You are given an integer array <code>arr</code>. You can choose a set of integers and remove all the occurrences of these integers in the array.</p> <p>Return <em>the minimum size of the set so that <strong>at least</strong> half of the integers of the array are removed</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> arr = [3,3,3,3,5,5,5,2,2,7] <strong>Output:</strong> 2 <strong>Explanation:</strong> 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 a size greater than half of the size of the old array. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> arr = [7,7,7,7,7,7] <strong>Output:</strong> 1 <strong>Explanation:</strong> The only possible set you can choose is {7}. This will make the new array empty. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= arr.length &lt;= 10<sup>5</sup></code></li> <li><code>arr.length</code> is even.</li> <li><code>1 &lt;= arr[i] &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n\n Solution(){\n // ios::sync_with_stdio(0);\n // cin.tie(0);\n // cout.tie(0);\n }\n\n\n int minSetSize(vector<int>& arr) {\n\n vector<int>temp(100001);\n\n for(int i = 0;i < arr.size();i++){\n temp[arr[i]]++;\n }\n\n int count = 0;\n int update = arr.size();\n\n priority_queue<int>q;\n\n for(int i = 0;i < 100001;i++){\n if(temp[i]>0){\n count++;\n q.push(temp[i]);\n }\n }\n\n if(count==1) return 1;\n int count2 = 0;\n\n int size = arr.size();\n\n while(update > size/2 && !q.empty()){\n int a = q.top();\n q.pop();\n update = update - a;\n count2++;\n }\n\n return count2;\n \n }\n};", "memory": "70918" }
1,464
<p>You are given an integer array <code>arr</code>. You can choose a set of integers and remove all the occurrences of these integers in the array.</p> <p>Return <em>the minimum size of the set so that <strong>at least</strong> half of the integers of the array are removed</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> arr = [3,3,3,3,5,5,5,2,2,7] <strong>Output:</strong> 2 <strong>Explanation:</strong> 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 a size greater than half of the size of the old array. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> arr = [7,7,7,7,7,7] <strong>Output:</strong> 1 <strong>Explanation:</strong> The only possible set you can choose is {7}. This will make the new array empty. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= arr.length &lt;= 10<sup>5</sup></code></li> <li><code>arr.length</code> is even.</li> <li><code>1 &lt;= arr[i] &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n int minSetSize(vector<int>& arr) {\n ios_base::sync_with_stdio(false);\n cin.tie(0);\n int count=0;\n vector<int> v(100001);\n for(auto val:arr){\n v[val]++;\n }\n int tcount=0;\n priority_queue<int> pq;\n for(auto val:v){\n if(val>0){\n tcount++;\n pq.push(val);\n }\n }\n if(tcount==1){return 1;}\n int n=arr.size();\n int x=n/2;\n while(!pq.empty() && n>x){\n n-=pq.top();\n pq.pop();\n count++;\n }\n return count;\n }\n};", "memory": "71306" }
1,464
<p>You are given an integer array <code>arr</code>. You can choose a set of integers and remove all the occurrences of these integers in the array.</p> <p>Return <em>the minimum size of the set so that <strong>at least</strong> half of the integers of the array are removed</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> arr = [3,3,3,3,5,5,5,2,2,7] <strong>Output:</strong> 2 <strong>Explanation:</strong> 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 a size greater than half of the size of the old array. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> arr = [7,7,7,7,7,7] <strong>Output:</strong> 1 <strong>Explanation:</strong> The only possible set you can choose is {7}. This will make the new array empty. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= arr.length &lt;= 10<sup>5</sup></code></li> <li><code>arr.length</code> is even.</li> <li><code>1 &lt;= arr[i] &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n int minSetSize(vector<int>& arr) {\n ios::sync_with_stdio(0);\n cin.tie(0);\n cout.tie(0);\n\n int limit = arr.size(), count = 0, half = limit / 2, sum = 0;\n\n#if 0\n //1.選擇m_map(這題值的range比較大)\n unordered_map<int,int> m_map;\n for(int num : arr) {\n ++m_map[num];\n }\n\n //2.把值放到priority_queue排序\n priority_queue<int> m_pq;\n for(auto& item : m_map) {\n m_pq.push(item.second);\n }\n#else\n //1.選擇vector\n vector<int> counts(1e5 + 1);\n for(int num : arr) {\n ++counts[num];\n }\n\n //2.把值放到priority_queue排序\n priority_queue<int> m_pq;\n for(int count : counts) {\n if(count > 0)\n m_pq.push(count);\n }\n#endif\n //3.把值pop出來直到累計總數已至少一半以上\n while(sum < half) {\n sum += m_pq.top();\n m_pq.pop();\n ++count;\n }\n return count;\n }\n};", "memory": "71693" }
1,464
<p>You are given an integer array <code>arr</code>. You can choose a set of integers and remove all the occurrences of these integers in the array.</p> <p>Return <em>the minimum size of the set so that <strong>at least</strong> half of the integers of the array are removed</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> arr = [3,3,3,3,5,5,5,2,2,7] <strong>Output:</strong> 2 <strong>Explanation:</strong> 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 a size greater than half of the size of the old array. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> arr = [7,7,7,7,7,7] <strong>Output:</strong> 1 <strong>Explanation:</strong> The only possible set you can choose is {7}. This will make the new array empty. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= arr.length &lt;= 10<sup>5</sup></code></li> <li><code>arr.length</code> is even.</li> <li><code>1 &lt;= arr[i] &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n int minSetSize(vector<int>& arr) {\n set<int> st;\n int n = arr.size();\n int mx = INT_MIN;\n for (int i = 0; i < n; i++) mx = max (mx , arr[i]);\n // map <int , int> mp;\n // for (int i: arr) mp[i]++; \n vector <int> v(mx+1,0);\n // for (auto i : mp) v.push_back(i.second);\n for (int i = 0; i < n; i++) v[arr[i]]++;\n sort (v.rbegin(),v.rend());\n int c = 0;\n for (int i = 0; i < v.size(); i++) {\n c += v[i];\n st.insert(i);\n if (c >= n / 2) return st.size();\n }\n return 0;\n }\n};", "memory": "72081" }
1,464
<p>You are given an integer array <code>arr</code>. You can choose a set of integers and remove all the occurrences of these integers in the array.</p> <p>Return <em>the minimum size of the set so that <strong>at least</strong> half of the integers of the array are removed</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> arr = [3,3,3,3,5,5,5,2,2,7] <strong>Output:</strong> 2 <strong>Explanation:</strong> 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 a size greater than half of the size of the old array. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> arr = [7,7,7,7,7,7] <strong>Output:</strong> 1 <strong>Explanation:</strong> The only possible set you can choose is {7}. This will make the new array empty. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= arr.length &lt;= 10<sup>5</sup></code></li> <li><code>arr.length</code> is even.</li> <li><code>1 &lt;= arr[i] &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n int N = 1e5 + 10;\n int minSetSize(vector<int>& arr) {\n vector<int> cnt(N);\n for(int& x : arr) cnt[x]++;\n int n = arr.size();\n int min_req = n / 2;\n if(n & 1) min_req++;\n vector<int> v;\n for(int& p : cnt) {\n if(p)v.push_back(p);\n }\n sort(v.rbegin(), v.rend());\n int have = 0;\n int i = 0;\n while(have < min_req) {\n have += v[i++];\n }\n return i;\n }\n};", "memory": "72468" }
1,464
<p>You are given an integer array <code>arr</code>. You can choose a set of integers and remove all the occurrences of these integers in the array.</p> <p>Return <em>the minimum size of the set so that <strong>at least</strong> half of the integers of the array are removed</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> arr = [3,3,3,3,5,5,5,2,2,7] <strong>Output:</strong> 2 <strong>Explanation:</strong> 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 a size greater than half of the size of the old array. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> arr = [7,7,7,7,7,7] <strong>Output:</strong> 1 <strong>Explanation:</strong> The only possible set you can choose is {7}. This will make the new array empty. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= arr.length &lt;= 10<sup>5</sup></code></li> <li><code>arr.length</code> is even.</li> <li><code>1 &lt;= arr[i] &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n int minSetSize(vector<int>& arr) {\n unordered_map<int, int> counter;\n for (int e : arr) {\n ++counter[e];\n }\n \n sort(arr.begin(), arr.end(), [&](int l, int r) {\n if (counter[l] == counter[r]) {\n return l > r;\n }\n \n return counter[l] > counter[r];\n });\n \n int result = 0;\n for (size_t i = 0; i < arr.size() / 2; ) {\n int len = counter[arr[i]];\n i += len;\n ++result;\n }\n \n return result;\n }\n};", "memory": "72856" }
1,464
<p>You are given an integer array <code>arr</code>. You can choose a set of integers and remove all the occurrences of these integers in the array.</p> <p>Return <em>the minimum size of the set so that <strong>at least</strong> half of the integers of the array are removed</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> arr = [3,3,3,3,5,5,5,2,2,7] <strong>Output:</strong> 2 <strong>Explanation:</strong> 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 a size greater than half of the size of the old array. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> arr = [7,7,7,7,7,7] <strong>Output:</strong> 1 <strong>Explanation:</strong> The only possible set you can choose is {7}. This will make the new array empty. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= arr.length &lt;= 10<sup>5</sup></code></li> <li><code>arr.length</code> is even.</li> <li><code>1 &lt;= arr[i] &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n int minSetSize(vector<int>& arr) {\n int ans = 0;\n // count frequency of ints\n int m = 0;\n unordered_map<int, int> F;\n for (int &n: arr) {\n m = max(m, ++F[n]);\n }\n // count freqnecy of frequencies\n int n = 0;\n int *FF = new int[++m];\n memset(FF, 0, sizeof(int) * m);\n for (auto &p: F) {\n ++FF[p.second];\n n = max(m, p.second);\n }\n\n // grab most frequent ints and remove those first\n int o = (arr.size() + 1) / 2;\n for (int i = n-1; i > 0 && o > 0; --i) {\n while (i > 0 && !FF[i]) --i;\n if (o >= FF[i] * i) {\n o -= FF[i] * i;\n ans += FF[i];\n FF[i] = 0;\n } else {\n while (o > 0 && FF[i]) {\n ++ans;\n o -= i;\n --FF[i];\n }\n FF[i] = 0;\n }\n }\n\n return ans;\n }\n};", "memory": "73243" }
1,464
<p>You are given an integer array <code>arr</code>. You can choose a set of integers and remove all the occurrences of these integers in the array.</p> <p>Return <em>the minimum size of the set so that <strong>at least</strong> half of the integers of the array are removed</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> arr = [3,3,3,3,5,5,5,2,2,7] <strong>Output:</strong> 2 <strong>Explanation:</strong> 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 a size greater than half of the size of the old array. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> arr = [7,7,7,7,7,7] <strong>Output:</strong> 1 <strong>Explanation:</strong> The only possible set you can choose is {7}. This will make the new array empty. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= arr.length &lt;= 10<sup>5</sup></code></li> <li><code>arr.length</code> is even.</li> <li><code>1 &lt;= arr[i] &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic: /* // Complexity: O(nlogn) Space: O(n)\n int minSetSize(vector<int>& A) {\n int n = A.size(), res = 0;\n if (n <= 1)\n return n;\n if (n == 2)\n return 1;\n\n vector<int> uniqs;\n\n unordered_map<int, int> f;\n for (int num : A)\n {\n if (f[num]++ == 0)\n uniqs.push_back(num);\n }\n\n if (uniqs.size() == n) // note: n+1/2\n return (n+1)/2;\n\n auto cmp = [&f](const int& i, const int& j) -> bool { return f[i] >= f[j]; };\n sort(uniqs.begin(), uniqs.end(), cmp); // max\n\n for (int num : uniqs)\n {\n n -= f[num];\n res++;\n\n if (n <= A.size()/2)\n break;\n }\n \n return res;\n } */\n\n // Faster Complexity: O(n) \n int minSetSize(vector<int>& nums) {\n int n = nums.size(), res = 0, maxf = 0;\n if (n <= 1)\n return n;\n if (n == 2)\n return 1;\n\n unordered_map<int, int> f;\n for (int num : nums)\n maxf = max(maxf, ++f[num]);\n\n vector<int> buckets(maxf+1, 0);\n for (auto& [k, v] : f)\n buckets[v]++;\n\n int numbersToRemoveFromArr = (n+1)/2;\n\n for (int i = maxf; i>0 && numbersToRemoveFromArr > 0; i--)\n {\n if (buckets[i] == 0)\n continue;\n\n if (numbersToRemoveFromArr - i*buckets[i] >= 0)\n {\n numbersToRemoveFromArr -= i*buckets[i];\n res += buckets[i];\n }\n else\n {\n while (buckets[i] && numbersToRemoveFromArr > 0)\n {\n numbersToRemoveFromArr -= i;\n res++;\n buckets[i]--;\n }\n }\n }\n \n return res;\n }\n};", "memory": "73631" }
1,464
<p>You are given an integer array <code>arr</code>. You can choose a set of integers and remove all the occurrences of these integers in the array.</p> <p>Return <em>the minimum size of the set so that <strong>at least</strong> half of the integers of the array are removed</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> arr = [3,3,3,3,5,5,5,2,2,7] <strong>Output:</strong> 2 <strong>Explanation:</strong> 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 a size greater than half of the size of the old array. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> arr = [7,7,7,7,7,7] <strong>Output:</strong> 1 <strong>Explanation:</strong> The only possible set you can choose is {7}. This will make the new array empty. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= arr.length &lt;= 10<sup>5</sup></code></li> <li><code>arr.length</code> is even.</li> <li><code>1 &lt;= arr[i] &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n int minSetSize(vector<int>& arr) {\n // lame sauce\n ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);\n\n int ans = 0;\n // count frequency of ints\n int m = 0;\n unordered_map<int, int> F;\n for (int &n: arr) {\n m = max(m, ++F[n]);\n }\n // count freqnecy of frequencies\n int n = 0;\n int *FF = new int[++m];\n memset(FF, 0, sizeof(int) * m);\n for (auto &p: F) {\n ++FF[p.second];\n n = max(m, p.second);\n }\n\n // grab most frequent ints and remove those first\n int o = (arr.size() + 1) / 2;\n for (int i = n-1; i > 0 && o > 0; --i) {\n while (i > 0 && !FF[i]) --i;\n if (o >= FF[i] * i) {\n o -= FF[i] * i;\n ans += FF[i];\n FF[i] = 0;\n } else {\n while (o > 0 && FF[i]) {\n ++ans;\n o -= i;\n --FF[i];\n }\n FF[i] = 0;\n }\n }\n\n return ans;\n }\n};", "memory": "74018" }
1,464
<p>You are given an integer array <code>arr</code>. You can choose a set of integers and remove all the occurrences of these integers in the array.</p> <p>Return <em>the minimum size of the set so that <strong>at least</strong> half of the integers of the array are removed</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> arr = [3,3,3,3,5,5,5,2,2,7] <strong>Output:</strong> 2 <strong>Explanation:</strong> 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 a size greater than half of the size of the old array. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> arr = [7,7,7,7,7,7] <strong>Output:</strong> 1 <strong>Explanation:</strong> The only possible set you can choose is {7}. This will make the new array empty. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= arr.length &lt;= 10<sup>5</sup></code></li> <li><code>arr.length</code> is even.</li> <li><code>1 &lt;= arr[i] &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n int minSetSize(vector<int>& arr) {\n int n = arr.size();\n unordered_map<int, int> freqMap;\n for (auto i: arr)\n freqMap[i]++;\n vector<int> freq;\n freq.reserve(freqMap.size());\n for (auto [_, f]: freqMap)\n freq.push_back(f);\n \n sort(freq.begin(), freq.end(), std::greater{});\n int allowed = n / 2, res = 0;\n for (int i = 0; i < freq.size() && n > allowed; i++)\n {\n n -= freq[i];\n res++;\n }\n \n return res;\n }\n};", "memory": "74406" }
1,464
<p>You are given an integer array <code>arr</code>. You can choose a set of integers and remove all the occurrences of these integers in the array.</p> <p>Return <em>the minimum size of the set so that <strong>at least</strong> half of the integers of the array are removed</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> arr = [3,3,3,3,5,5,5,2,2,7] <strong>Output:</strong> 2 <strong>Explanation:</strong> 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 a size greater than half of the size of the old array. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> arr = [7,7,7,7,7,7] <strong>Output:</strong> 1 <strong>Explanation:</strong> The only possible set you can choose is {7}. This will make the new array empty. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= arr.length &lt;= 10<sup>5</sup></code></li> <li><code>arr.length</code> is even.</li> <li><code>1 &lt;= arr[i] &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n int minSetSize(vector<int>& arr) {\n std::unordered_map<int, int> counter;\n for (auto const& num: arr){\n counter[num]++;\n }\n\n std::vector<int> vcounter;\n vcounter.reserve(counter.size());\n for (auto const& pair: counter){\n vcounter.emplace_back(pair.second);\n }\n\n std::sort(vcounter.begin(), vcounter.end(), std::greater<>());\n\n int target{(static_cast<int>(arr.size())+1)/2}, ans{};\n for (const int count:vcounter){\n target-=count;\n ans++;\n if (target<=0) return ans;\n }\n\n return ans;\n \n }\n};", "memory": "74793" }
1,464
<p>You are given an integer array <code>arr</code>. You can choose a set of integers and remove all the occurrences of these integers in the array.</p> <p>Return <em>the minimum size of the set so that <strong>at least</strong> half of the integers of the array are removed</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> arr = [3,3,3,3,5,5,5,2,2,7] <strong>Output:</strong> 2 <strong>Explanation:</strong> 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 a size greater than half of the size of the old array. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> arr = [7,7,7,7,7,7] <strong>Output:</strong> 1 <strong>Explanation:</strong> The only possible set you can choose is {7}. This will make the new array empty. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= arr.length &lt;= 10<sup>5</sup></code></li> <li><code>arr.length</code> is even.</li> <li><code>1 &lt;= arr[i] &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n int minSetSize(vector<int>& arr) {\n int n = arr.size();\n unordered_map<int,int> counter;\n priority_queue<int, vector<int>, greater<int> > pq;\n for (int x : arr){\n counter[x]++;\n }\n int curr = 0;\n int ans = INT_MAX;\n for (auto [key, freq] : counter){ \n curr += freq;\n pq.push(freq);\n while( curr >= n/2){\n ans = min(ans, (int) pq.size());\n curr -= pq.top();\n pq.pop();\n }\n }\n return ans;\n }\n};", "memory": "75181" }
1,464
<p>You are given an integer array <code>arr</code>. You can choose a set of integers and remove all the occurrences of these integers in the array.</p> <p>Return <em>the minimum size of the set so that <strong>at least</strong> half of the integers of the array are removed</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> arr = [3,3,3,3,5,5,5,2,2,7] <strong>Output:</strong> 2 <strong>Explanation:</strong> 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 a size greater than half of the size of the old array. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> arr = [7,7,7,7,7,7] <strong>Output:</strong> 1 <strong>Explanation:</strong> The only possible set you can choose is {7}. This will make the new array empty. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= arr.length &lt;= 10<sup>5</sup></code></li> <li><code>arr.length</code> is even.</li> <li><code>1 &lt;= arr[i] &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n int minSetSize(vector<int>& arr) {\n int n= arr.size();\n map<int,int> m;\n int target = n/2;\n for(int i=0;i<n;i++){\n m[arr[i]]++;\n }\n arr.clear();\n for(auto i : m){\n arr.push_back(i.second);\n }\n sort(arr.begin(),arr.end());\n int sum=0,count=0,i=arr.size()-1;\n while(sum<target){\n count++;\n sum=sum+arr[i--];\n\n }\n return count;\n \n \n \n }\n};", "memory": "75568" }
1,464
<p>You are given an integer array <code>arr</code>. You can choose a set of integers and remove all the occurrences of these integers in the array.</p> <p>Return <em>the minimum size of the set so that <strong>at least</strong> half of the integers of the array are removed</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> arr = [3,3,3,3,5,5,5,2,2,7] <strong>Output:</strong> 2 <strong>Explanation:</strong> 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 a size greater than half of the size of the old array. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> arr = [7,7,7,7,7,7] <strong>Output:</strong> 1 <strong>Explanation:</strong> The only possible set you can choose is {7}. This will make the new array empty. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= arr.length &lt;= 10<sup>5</sup></code></li> <li><code>arr.length</code> is even.</li> <li><code>1 &lt;= arr[i] &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n int minSetSize(vector<int>& arr) {\n unordered_map<int, int> numToFreq;\n for (auto num : arr) {\n numToFreq[num]++;\n }\n vector<int> freqFreq(arr.size() + 1, 0); // frequency of frequency\n for (auto [num, freq] : numToFreq) {\n freqFreq[freq]++;\n }\n int ans = 0;\n int removed = 0;\n int target = ceil(arr.size() / 2);\n int frequency = arr.size();\n while (removed < target) {\n while (freqFreq[frequency] == 0) {\n frequency--;\n }\n removed += frequency;\n freqFreq[frequency]--;\n ans++;\n }\n return ans;\n }\n};", "memory": "75956" }
1,464
<p>You are given an integer array <code>arr</code>. You can choose a set of integers and remove all the occurrences of these integers in the array.</p> <p>Return <em>the minimum size of the set so that <strong>at least</strong> half of the integers of the array are removed</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> arr = [3,3,3,3,5,5,5,2,2,7] <strong>Output:</strong> 2 <strong>Explanation:</strong> 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 a size greater than half of the size of the old array. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> arr = [7,7,7,7,7,7] <strong>Output:</strong> 1 <strong>Explanation:</strong> The only possible set you can choose is {7}. This will make the new array empty. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= arr.length &lt;= 10<sup>5</sup></code></li> <li><code>arr.length</code> is even.</li> <li><code>1 &lt;= arr[i] &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "code": "#include <cmath>\nclass Solution {\npublic:\n int minSetSize(vector<int>& arr) {\n unordered_map<int, int> m;\n int n = arr.size();\n for(const int& i : arr){\n m[i]++;\n }\n vector<int> freqs(n+1, 0); // freqs[i] = number of elements repeated i times \n for(auto i = m.begin(); i!=m.end(); ++i) {\n freqs[i->second]++;\n }\n int posn = n;\n int to_remove = ceil(n/2);\n int removed = 0;\n\n while(to_remove > 0) {\n if(freqs[posn] == 0) {\n posn--;\n } else {\n to_remove-=posn;\n freqs[posn]--;\n removed++;\n }\n }\n\n return removed;\n\n }\n};", "memory": "76343" }
1,464
<p>You are given an integer array <code>arr</code>. You can choose a set of integers and remove all the occurrences of these integers in the array.</p> <p>Return <em>the minimum size of the set so that <strong>at least</strong> half of the integers of the array are removed</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> arr = [3,3,3,3,5,5,5,2,2,7] <strong>Output:</strong> 2 <strong>Explanation:</strong> 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 a size greater than half of the size of the old array. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> arr = [7,7,7,7,7,7] <strong>Output:</strong> 1 <strong>Explanation:</strong> The only possible set you can choose is {7}. This will make the new array empty. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= arr.length &lt;= 10<sup>5</sup></code></li> <li><code>arr.length</code> is even.</li> <li><code>1 &lt;= arr[i] &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n int minSetSize(vector<int>& arr) {\n unordered_map<int,int>mp;\n int n=arr.size(),k=(n+1)/2,answer=0,i=n;\n vector<int>list(n+1,0);\n\n for(auto num:arr)mp[num]++;\n for(auto it:mp)list[it.second]++;\n while(k>0){\n answer++;\n while(list[i]==0)i--;\n list[i]--;\n k-=i;\n }\n return answer;\n }\n};", "memory": "76731" }
1,464
<p>You are given an integer array <code>arr</code>. You can choose a set of integers and remove all the occurrences of these integers in the array.</p> <p>Return <em>the minimum size of the set so that <strong>at least</strong> half of the integers of the array are removed</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> arr = [3,3,3,3,5,5,5,2,2,7] <strong>Output:</strong> 2 <strong>Explanation:</strong> 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 a size greater than half of the size of the old array. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> arr = [7,7,7,7,7,7] <strong>Output:</strong> 1 <strong>Explanation:</strong> The only possible set you can choose is {7}. This will make the new array empty. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= arr.length &lt;= 10<sup>5</sup></code></li> <li><code>arr.length</code> is even.</li> <li><code>1 &lt;= arr[i] &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n int minSetSize(vector<int>& arr) {\n\t // Sort the element count from max to min\n\n\t // Construct a hash of element to count\n\t std::unordered_map<int, int> hash;\n\t for(int i = 0; i < arr.size(); i++) {\n\t\t hash[arr[i]] += 1;\n\t }\n\n\t // Convert to a vector and sort\n\t vector<std::pair<int, int>> vec(hash.begin(), hash.end());\n\t sort(vec.begin(), vec.end(), \n\t\t\t [](const pair<int, int> &a, const pair<int, int> &b) \n\t\t\t {\n\t\t\t\treturn a.second > b.second;\n\t\t\t });\n\n\t int size = arr.size();\n\t int half = size / 2;\n\n\t for(int i = 0; i < vec.size(); i++) {\n\t\t size -= vec[i].second;\n\n\t\t if (size <= half)\n\t\t\t return i+1;\n\t }\n\n\t return -1;\n }\n};\n", "memory": "77118" }
1,464
<p>You are given an integer array <code>arr</code>. You can choose a set of integers and remove all the occurrences of these integers in the array.</p> <p>Return <em>the minimum size of the set so that <strong>at least</strong> half of the integers of the array are removed</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> arr = [3,3,3,3,5,5,5,2,2,7] <strong>Output:</strong> 2 <strong>Explanation:</strong> 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 a size greater than half of the size of the old array. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> arr = [7,7,7,7,7,7] <strong>Output:</strong> 1 <strong>Explanation:</strong> The only possible set you can choose is {7}. This will make the new array empty. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= arr.length &lt;= 10<sup>5</sup></code></li> <li><code>arr.length</code> is even.</li> <li><code>1 &lt;= arr[i] &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n int minSetSize(vector<int>& arr) {\n int n=arr.size(),sum=0,count=0;\n unordered_map<int,int>mp;\n for(int i=0;i<n;i++){\n mp[arr[i]]++;\n\n }\n vector<int> freq;\n for (const auto& entry : mp) {\n freq.push_back(entry.second);\n }\n sort(freq.begin(),freq.end(),greater<int>());\n for(int i=0;i<freq.size();i++){\n sum=sum+freq[i];\n count++;\n if(sum>=n/2){\n return count;\n }\n }\n return 0;\n }\n\n\n};", "memory": "79056" }
1,464
<p>You are given an integer array <code>arr</code>. You can choose a set of integers and remove all the occurrences of these integers in the array.</p> <p>Return <em>the minimum size of the set so that <strong>at least</strong> half of the integers of the array are removed</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> arr = [3,3,3,3,5,5,5,2,2,7] <strong>Output:</strong> 2 <strong>Explanation:</strong> 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 a size greater than half of the size of the old array. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> arr = [7,7,7,7,7,7] <strong>Output:</strong> 1 <strong>Explanation:</strong> The only possible set you can choose is {7}. This will make the new array empty. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= arr.length &lt;= 10<sup>5</sup></code></li> <li><code>arr.length</code> is even.</li> <li><code>1 &lt;= arr[i] &lt;= 10<sup>5</sup></code></li> </ul>
1
{ "code": "class Solution {\npublic:\n int minSetSize(vector<int>& arr) {\n unordered_map<int,int> mp;\n for(int i=0;i<arr.size();i++)\n mp[arr[i]]++;\n // priority_queue<pair<int,int>> pq;\n vector<int> v;\n for(auto it:mp)\n v.push_back(it.second);\n sort(v.begin(),v.end(), greater());\n int val=0,i=0;\n while(i<v.size() && val < arr.size()/2)\n {\n val += v[i++];\n // cnt++;\n // pq.pop();\n }\n return i;\n }\n};", "memory": "79443" }
1,464
<p>You are given an integer array <code>arr</code>. You can choose a set of integers and remove all the occurrences of these integers in the array.</p> <p>Return <em>the minimum size of the set so that <strong>at least</strong> half of the integers of the array are removed</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> arr = [3,3,3,3,5,5,5,2,2,7] <strong>Output:</strong> 2 <strong>Explanation:</strong> 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 a size greater than half of the size of the old array. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> arr = [7,7,7,7,7,7] <strong>Output:</strong> 1 <strong>Explanation:</strong> The only possible set you can choose is {7}. This will make the new array empty. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= arr.length &lt;= 10<sup>5</sup></code></li> <li><code>arr.length</code> is even.</li> <li><code>1 &lt;= arr[i] &lt;= 10<sup>5</sup></code></li> </ul>
1
{ "code": "class Solution {\npublic:\n int minSetSize(vector<int>& arr) {\n unordered_map<int, int> m;\n for(int a: arr){\n m[a]++;\n }\n priority_queue<int, vector<int>> pq;\n for(auto it: m){\n pq.push({it.second});\n }\n\n int target = arr.size()/2;\n int removed =0;\n int count = 0;\n while(removed<target){\n removed += pq.top();\n pq.pop();\n count++;\n }\n return count;\n \n }\n};", "memory": "79443" }
1,464
<p>You are given an integer array <code>arr</code>. You can choose a set of integers and remove all the occurrences of these integers in the array.</p> <p>Return <em>the minimum size of the set so that <strong>at least</strong> half of the integers of the array are removed</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> arr = [3,3,3,3,5,5,5,2,2,7] <strong>Output:</strong> 2 <strong>Explanation:</strong> 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 a size greater than half of the size of the old array. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> arr = [7,7,7,7,7,7] <strong>Output:</strong> 1 <strong>Explanation:</strong> The only possible set you can choose is {7}. This will make the new array empty. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= arr.length &lt;= 10<sup>5</sup></code></li> <li><code>arr.length</code> is even.</li> <li><code>1 &lt;= arr[i] &lt;= 10<sup>5</sup></code></li> </ul>
1
{ "code": "auto init = []() {\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n cout.tie(nullptr);\n return 'c';\n}();\nclass Solution {\npublic:\n int minSetSize(vector<int>& arr) {\n unordered_map<int,int>mp;\n for(auto i : arr){\n mp[i]++;\n }\n int n = arr.size(),cnt = 0;\n int rem = n/2;\n priority_queue<int>q;\n for(auto& p:mp){\n q.push(p.second);\n }\n while(rem>0){\n int node = q.top();\n q.pop();\n rem-=node;\n if(rem<=0) return ++cnt;\n else cnt++;\n }\n return 0;\n }\n};", "memory": "79831" }
1,464
<p>You are given an integer array <code>arr</code>. You can choose a set of integers and remove all the occurrences of these integers in the array.</p> <p>Return <em>the minimum size of the set so that <strong>at least</strong> half of the integers of the array are removed</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> arr = [3,3,3,3,5,5,5,2,2,7] <strong>Output:</strong> 2 <strong>Explanation:</strong> 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 a size greater than half of the size of the old array. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> arr = [7,7,7,7,7,7] <strong>Output:</strong> 1 <strong>Explanation:</strong> The only possible set you can choose is {7}. This will make the new array empty. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= arr.length &lt;= 10<sup>5</sup></code></li> <li><code>arr.length</code> is even.</li> <li><code>1 &lt;= arr[i] &lt;= 10<sup>5</sup></code></li> </ul>
1
{ "code": "class Solution {\npublic:\n int minSetSize(vector<int>& arr) {\n // count the number of occurrences of each element\n std::unordered_map<int, int> occurrences;\n\n for (int i : arr) {\n auto it = occurrences.find(i);\n\n if (it == occurrences.end()) {\n occurrences[i] = 1;\n continue;\n }\n\n ++it->second;\n }\n\n // sort the elements by occurrence\n vector<int> sorted(occurrences.size());\n \n for (auto &p : occurrences) {\n sorted.emplace_back(p.second);\n }\n\n std::sort(sorted.begin(), sorted.end());\n\n // count the minimum number of elements that must be removed\n // so that the size of the array is halved\n int minElements = 0;\n int half = arr.size() / 2;\n\n for (auto it = sorted.rbegin(); half > 0; it++) {\n half -= *it;\n ++minElements;\n }\n\n return minElements;\n }\n};", "memory": "80218" }
1,464
<p>You are given an integer array <code>arr</code>. You can choose a set of integers and remove all the occurrences of these integers in the array.</p> <p>Return <em>the minimum size of the set so that <strong>at least</strong> half of the integers of the array are removed</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> arr = [3,3,3,3,5,5,5,2,2,7] <strong>Output:</strong> 2 <strong>Explanation:</strong> 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 a size greater than half of the size of the old array. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> arr = [7,7,7,7,7,7] <strong>Output:</strong> 1 <strong>Explanation:</strong> The only possible set you can choose is {7}. This will make the new array empty. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= arr.length &lt;= 10<sup>5</sup></code></li> <li><code>arr.length</code> is even.</li> <li><code>1 &lt;= arr[i] &lt;= 10<sup>5</sup></code></li> </ul>
1
{ "code": "class Solution {\npublic:\n int minSetSize(vector<int>& arr) {\n unordered_map<int, int> numFreqs;\n for (const int& num: arr) {\n numFreqs[num] += 1;\n }\n \n vector<int> freqs; // we use it because if insert the heap step by step is O(nlogn)\n for (const pair<int, int>& numFreq: numFreqs) {\n freqs.push_back(numFreq.second);\n }\n \n priority_queue<int> maxFreqsHeap(freqs.begin(), freqs.end()); // O(n)\n \n int currentSize = arr.size();\n int numberOfOperation = 0;\n while (currentSize > arr.size() / 2) {\n currentSize -= maxFreqsHeap.top();\n maxFreqsHeap.pop();\n numberOfOperation += 1;\n }\n \n return numberOfOperation;\n }\n};", "memory": "80606" }
1,464
<p>You are given an integer array <code>arr</code>. You can choose a set of integers and remove all the occurrences of these integers in the array.</p> <p>Return <em>the minimum size of the set so that <strong>at least</strong> half of the integers of the array are removed</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> arr = [3,3,3,3,5,5,5,2,2,7] <strong>Output:</strong> 2 <strong>Explanation:</strong> 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 a size greater than half of the size of the old array. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> arr = [7,7,7,7,7,7] <strong>Output:</strong> 1 <strong>Explanation:</strong> The only possible set you can choose is {7}. This will make the new array empty. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= arr.length &lt;= 10<sup>5</sup></code></li> <li><code>arr.length</code> is even.</li> <li><code>1 &lt;= arr[i] &lt;= 10<sup>5</sup></code></li> </ul>
1
{ "code": "\n\nclass Solution {\npublic:\n int minSetSize(vector<int>& arr) {\n map<int,int>mp;\n priority_queue<int>maxHeap;\n int target = arr.size() / 2; \n int removed = 0;\n int setSize = 0;\n \n for(auto&num : arr){\n mp[num]++;\n }\n \n for (auto&x : mp){\n maxHeap.push(x.second);\n }\n \n while(removed < target){\n int freq = maxHeap.top();\n maxHeap.pop();\n removed+=freq;\n setSize++;\n }\n return setSize;\n }\n};", "memory": "80993" }
1,464
<p>You are given an integer array <code>arr</code>. You can choose a set of integers and remove all the occurrences of these integers in the array.</p> <p>Return <em>the minimum size of the set so that <strong>at least</strong> half of the integers of the array are removed</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> arr = [3,3,3,3,5,5,5,2,2,7] <strong>Output:</strong> 2 <strong>Explanation:</strong> 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 a size greater than half of the size of the old array. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> arr = [7,7,7,7,7,7] <strong>Output:</strong> 1 <strong>Explanation:</strong> The only possible set you can choose is {7}. This will make the new array empty. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= arr.length &lt;= 10<sup>5</sup></code></li> <li><code>arr.length</code> is even.</li> <li><code>1 &lt;= arr[i] &lt;= 10<sup>5</sup></code></li> </ul>
1
{ "code": "class Solution {\npublic:\n int minSetSize(vector<int>& arr) {\n int n=arr.size();\n int k=n;\n int t=n/2;\n int p=0;\n vector<int>ans;\n map<int,int>mp;\n for(auto e:arr)\n {\n mp[e]++;\n } \n priority_queue<int> q;\n\t\t\tfor(auto i:mp) q.push(i.second);\n\t\t\tint count=0;\n\t\t\twhile(k>n/2){\n\t\t\t\tk-=q.top();\n\t\t\t\tcount++;\n\t\t\t\tq.pop();\n\t\t\t}\n\t\t\treturn count;\n \n }\n};", "memory": "81381" }
1,464
<p>You are given an integer array <code>arr</code>. You can choose a set of integers and remove all the occurrences of these integers in the array.</p> <p>Return <em>the minimum size of the set so that <strong>at least</strong> half of the integers of the array are removed</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> arr = [3,3,3,3,5,5,5,2,2,7] <strong>Output:</strong> 2 <strong>Explanation:</strong> 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 a size greater than half of the size of the old array. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> arr = [7,7,7,7,7,7] <strong>Output:</strong> 1 <strong>Explanation:</strong> The only possible set you can choose is {7}. This will make the new array empty. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= arr.length &lt;= 10<sup>5</sup></code></li> <li><code>arr.length</code> is even.</li> <li><code>1 &lt;= arr[i] &lt;= 10<sup>5</sup></code></li> </ul>
1
{ "code": "\n\nclass Solution {\npublic:\n int minSetSize(vector<int>& arr) {\n map<int,int>mp;\n priority_queue<int> maxHeap;\n int target = arr.size() / 2; // We need to remove at least half of the elements\n int removed = 0;\n int setSize = 0;\n // Count the frequency of each element\n for(auto&num : arr){\n mp[num]++;\n }\n // Push all frequencies into the max heap\n for (auto&x : mp){\n maxHeap.push(x.second);\n }\n // Remove elements until we've removed at least half\n while(removed < target){\n int freq = maxHeap.top();\n maxHeap.pop();\n removed+=freq;\n setSize++;\n }\n return setSize;\n }\n};", "memory": "81381" }
1,464
<p>You are given an integer array <code>arr</code>. You can choose a set of integers and remove all the occurrences of these integers in the array.</p> <p>Return <em>the minimum size of the set so that <strong>at least</strong> half of the integers of the array are removed</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> arr = [3,3,3,3,5,5,5,2,2,7] <strong>Output:</strong> 2 <strong>Explanation:</strong> 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 a size greater than half of the size of the old array. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> arr = [7,7,7,7,7,7] <strong>Output:</strong> 1 <strong>Explanation:</strong> The only possible set you can choose is {7}. This will make the new array empty. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= arr.length &lt;= 10<sup>5</sup></code></li> <li><code>arr.length</code> is even.</li> <li><code>1 &lt;= arr[i] &lt;= 10<sup>5</sup></code></li> </ul>
1
{ "code": "class Solution {\npublic:\n int minSetSize(vector<int>& arr) {\n map<int,int>mp;\n priority_queue<int>maxHeap;\n int target=arr.size()/2;\n int removed=0;\n int setSize=0;\n for(auto &num:arr)\n mp[num]++;\n for(auto &x:mp)\n {\n maxHeap.push(x.second);\n }\n while(removed<target)\n {\n int freq=maxHeap.top();\n maxHeap.pop();\n removed+=freq;\n setSize++;\n }\n return setSize;\n }\n};", "memory": "81768" }
1,464
<p>You are given an integer array <code>arr</code>. You can choose a set of integers and remove all the occurrences of these integers in the array.</p> <p>Return <em>the minimum size of the set so that <strong>at least</strong> half of the integers of the array are removed</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> arr = [3,3,3,3,5,5,5,2,2,7] <strong>Output:</strong> 2 <strong>Explanation:</strong> 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 a size greater than half of the size of the old array. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> arr = [7,7,7,7,7,7] <strong>Output:</strong> 1 <strong>Explanation:</strong> The only possible set you can choose is {7}. This will make the new array empty. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= arr.length &lt;= 10<sup>5</sup></code></li> <li><code>arr.length</code> is even.</li> <li><code>1 &lt;= arr[i] &lt;= 10<sup>5</sup></code></li> </ul>
1
{ "code": "class Solution {\npublic:\n int minSetSize(vector<int>& arr) \n {\n std::map<int, int> numCount;\n\n for(auto i:arr)\n {\n numCount[i]++;\n }\n\n std::priority_queue<int> maxHeap;\n\n for(auto i:numCount) \n {\n maxHeap.push(i.second);\n } \n\n int halfSize = arr.size()/2;\n int sizeRemovedFromHeap = 0;\n int numbersCompared = 0;\n\n while(sizeRemovedFromHeap < halfSize)\n {\n sizeRemovedFromHeap +=maxHeap.top();\n maxHeap.pop();\n numbersCompared++;\n }\n\n return numbersCompared;\n }\n};", "memory": "81768" }
1,464
<p>You are given an integer array <code>arr</code>. You can choose a set of integers and remove all the occurrences of these integers in the array.</p> <p>Return <em>the minimum size of the set so that <strong>at least</strong> half of the integers of the array are removed</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> arr = [3,3,3,3,5,5,5,2,2,7] <strong>Output:</strong> 2 <strong>Explanation:</strong> 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 a size greater than half of the size of the old array. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> arr = [7,7,7,7,7,7] <strong>Output:</strong> 1 <strong>Explanation:</strong> The only possible set you can choose is {7}. This will make the new array empty. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= arr.length &lt;= 10<sup>5</sup></code></li> <li><code>arr.length</code> is even.</li> <li><code>1 &lt;= arr[i] &lt;= 10<sup>5</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n int minSetSize(vector<int>& arr) {\n\n int ans = arr.size();\n int n = arr.size();\n map<int,int> mp;\n for(int i =0;i<arr.size();i++){\n mp[arr[i]]++;\n }\n\n vector<int> freq;\n for(auto it = mp.begin();it!=mp.end();it++){\n freq.push_back(it->second);\n }\n sort(freq.begin(),freq.end());\n\n int sum = n/2;\n int count = 0;\n int i = freq.size()-1;\n while(i>=0 && sum >0){\n sum = sum - freq[i--];\n count++;\n }\n\n return count;\n \n }\n};\n/**\n\n[3,3,3,3,5,5,5,2,2,7]\n\n2 - 2\n3 - 4\n5 - 3\n7 - 1\n\n\n\n**/", "memory": "82156" }
1,464
<p>You are given an integer array <code>arr</code>. You can choose a set of integers and remove all the occurrences of these integers in the array.</p> <p>Return <em>the minimum size of the set so that <strong>at least</strong> half of the integers of the array are removed</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> arr = [3,3,3,3,5,5,5,2,2,7] <strong>Output:</strong> 2 <strong>Explanation:</strong> 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 a size greater than half of the size of the old array. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> arr = [7,7,7,7,7,7] <strong>Output:</strong> 1 <strong>Explanation:</strong> The only possible set you can choose is {7}. This will make the new array empty. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= arr.length &lt;= 10<sup>5</sup></code></li> <li><code>arr.length</code> is even.</li> <li><code>1 &lt;= arr[i] &lt;= 10<sup>5</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n int minSetSize(vector<int>& arr) {\n \n int size = arr.size();\n\n map<int,int> mpp;\n\n for( int i = 0 ; i < size ; i++ )\n {\n mpp[ arr[i] ]++;\n }\n\n priority_queue<int> pr;\n\n for( auto it : mpp )\n {\n pr.push( it.second );\n }\n \n int set_size = 0;\n int remove = 0;\n int target = size / 2;\n\n while( remove < target )\n {\n int freq = pr.top();\n\n pr.pop();\n\n remove = remove + freq;\n\n set_size ++;\n }\n\n return set_size;\n }\n};", "memory": "82543" }
1,464
<p>You are given an integer array <code>arr</code>. You can choose a set of integers and remove all the occurrences of these integers in the array.</p> <p>Return <em>the minimum size of the set so that <strong>at least</strong> half of the integers of the array are removed</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> arr = [3,3,3,3,5,5,5,2,2,7] <strong>Output:</strong> 2 <strong>Explanation:</strong> 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 a size greater than half of the size of the old array. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> arr = [7,7,7,7,7,7] <strong>Output:</strong> 1 <strong>Explanation:</strong> The only possible set you can choose is {7}. This will make the new array empty. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= arr.length &lt;= 10<sup>5</sup></code></li> <li><code>arr.length</code> is even.</li> <li><code>1 &lt;= arr[i] &lt;= 10<sup>5</sup></code></li> </ul>
2
{ "code": "#pragma GCC target(\"avx, mmx, sse2, sse3, sse4\")\n\nauto disableSync = [](void) noexcept -> int\n{\n std::ios::sync_with_stdio(false);\n std::cin.tie(nullptr);\n std::cout.tie(nullptr);\n return 0;\n}();\n\nclass Solution final\n{\npublic:\n int minSetSize(const std::vector<int>& nums) const noexcept\n {\n std::unordered_map<int, int> counter;\n for (const int num : nums)\n ++counter[num];\n std::vector<int> counts;\n counts.reserve(counter.size() + 4);\n for (const auto& [_, count] : counter)\n counts.push_back(count);\n std::sort(counts.rbegin(), counts.rend());\n const int halfSize = nums.size() >> 1;\n int nbRemoved = 0, nbSets = 1;\n for (auto it = counts.cbegin(); true; ++it, ++nbSets)\n if ((nbRemoved += *it) >= halfSize)\n return nbSets;\n return INT_MAX; \n }\n};", "memory": "82931" }
1,464
<p>You are given an integer array <code>arr</code>. You can choose a set of integers and remove all the occurrences of these integers in the array.</p> <p>Return <em>the minimum size of the set so that <strong>at least</strong> half of the integers of the array are removed</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> arr = [3,3,3,3,5,5,5,2,2,7] <strong>Output:</strong> 2 <strong>Explanation:</strong> 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 a size greater than half of the size of the old array. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> arr = [7,7,7,7,7,7] <strong>Output:</strong> 1 <strong>Explanation:</strong> The only possible set you can choose is {7}. This will make the new array empty. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= arr.length &lt;= 10<sup>5</sup></code></li> <li><code>arr.length</code> is even.</li> <li><code>1 &lt;= arr[i] &lt;= 10<sup>5</sup></code></li> </ul>
2
{ "code": "#pragma GCC target(\"avx, mmx, sse2, sse3, sse4\")\n\nauto disableSync = [](void) noexcept -> int\n{\n std::ios::sync_with_stdio(false);\n std::cin.tie(nullptr);\n std::cout.tie(nullptr);\n return 0;\n}();\n\nclass Solution final\n{\npublic:\n int minSetSize(const std::vector<int>& nums) const noexcept\n {\n std::unordered_map<int, int> counter;\n for (const int num : nums)\n ++counter[num];\n std::vector<int> counts;\n counts.reserve(counter.size() + 4);\n for (const auto& [_, count] : counter)\n counts.push_back(count);\n std::sort(counts.rbegin(), counts.rend());\n const int halfSize = nums.size() >> 1;\n int nbRemoved = 0, nbSets = 1;\n for (auto it = counts.cbegin(); true; ++it, ++nbSets)\n if ((nbRemoved += *it) >= halfSize)\n return nbSets;\n return INT_MAX; \n }\n};", "memory": "82931" }
1,464
<p>You are given an integer array <code>arr</code>. You can choose a set of integers and remove all the occurrences of these integers in the array.</p> <p>Return <em>the minimum size of the set so that <strong>at least</strong> half of the integers of the array are removed</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> arr = [3,3,3,3,5,5,5,2,2,7] <strong>Output:</strong> 2 <strong>Explanation:</strong> 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 a size greater than half of the size of the old array. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> arr = [7,7,7,7,7,7] <strong>Output:</strong> 1 <strong>Explanation:</strong> The only possible set you can choose is {7}. This will make the new array empty. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= arr.length &lt;= 10<sup>5</sup></code></li> <li><code>arr.length</code> is even.</li> <li><code>1 &lt;= arr[i] &lt;= 10<sup>5</sup></code></li> </ul>
2
{ "code": "#pragma GCC target(\"avx, mmx, sse2, sse3, sse4\")\n\nauto disableSync = [](void) noexcept -> int\n{\n std::ios::sync_with_stdio(false);\n std::cin.tie(nullptr);\n std::cout.tie(nullptr);\n return 0;\n}();\n\nclass Solution final\n{\npublic:\n int minSetSize(const std::vector<int>& nums) const noexcept\n {\n std::unordered_map<int, int> counter;\n for (const int num : nums)\n ++counter[num];\n std::vector<int> counts;\n counts.reserve(counter.size() + 4);\n for (const auto& [_, count] : counter)\n counts.push_back(count);\n std::sort(counts.rbegin(), counts.rend());\n int halfSize = nums.size() >> 1;\n for (auto it = counts.cbegin(); true; ++it)\n if ((halfSize -= *it) <= 0)\n return it - counts.begin() + 1;\n return INT_MAX; \n }\n};", "memory": "83318" }
1,464
<p>You are given an integer array <code>arr</code>. You can choose a set of integers and remove all the occurrences of these integers in the array.</p> <p>Return <em>the minimum size of the set so that <strong>at least</strong> half of the integers of the array are removed</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> arr = [3,3,3,3,5,5,5,2,2,7] <strong>Output:</strong> 2 <strong>Explanation:</strong> 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 a size greater than half of the size of the old array. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> arr = [7,7,7,7,7,7] <strong>Output:</strong> 1 <strong>Explanation:</strong> The only possible set you can choose is {7}. This will make the new array empty. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= arr.length &lt;= 10<sup>5</sup></code></li> <li><code>arr.length</code> is even.</li> <li><code>1 &lt;= arr[i] &lt;= 10<sup>5</sup></code></li> </ul>
2
{ "code": "#pragma GCC target(\"avx, mmx, sse2, sse3, sse4\")\n\nauto disableSync = [](void) noexcept -> int\n{\n std::ios::sync_with_stdio(false);\n std::cin.tie(nullptr);\n std::cout.tie(nullptr);\n return 0;\n}();\n\nclass Solution final\n{\npublic:\n int minSetSize(const std::vector<int>& nums) const noexcept\n {\n std::unordered_map<int, int> counter;\n for (const int num : nums)\n ++counter[num];\n std::vector<int> counts;\n counts.reserve(counter.size() + 4);\n for (const auto& [_, count] : counter)\n counts.push_back(count);\n std::sort(counts.rbegin(), counts.rend());\n int halfSize = nums.size() >> 1;\n for (auto it = counts.cbegin(); true; ++it)\n if ((halfSize -= *it) <= 0)\n return it - counts.begin() + 1;\n return INT_MAX; \n }\n};", "memory": "83318" }
1,464
<p>You are given an integer array <code>arr</code>. You can choose a set of integers and remove all the occurrences of these integers in the array.</p> <p>Return <em>the minimum size of the set so that <strong>at least</strong> half of the integers of the array are removed</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> arr = [3,3,3,3,5,5,5,2,2,7] <strong>Output:</strong> 2 <strong>Explanation:</strong> 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 a size greater than half of the size of the old array. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> arr = [7,7,7,7,7,7] <strong>Output:</strong> 1 <strong>Explanation:</strong> The only possible set you can choose is {7}. This will make the new array empty. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= arr.length &lt;= 10<sup>5</sup></code></li> <li><code>arr.length</code> is even.</li> <li><code>1 &lt;= arr[i] &lt;= 10<sup>5</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n int minSetSize(vector<int>& arr) {\n unordered_map<int,int> mp;\n for(auto x : arr){\n mp[x]++;\n }\n priority_queue<pair<int,int> > pq;\n for(auto x : mp){\n pq.push({x.second,x.first});\n }\n int n = arr.size(), res = 0;\n while(pq.size() > 0 and n > arr.size()/2){\n n -= pq.top().first;\n pq.pop();\n res++;\n }\n return res;\n }\n};", "memory": "83706" }
1,464
<p>You are given an integer array <code>arr</code>. You can choose a set of integers and remove all the occurrences of these integers in the array.</p> <p>Return <em>the minimum size of the set so that <strong>at least</strong> half of the integers of the array are removed</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> arr = [3,3,3,3,5,5,5,2,2,7] <strong>Output:</strong> 2 <strong>Explanation:</strong> 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 a size greater than half of the size of the old array. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> arr = [7,7,7,7,7,7] <strong>Output:</strong> 1 <strong>Explanation:</strong> The only possible set you can choose is {7}. This will make the new array empty. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= arr.length &lt;= 10<sup>5</sup></code></li> <li><code>arr.length</code> is even.</li> <li><code>1 &lt;= arr[i] &lt;= 10<sup>5</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n int minSetSize(vector<int>& arr) {\n std::unordered_map<int, int> freq;\n for(auto& i: arr){\n freq[i]++;\n }\n\n std::priority_queue<pair<int,int>> q;\n for(auto& x: freq){\n q.push({x.second, x.first});\n }\n\n int half_size = arr.size() / 2;\n int curr_size = arr.size();\n int n = 0;\n while(curr_size > half_size){\n auto x = q.top();\n q.pop();\n\n curr_size -= x.first;\n n++;\n }\n\n return n;\n }\n};", "memory": "84093" }
1,464
<p>You are given an integer array <code>arr</code>. You can choose a set of integers and remove all the occurrences of these integers in the array.</p> <p>Return <em>the minimum size of the set so that <strong>at least</strong> half of the integers of the array are removed</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> arr = [3,3,3,3,5,5,5,2,2,7] <strong>Output:</strong> 2 <strong>Explanation:</strong> 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 a size greater than half of the size of the old array. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> arr = [7,7,7,7,7,7] <strong>Output:</strong> 1 <strong>Explanation:</strong> The only possible set you can choose is {7}. This will make the new array empty. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= arr.length &lt;= 10<sup>5</sup></code></li> <li><code>arr.length</code> is even.</li> <li><code>1 &lt;= arr[i] &lt;= 10<sup>5</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n int minSetSize(vector<int>& arr) {\n int n=arr.size();\n unordered_map<int,int> map;\n for(auto it: arr)\n map[it]++;\n priority_queue<pair<int,int>> pq;\n for(auto it: map)\n {\n pq.push({it.second,it.first});\n }\n int s=0;\n int ans=-1;\n int size=n;\n while(!pq.empty())\n {\n int freq=pq.top().first;\n int ele=pq.top().second;\n pq.pop();\n s++;\n size-=freq;\n if(size<=(n/2))\n {\n ans=s;\n break;\n }\n }\n return ans;\n }\n};", "memory": "84481" }
1,464
<p>You are given an integer array <code>arr</code>. You can choose a set of integers and remove all the occurrences of these integers in the array.</p> <p>Return <em>the minimum size of the set so that <strong>at least</strong> half of the integers of the array are removed</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> arr = [3,3,3,3,5,5,5,2,2,7] <strong>Output:</strong> 2 <strong>Explanation:</strong> 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 a size greater than half of the size of the old array. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> arr = [7,7,7,7,7,7] <strong>Output:</strong> 1 <strong>Explanation:</strong> The only possible set you can choose is {7}. This will make the new array empty. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= arr.length &lt;= 10<sup>5</sup></code></li> <li><code>arr.length</code> is even.</li> <li><code>1 &lt;= arr[i] &lt;= 10<sup>5</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n int minSetSize(vector<int>& arr) {\n unordered_map<int,int> mp;\n for(int i=0;i<arr.size();i++)\n mp[arr[i]]++;\n priority_queue<pair<int,int>> pq;\n for(auto it:mp)\n pq.push({it.second,it.first});\n int val=0,cnt=0;\n while(!pq.empty() && val < arr.size()/2)\n {\n val += pq.top().first;\n cnt++;\n pq.pop();\n }\n return cnt;\n }\n};", "memory": "84868" }
1,464
<p>You are given an integer array <code>arr</code>. You can choose a set of integers and remove all the occurrences of these integers in the array.</p> <p>Return <em>the minimum size of the set so that <strong>at least</strong> half of the integers of the array are removed</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> arr = [3,3,3,3,5,5,5,2,2,7] <strong>Output:</strong> 2 <strong>Explanation:</strong> 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 a size greater than half of the size of the old array. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> arr = [7,7,7,7,7,7] <strong>Output:</strong> 1 <strong>Explanation:</strong> The only possible set you can choose is {7}. This will make the new array empty. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= arr.length &lt;= 10<sup>5</sup></code></li> <li><code>arr.length</code> is even.</li> <li><code>1 &lt;= arr[i] &lt;= 10<sup>5</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n int minSetSize(vector<int>& arr) {\n int n =arr.size();\n unordered_map<int,int>mpp;\n for(int i=0;i<n;i++){\n mpp[arr[i]]++;\n }\n priority_queue<pair<int,int>>maxH;\n for(auto it:mpp){\n maxH.push({it.second,it.first});\n }\n int count=0;\n int removed = 0;\n while(!maxH.empty() && removed<n/2){\n auto [freq,val] = maxH.top();\n maxH.pop();\n removed+=freq;\n count++;\n }\n return count;\n }\n};", "memory": "84868" }
1,464
<p>You are given an integer array <code>arr</code>. You can choose a set of integers and remove all the occurrences of these integers in the array.</p> <p>Return <em>the minimum size of the set so that <strong>at least</strong> half of the integers of the array are removed</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> arr = [3,3,3,3,5,5,5,2,2,7] <strong>Output:</strong> 2 <strong>Explanation:</strong> 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 a size greater than half of the size of the old array. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> arr = [7,7,7,7,7,7] <strong>Output:</strong> 1 <strong>Explanation:</strong> The only possible set you can choose is {7}. This will make the new array empty. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= arr.length &lt;= 10<sup>5</sup></code></li> <li><code>arr.length</code> is even.</li> <li><code>1 &lt;= arr[i] &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int minSetSize(vector<int>& arr) {\n unordered_map<int,int> mp;\n for(auto it:arr) mp[it]++;\n vector<int> val;\n int n=arr.size()/2;\n for(auto it:mp){\n val.push_back(it.second);\n }\n\n\n sort(val.rbegin(),val.rend());\n int len=0;\n\n for(auto it:val){\n int freq=it;\n if(n>0){\n n-=freq;\n len++;\n }\n }\n\n return len;\n \n }\n};", "memory": "85256" }
1,464
<p>You are given an integer array <code>arr</code>. You can choose a set of integers and remove all the occurrences of these integers in the array.</p> <p>Return <em>the minimum size of the set so that <strong>at least</strong> half of the integers of the array are removed</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> arr = [3,3,3,3,5,5,5,2,2,7] <strong>Output:</strong> 2 <strong>Explanation:</strong> 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 a size greater than half of the size of the old array. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> arr = [7,7,7,7,7,7] <strong>Output:</strong> 1 <strong>Explanation:</strong> The only possible set you can choose is {7}. This will make the new array empty. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= arr.length &lt;= 10<sup>5</sup></code></li> <li><code>arr.length</code> is even.</li> <li><code>1 &lt;= arr[i] &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int minSetSize(vector<int>& arr) {\n int n = arr.size();\n unordered_map<int, int> mp;\n for(auto num: arr) {\n mp[num]++;\n }\n vector<int> freq;\n for(auto& it:mp) {\n freq.push_back(it.second);\n }\n sort(freq.rbegin(), freq.rend());\n\n int remove = 0, setSize = 0;\n for(auto ch:freq) {\n remove += ch;\n setSize++;\n if(remove >= n/2){\n break;\n }\n }\n return setSize;\n }\n};", "memory": "85256" }
1,464
<p>You are given an integer array <code>arr</code>. You can choose a set of integers and remove all the occurrences of these integers in the array.</p> <p>Return <em>the minimum size of the set so that <strong>at least</strong> half of the integers of the array are removed</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> arr = [3,3,3,3,5,5,5,2,2,7] <strong>Output:</strong> 2 <strong>Explanation:</strong> 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 a size greater than half of the size of the old array. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> arr = [7,7,7,7,7,7] <strong>Output:</strong> 1 <strong>Explanation:</strong> The only possible set you can choose is {7}. This will make the new array empty. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= arr.length &lt;= 10<sup>5</sup></code></li> <li><code>arr.length</code> is even.</li> <li><code>1 &lt;= arr[i] &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int minSetSize(vector<int>& arr) {\n std::unordered_map<int, int> nums_count;\n for (size_t i = 0; i < arr.size(); i++) {\n nums_count[arr[i]]++;\n }\n\n std::vector<int> counts;\n for (auto it = nums_count.begin(); it != nums_count.end(); it++) {\n counts.push_back(it->second);\n }\n\n std::sort(counts.rbegin(), counts.rend());\n\n int i = 0;\n for (size_t current_size = arr.size(); i < counts.size() && current_size > arr.size() / 2; i++) {\n current_size -= counts[i];\n }\n\n return i;\n }\n};", "memory": "85643" }
1,464
<p>You are given an integer array <code>arr</code>. You can choose a set of integers and remove all the occurrences of these integers in the array.</p> <p>Return <em>the minimum size of the set so that <strong>at least</strong> half of the integers of the array are removed</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> arr = [3,3,3,3,5,5,5,2,2,7] <strong>Output:</strong> 2 <strong>Explanation:</strong> 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 a size greater than half of the size of the old array. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> arr = [7,7,7,7,7,7] <strong>Output:</strong> 1 <strong>Explanation:</strong> The only possible set you can choose is {7}. This will make the new array empty. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= arr.length &lt;= 10<sup>5</sup></code></li> <li><code>arr.length</code> is even.</li> <li><code>1 &lt;= arr[i] &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int minSetSize(vector<int>& arr) {\n unordered_map<int, int> hash; // value, count\n for (int v : arr) {\n hash[v]++;\n }\n /* now we need to sort by the count\n * and btw we don't need the values themselve */\n vector<int> vec;\n for(auto [value, count] : hash) {\n vec.push_back(count);\n }\n sort(vec.rbegin(), vec.rend());\n int total = arr.size();\n for (int i = 0 ; i < vec.size(); i++) {\n total -= vec[i];\n if (total <= arr.size()/2)\n return i+1;\n }\n return -1;\n }\n};", "memory": "85643" }
1,464
<p>You are given an integer array <code>arr</code>. You can choose a set of integers and remove all the occurrences of these integers in the array.</p> <p>Return <em>the minimum size of the set so that <strong>at least</strong> half of the integers of the array are removed</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> arr = [3,3,3,3,5,5,5,2,2,7] <strong>Output:</strong> 2 <strong>Explanation:</strong> 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 a size greater than half of the size of the old array. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> arr = [7,7,7,7,7,7] <strong>Output:</strong> 1 <strong>Explanation:</strong> The only possible set you can choose is {7}. This will make the new array empty. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= arr.length &lt;= 10<sup>5</sup></code></li> <li><code>arr.length</code> is even.</li> <li><code>1 &lt;= arr[i] &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n struct Pair\n {\n int number;\n int frequency;\n };\n int minSetSize(vector<int>& arr) {\n int original_size = arr.size();\n map<int,int> frequency;\n for(int i:arr)\n {\n frequency[i]++;\n }\n vector<Pair> pairs;\n Pair pair;\n for(auto& i:frequency)\n {\n pair.number = i.first;\n pair.frequency = i.second;\n pairs.push_back(pair);\n }\n sort(pairs.begin(),pairs.end(),[](Pair& a,Pair& b)\n {\n return a.frequency>b.frequency;\n });\n int current_size = original_size;\n int answer = 0;\n for(auto& pair:pairs)\n {\n if(current_size<=original_size/2)\n {\n break;\n }\n else\n {\n answer++;\n current_size-=pair.frequency;\n }\n }\n return answer;\n\n\n \n\n }\n};", "memory": "86031" }
1,464
<p>You are given an integer array <code>arr</code>. You can choose a set of integers and remove all the occurrences of these integers in the array.</p> <p>Return <em>the minimum size of the set so that <strong>at least</strong> half of the integers of the array are removed</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> arr = [3,3,3,3,5,5,5,2,2,7] <strong>Output:</strong> 2 <strong>Explanation:</strong> 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 a size greater than half of the size of the old array. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> arr = [7,7,7,7,7,7] <strong>Output:</strong> 1 <strong>Explanation:</strong> The only possible set you can choose is {7}. This will make the new array empty. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= arr.length &lt;= 10<sup>5</sup></code></li> <li><code>arr.length</code> is even.</li> <li><code>1 &lt;= arr[i] &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int minSetSize(vector<int>& arr) {\n int n=arr.size();\n priority_queue<pair<int,int>>pq;\n map<int,int>mpp;\n for(auto it:arr) mpp[it]++;\n for(auto it:mpp){\n pq.push({it.second,it.first});\n } \n int count=0;\n int ans=0;\n while(!pq.empty()){\n auto it=pq.top();\n pq.pop();\n int node=it.first;\n count+=node;\n ans++;\n if(count>=n/2)return ans;\n }\n return ans;\n }\n};", "memory": "86418" }
1,464
<p>You are given an integer array <code>arr</code>. You can choose a set of integers and remove all the occurrences of these integers in the array.</p> <p>Return <em>the minimum size of the set so that <strong>at least</strong> half of the integers of the array are removed</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> arr = [3,3,3,3,5,5,5,2,2,7] <strong>Output:</strong> 2 <strong>Explanation:</strong> 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 a size greater than half of the size of the old array. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> arr = [7,7,7,7,7,7] <strong>Output:</strong> 1 <strong>Explanation:</strong> The only possible set you can choose is {7}. This will make the new array empty. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= arr.length &lt;= 10<sup>5</sup></code></li> <li><code>arr.length</code> is even.</li> <li><code>1 &lt;= arr[i] &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int minSetSize(vector<int>& arr) {\n int n=arr.size();\n map<int,int>mp;\n for(auto x:arr) mp[x]++;\n priority_queue<pair<int,int>>pq;\n for(auto x:mp){\n pq.push({x.second,x.first});\n }\n\n int ans=0,curSize=n;\n while(!pq.empty()){\n int cnt=pq.top().first;\n curSize-=cnt;\n ans++;\n if(curSize<=n/2) return ans;\n pq.pop();\n }\n return ans;\n }\n};", "memory": "86806" }
1,464
<p>You are given an integer array <code>arr</code>. You can choose a set of integers and remove all the occurrences of these integers in the array.</p> <p>Return <em>the minimum size of the set so that <strong>at least</strong> half of the integers of the array are removed</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> arr = [3,3,3,3,5,5,5,2,2,7] <strong>Output:</strong> 2 <strong>Explanation:</strong> 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 a size greater than half of the size of the old array. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> arr = [7,7,7,7,7,7] <strong>Output:</strong> 1 <strong>Explanation:</strong> The only possible set you can choose is {7}. This will make the new array empty. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= arr.length &lt;= 10<sup>5</sup></code></li> <li><code>arr.length</code> is even.</li> <li><code>1 &lt;= arr[i] &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int minSetSize(vector<int>& arr) {\n unordered_map<long,long>mpp;\n \n for(int i=0;i<arr.size();i++){\n mpp[arr[i]]++;\n }\n \n int count=1;\n vector<int> v;\n \n for(auto x:mpp){\n v.push_back(x.second);\n }\n \n sort(v.begin(),v.end());\n \n if(v.size()==1){\n return count;\n }\n \n int n=arr.size();\n int sum=0;\n \n for(int i=v.size()-1;i>=0;i--){\n sum=sum+v[i];\n if(sum>=n/2){\n return count ;\n }\n else{\n count++;\n }\n }\n \n return count;\n }\n};\n\n", "memory": "86806" }
1,464
<p>You are given an integer array <code>arr</code>. You can choose a set of integers and remove all the occurrences of these integers in the array.</p> <p>Return <em>the minimum size of the set so that <strong>at least</strong> half of the integers of the array are removed</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> arr = [3,3,3,3,5,5,5,2,2,7] <strong>Output:</strong> 2 <strong>Explanation:</strong> 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 a size greater than half of the size of the old array. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> arr = [7,7,7,7,7,7] <strong>Output:</strong> 1 <strong>Explanation:</strong> The only possible set you can choose is {7}. This will make the new array empty. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= arr.length &lt;= 10<sup>5</sup></code></li> <li><code>arr.length</code> is even.</li> <li><code>1 &lt;= arr[i] &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int minSetSize(vector<int>& arr) {\n int n=arr.size();\n map<int,int>mp;\n for(int i=0;i<n;i++){\n mp[arr[i]]++;\n }\n vector<int>res;\n for(auto &x:mp){\n res.push_back(x.second);\n }\n sort(res.rbegin(),res.rend());\n int sum=0;\n int cnt=0;\n for(int i=0;i<res.size();i++){\n sum+=res[i];\n cnt++;\n if(sum>=n/2){\n break;\n }\n }\n return cnt;\n\n \n }\n};", "memory": "87193" }