diff --git a/solution/1300-1399/1338.Reduce Array Size to The Half/README.md b/solution/1300-1399/1338.Reduce Array Size to The Half/README.md index b0b73b6b71b1a..92d0586cf0d82 100644 --- a/solution/1300-1399/1338.Reduce Array Size to The Half/README.md +++ b/solution/1300-1399/1338.Reduce Array Size to The Half/README.md @@ -64,9 +64,9 @@ tags: ### 方法一:计数 + 排序 -我们可以用哈希表或数组 $cnt$ 统计数组 $arr$ 中每个数字出现的次数,然后将 $cnt$ 中的数字从大到小排序,从大到小遍历 $cnt$,每次遍历将当前数字 $x$ 加入答案,并将 $m$ 加上 $x$,如果 $m \geq \frac{n}{2}$,则返回答案。 +我们可以用哈希表或数组 $cnt$ 统计数组 $\textit{arr}$ 中每个数字出现的次数,然后将 $cnt$ 中的数字从大到小排序,从大到小遍历 $\textit{cnt}$,每次遍历将当前数字 $x$ 加入答案,并将 $m$ 加上 $x$,如果 $m \geq \frac{n}{2}$,则返回答案。 -时间复杂度 $O(n \times \log n)$,空间复杂度 $O(n)$。其中 $n$ 为数组 $arr$ 的长度。 +时间复杂度 $O(n \times \log n)$,空间复杂度 $O(n)$。其中 $n$ 为数组 $\textit{arr}$ 的长度。 @@ -120,7 +120,7 @@ class Solution { class Solution { public: int minSetSize(vector& arr) { - int mx = *max_element(arr.begin(), arr.end()); + int mx = ranges::max(arr); int cnt[mx + 1]; memset(cnt, 0, sizeof(cnt)); for (int& x : arr) { @@ -169,18 +169,15 @@ func minSetSize(arr []int) (ans int) { ```ts function minSetSize(arr: number[]): number { - const counter = new Map(); + const cnt = new Map(); for (const v of arr) { - counter.set(v, (counter.get(v) ?? 0) + 1); + cnt.set(v, (cnt.get(v) ?? 0) + 1); } - const t = Array.from(counter.values()); - t.sort((a, b) => b - a); - let ans = 0; - let n = 0; - for (const cnt of t) { - n += cnt; + let [ans, m] = [0, 0]; + for (const v of Array.from(cnt.values()).sort((a, b) => b - a)) { + m += v; ++ans; - if (n * 2 >= arr.length) { + if (m * 2 >= arr.length) { break; } } diff --git a/solution/1300-1399/1338.Reduce Array Size to The Half/README_EN.md b/solution/1300-1399/1338.Reduce Array Size to The Half/README_EN.md index 09b550baa01b3..c64665e41dcd1 100644 --- a/solution/1300-1399/1338.Reduce Array Size to The Half/README_EN.md +++ b/solution/1300-1399/1338.Reduce Array Size to The Half/README_EN.md @@ -60,7 +60,11 @@ Choosing set {2,7} is not possible as it will make the new array [3,3,3,3,5,5,5] -### Solution 1 +### Solution 1: Counting + Sorting + +We can use a hash table or an array $\textit{cnt}$ to count the occurrences of each number in the array $\textit{arr}$. Then, we sort the numbers in $\textit{cnt}$ in descending order. We traverse $\textit{cnt}$ from largest to smallest, adding the current number $x$ to the answer and adding $x$ to $m$. If $m \geq \frac{n}{2}$, we return the answer. + +The time complexity is $O(n \times \log n)$, and the space complexity is $O(n)$. Here, $n$ is the length of the array $\textit{arr}$. @@ -114,7 +118,7 @@ class Solution { class Solution { public: int minSetSize(vector& arr) { - int mx = *max_element(arr.begin(), arr.end()); + int mx = ranges::max(arr); int cnt[mx + 1]; memset(cnt, 0, sizeof(cnt)); for (int& x : arr) { @@ -163,18 +167,15 @@ func minSetSize(arr []int) (ans int) { ```ts function minSetSize(arr: number[]): number { - const counter = new Map(); + const cnt = new Map(); for (const v of arr) { - counter.set(v, (counter.get(v) ?? 0) + 1); + cnt.set(v, (cnt.get(v) ?? 0) + 1); } - const t = Array.from(counter.values()); - t.sort((a, b) => b - a); - let ans = 0; - let n = 0; - for (const cnt of t) { - n += cnt; + let [ans, m] = [0, 0]; + for (const v of Array.from(cnt.values()).sort((a, b) => b - a)) { + m += v; ++ans; - if (n * 2 >= arr.length) { + if (m * 2 >= arr.length) { break; } } diff --git a/solution/1300-1399/1338.Reduce Array Size to The Half/Solution.cpp b/solution/1300-1399/1338.Reduce Array Size to The Half/Solution.cpp index 02780bf9eca6e..09be70f13992c 100644 --- a/solution/1300-1399/1338.Reduce Array Size to The Half/Solution.cpp +++ b/solution/1300-1399/1338.Reduce Array Size to The Half/Solution.cpp @@ -1,7 +1,7 @@ class Solution { public: int minSetSize(vector& arr) { - int mx = *max_element(arr.begin(), arr.end()); + int mx = ranges::max(arr); int cnt[mx + 1]; memset(cnt, 0, sizeof(cnt)); for (int& x : arr) { @@ -21,4 +21,4 @@ class Solution { } return ans; } -}; \ No newline at end of file +}; diff --git a/solution/1300-1399/1338.Reduce Array Size to The Half/Solution.ts b/solution/1300-1399/1338.Reduce Array Size to The Half/Solution.ts index fadbb7232ab02..d23d6d32e02af 100644 --- a/solution/1300-1399/1338.Reduce Array Size to The Half/Solution.ts +++ b/solution/1300-1399/1338.Reduce Array Size to The Half/Solution.ts @@ -1,16 +1,13 @@ function minSetSize(arr: number[]): number { - const counter = new Map(); + const cnt = new Map(); for (const v of arr) { - counter.set(v, (counter.get(v) ?? 0) + 1); + cnt.set(v, (cnt.get(v) ?? 0) + 1); } - const t = Array.from(counter.values()); - t.sort((a, b) => b - a); - let ans = 0; - let n = 0; - for (const cnt of t) { - n += cnt; + let [ans, m] = [0, 0]; + for (const v of Array.from(cnt.values()).sort((a, b) => b - a)) { + m += v; ++ans; - if (n * 2 >= arr.length) { + if (m * 2 >= arr.length) { break; } }