Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 70f3038

Browse files
feat: update solution to lc problem: No.1338 (doocs#3860)
1 parent 02d60b0 commit 70f3038

File tree

4 files changed

+29
-34
lines changed

4 files changed

+29
-34
lines changed

‎solution/1300-1399/1338.Reduce Array Size to The Half/README.md‎

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,9 @@ tags:
6464

6565
### 方法一:计数 + 排序
6666

67-
我们可以用哈希表或数组 $cnt$ 统计数组 $arr$ 中每个数字出现的次数,然后将 $cnt$ 中的数字从大到小排序,从大到小遍历 $cnt,ドル每次遍历将当前数字 $x$ 加入答案,并将 $m$ 加上 $x,ドル如果 $m \geq \frac{n}{2},ドル则返回答案。
67+
我们可以用哈希表或数组 $cnt$ 统计数组 $\textit{arr}$ 中每个数字出现的次数,然后将 $cnt$ 中的数字从大到小排序,从大到小遍历 $\textit{cnt},ドル每次遍历将当前数字 $x$ 加入答案,并将 $m$ 加上 $x,ドル如果 $m \geq \frac{n}{2},ドル则返回答案。
6868

69-
时间复杂度 $O(n \times \log n),ドル空间复杂度 $O(n)$。其中 $n$ 为数组 $arr$ 的长度。
69+
时间复杂度 $O(n \times \log n),ドル空间复杂度 $O(n)$。其中 $n$ 为数组 $\textit{arr}$ 的长度。
7070

7171
<!-- tabs:start -->
7272

@@ -120,7 +120,7 @@ class Solution {
120120
class Solution {
121121
public:
122122
int minSetSize(vector<int>& arr) {
123-
int mx = *max_element(arr.begin(), arr.end());
123+
int mx = ranges::max(arr);
124124
int cnt[mx + 1];
125125
memset(cnt, 0, sizeof(cnt));
126126
for (int& x : arr) {
@@ -169,18 +169,15 @@ func minSetSize(arr []int) (ans int) {
169169

170170
```ts
171171
function minSetSize(arr: number[]): number {
172-
const counter = new Map<number, number>();
172+
const cnt = new Map<number, number>();
173173
for (const v of arr) {
174-
counter.set(v, (counter.get(v) ?? 0) + 1);
174+
cnt.set(v, (cnt.get(v) ?? 0) + 1);
175175
}
176-
const t = Array.from(counter.values());
177-
t.sort((a, b) => b - a);
178-
let ans = 0;
179-
let n = 0;
180-
for (const cnt of t) {
181-
n += cnt;
176+
let [ans, m] = [0, 0];
177+
for (const v of Array.from(cnt.values()).sort((a, b) => b - a)) {
178+
m += v;
182179
++ans;
183-
if (n * 2 >= arr.length) {
180+
if (m * 2 >= arr.length) {
184181
break;
185182
}
186183
}

‎solution/1300-1399/1338.Reduce Array Size to The Half/README_EN.md‎

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -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]
6060

6161
<!-- solution:start -->
6262

63-
### Solution 1
63+
### Solution 1: Counting + Sorting
64+
65+
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.
66+
67+
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}$.
6468

6569
<!-- tabs:start -->
6670

@@ -114,7 +118,7 @@ class Solution {
114118
class Solution {
115119
public:
116120
int minSetSize(vector<int>& arr) {
117-
int mx = *max_element(arr.begin(), arr.end());
121+
int mx = ranges::max(arr);
118122
int cnt[mx + 1];
119123
memset(cnt, 0, sizeof(cnt));
120124
for (int& x : arr) {
@@ -163,18 +167,15 @@ func minSetSize(arr []int) (ans int) {
163167

164168
```ts
165169
function minSetSize(arr: number[]): number {
166-
const counter = new Map<number, number>();
170+
const cnt = new Map<number, number>();
167171
for (const v of arr) {
168-
counter.set(v, (counter.get(v) ?? 0) + 1);
172+
cnt.set(v, (cnt.get(v) ?? 0) + 1);
169173
}
170-
const t = Array.from(counter.values());
171-
t.sort((a, b) => b - a);
172-
let ans = 0;
173-
let n = 0;
174-
for (const cnt of t) {
175-
n += cnt;
174+
let [ans, m] = [0, 0];
175+
for (const v of Array.from(cnt.values()).sort((a, b) => b - a)) {
176+
m += v;
176177
++ans;
177-
if (n * 2 >= arr.length) {
178+
if (m * 2 >= arr.length) {
178179
break;
179180
}
180181
}

‎solution/1300-1399/1338.Reduce Array Size to The Half/Solution.cpp‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
class Solution {
22
public:
33
int minSetSize(vector<int>& arr) {
4-
int mx = *max_element(arr.begin(), arr.end());
4+
int mx = ranges::max(arr);
55
int cnt[mx + 1];
66
memset(cnt, 0, sizeof(cnt));
77
for (int& x : arr) {
@@ -21,4 +21,4 @@ class Solution {
2121
}
2222
return ans;
2323
}
24-
};
24+
};

‎solution/1300-1399/1338.Reduce Array Size to The Half/Solution.ts‎

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
11
function minSetSize(arr: number[]): number {
2-
const counter = new Map<number, number>();
2+
const cnt = new Map<number, number>();
33
for (const v of arr) {
4-
counter.set(v, (counter.get(v) ?? 0) + 1);
4+
cnt.set(v, (cnt.get(v) ?? 0) + 1);
55
}
6-
const t = Array.from(counter.values());
7-
t.sort((a, b) => b - a);
8-
let ans = 0;
9-
let n = 0;
10-
for (const cnt of t) {
11-
n += cnt;
6+
let [ans, m] = [0, 0];
7+
for (const v of Array.from(cnt.values()).sort((a, b) => b - a)) {
8+
m += v;
129
++ans;
13-
if (n * 2 >= arr.length) {
10+
if (m * 2 >= arr.length) {
1411
break;
1512
}
1613
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /