Skip to content

Commit 84b9508

Browse files
committed
feat: add solutions to lc problem: No.1338
No.1338.Reduce Array Size to The Half
1 parent e4153d2 commit 84b9508

File tree

6 files changed

+179
-139
lines changed

6 files changed

+179
-139
lines changed

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

Lines changed: 63 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,11 @@
4444

4545
<!-- 这里可写通用的实现逻辑 -->
4646

47-
哈希表计数,按出现的频率倒序。
47+
**方法一:计数 + 排序**
48+
49+
我们可以用哈希表或数组 $cnt$ 统计数组 $arr$ 中每个数字出现的次数,然后将 $cnt$ 中的数字从大到小排序,从大到小遍历 $cnt$,每次遍历将当前数字 $x$ 加入答案,并将 $m$ 加上 $x$,如果 $m \geq \frac{n}{2}$,则返回答案。
50+
51+
时间复杂度 $O(n \times \log n)$,空间复杂度 $O(n)$。其中 $n$ 为数组 $arr$ 的长度。
4852

4953
<!-- tabs:start -->
5054

@@ -55,12 +59,12 @@
5559
```python
5660
class Solution:
5761
def minSetSize(self, arr: List[int]) -> int:
58-
couter = Counter(arr)
59-
ans = n = 0
60-
for _, cnt in couter.most_common():
61-
n += cnt
62+
cnt = Counter(arr)
63+
ans = m = 0
64+
for _, v in cnt.most_common():
65+
m += v
6266
ans += 1
63-
if n * 2 >= len(arr):
67+
if m * 2 >= len(arr):
6468
break
6569
return ans
6670
```
@@ -72,25 +76,26 @@ class Solution:
7276
```java
7377
class Solution {
7478
public int minSetSize(int[] arr) {
75-
Map<Integer, Integer> counter = new HashMap<>();
76-
for (int v : arr) {
77-
counter.put(v, counter.getOrDefault(v, 0) + 1);
79+
int mx = 0;
80+
for (int x : arr) {
81+
mx = Math.max(mx, x);
7882
}
79-
List<Integer> t = new ArrayList<>();
80-
for (int cnt : counter.values()) {
81-
t.add(cnt);
83+
int[] cnt = new int[mx + 1];
84+
for (int x : arr) {
85+
++cnt[x];
8286
}
83-
Collections.sort(t, Collections.reverseOrder());
87+
Arrays.sort(cnt);
8488
int ans = 0;
85-
int n = 0;
86-
for (int cnt : t) {
87-
n += cnt;
88-
++ans;
89-
if (n * 2 >= arr.length) {
90-
break;
89+
int m = 0;
90+
for (int i = mx; ; --i) {
91+
if (cnt[i] > 0) {
92+
m += cnt[i];
93+
++ans;
94+
if (m * 2 >= arr.length) {
95+
return ans;
96+
}
9197
}
9298
}
93-
return ans;
9499
}
95100
}
96101
```
@@ -101,17 +106,23 @@ class Solution {
101106
class Solution {
102107
public:
103108
int minSetSize(vector<int>& arr) {
104-
unordered_map<int, int> counter;
105-
for (int v : arr) ++counter[v];
106-
vector<int> t;
107-
for (auto& [k, v] : counter) t.push_back(v);
108-
sort(t.begin(), t.end(), greater<int>());
109+
int mx = *max_element(arr.begin(), arr.end());
110+
int cnt[mx + 1];
111+
memset(cnt, 0, sizeof(cnt));
112+
for (int& x : arr) {
113+
++cnt[x];
114+
}
115+
sort(cnt, cnt + mx + 1, greater<int>());
109116
int ans = 0;
110-
int n = 0;
111-
for (int cnt : t) {
112-
n += cnt;
113-
++ans;
114-
if (n * 2 >= arr.size()) break;
117+
int m = 0;
118+
for (int& x : cnt) {
119+
if (x) {
120+
m += x;
121+
++ans;
122+
if (m * 2 >= arr.size()) {
123+
break;
124+
}
125+
}
115126
}
116127
return ans;
117128
}
@@ -121,27 +132,32 @@ public:
121132
### **Go**
122133
123134
```go
124-
func minSetSize(arr []int) int {
125-
counter := make(map[int]int)
126-
for _, v := range arr {
127-
counter[v]++
135+
func minSetSize(arr []int) (ans int) {
136+
mx := 0
137+
for _, x := range arr {
138+
mx = max(mx, x)
128139
}
129-
var t []int
130-
for _, v := range counter {
131-
t = append(t, v)
140+
cnt := make([]int, mx+1)
141+
for _, x := range arr {
142+
cnt[x]++
132143
}
133-
sort.Slice(t, func(i, j int) bool {
134-
return t[i] > t[j]
135-
})
136-
ans, n := 0, 0
137-
for _, cnt := range t {
138-
n += cnt
139-
ans++
140-
if n*2 >= len(arr) {
141-
break
144+
sort.Ints(cnt)
145+
for i, m := mx, 0; ; i-- {
146+
if cnt[i] > 0 {
147+
m += cnt[i]
148+
ans++
149+
if m >= len(arr)/2 {
150+
return
151+
}
142152
}
143153
}
144-
return ans
154+
}
155+
156+
func max(a, b int) int {
157+
if a > b {
158+
return a
159+
}
160+
return b
145161
}
146162
```
147163

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

Lines changed: 58 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,12 @@ Choosing set {2,7} is not possible as it will make the new array [3,3,3,3,5,5,5]
4545
```python
4646
class Solution:
4747
def minSetSize(self, arr: List[int]) -> int:
48-
couter = Counter(arr)
49-
ans = n = 0
50-
for _, cnt in couter.most_common():
51-
n += cnt
48+
cnt = Counter(arr)
49+
ans = m = 0
50+
for _, v in cnt.most_common():
51+
m += v
5252
ans += 1
53-
if n * 2 >= len(arr):
53+
if m * 2 >= len(arr):
5454
break
5555
return ans
5656
```
@@ -60,25 +60,26 @@ class Solution:
6060
```java
6161
class Solution {
6262
public int minSetSize(int[] arr) {
63-
Map<Integer, Integer> counter = new HashMap<>();
64-
for (int v : arr) {
65-
counter.put(v, counter.getOrDefault(v, 0) + 1);
63+
int mx = 0;
64+
for (int x : arr) {
65+
mx = Math.max(mx, x);
6666
}
67-
List<Integer> t = new ArrayList<>();
68-
for (int cnt : counter.values()) {
69-
t.add(cnt);
67+
int[] cnt = new int[mx + 1];
68+
for (int x : arr) {
69+
++cnt[x];
7070
}
71-
Collections.sort(t, Collections.reverseOrder());
71+
Arrays.sort(cnt);
7272
int ans = 0;
73-
int n = 0;
74-
for (int cnt : t) {
75-
n += cnt;
76-
++ans;
77-
if (n * 2 >= arr.length) {
78-
break;
73+
int m = 0;
74+
for (int i = mx; ; --i) {
75+
if (cnt[i] > 0) {
76+
m += cnt[i];
77+
++ans;
78+
if (m * 2 >= arr.length) {
79+
return ans;
80+
}
7981
}
8082
}
81-
return ans;
8283
}
8384
}
8485
```
@@ -89,17 +90,23 @@ class Solution {
8990
class Solution {
9091
public:
9192
int minSetSize(vector<int>& arr) {
92-
unordered_map<int, int> counter;
93-
for (int v : arr) ++counter[v];
94-
vector<int> t;
95-
for (auto& [k, v] : counter) t.push_back(v);
96-
sort(t.begin(), t.end(), greater<int>());
93+
int mx = *max_element(arr.begin(), arr.end());
94+
int cnt[mx + 1];
95+
memset(cnt, 0, sizeof(cnt));
96+
for (int& x : arr) {
97+
++cnt[x];
98+
}
99+
sort(cnt, cnt + mx + 1, greater<int>());
97100
int ans = 0;
98-
int n = 0;
99-
for (int cnt : t) {
100-
n += cnt;
101-
++ans;
102-
if (n * 2 >= arr.size()) break;
101+
int m = 0;
102+
for (int& x : cnt) {
103+
if (x) {
104+
m += x;
105+
++ans;
106+
if (m * 2 >= arr.size()) {
107+
break;
108+
}
109+
}
103110
}
104111
return ans;
105112
}
@@ -109,27 +116,32 @@ public:
109116
### **Go**
110117
111118
```go
112-
func minSetSize(arr []int) int {
113-
counter := make(map[int]int)
114-
for _, v := range arr {
115-
counter[v]++
119+
func minSetSize(arr []int) (ans int) {
120+
mx := 0
121+
for _, x := range arr {
122+
mx = max(mx, x)
116123
}
117-
var t []int
118-
for _, v := range counter {
119-
t = append(t, v)
124+
cnt := make([]int, mx+1)
125+
for _, x := range arr {
126+
cnt[x]++
120127
}
121-
sort.Slice(t, func(i, j int) bool {
122-
return t[i] > t[j]
123-
})
124-
ans, n := 0, 0
125-
for _, cnt := range t {
126-
n += cnt
127-
ans++
128-
if n*2 >= len(arr) {
129-
break
128+
sort.Ints(cnt)
129+
for i, m := mx, 0; ; i-- {
130+
if cnt[i] > 0 {
131+
m += cnt[i]
132+
ans++
133+
if m >= len(arr)/2 {
134+
return
135+
}
130136
}
131137
}
132-
return ans
138+
}
139+
140+
func max(a, b int) int {
141+
if a > b {
142+
return a
143+
}
144+
return b
133145
}
134146
```
135147

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

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,23 @@
11
class Solution {
22
public:
33
int minSetSize(vector<int>& arr) {
4-
unordered_map<int, int> counter;
5-
for (int v : arr) ++counter[v];
6-
vector<int> t;
7-
for (auto& [k, v] : counter) t.push_back(v);
8-
sort(t.begin(), t.end(), greater<int>());
4+
int mx = *max_element(arr.begin(), arr.end());
5+
int cnt[mx + 1];
6+
memset(cnt, 0, sizeof(cnt));
7+
for (int& x : arr) {
8+
++cnt[x];
9+
}
10+
sort(cnt, cnt + mx + 1, greater<int>());
911
int ans = 0;
10-
int n = 0;
11-
for (int cnt : t) {
12-
n += cnt;
13-
++ans;
14-
if (n * 2 >= arr.size()) break;
12+
int m = 0;
13+
for (int& x : cnt) {
14+
if (x) {
15+
m += x;
16+
++ans;
17+
if (m * 2 >= arr.size()) {
18+
break;
19+
}
20+
}
1521
}
1622
return ans;
1723
}
Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,27 @@
1-
func minSetSize(arr []int) int {
2-
counter := make(map[int]int)
3-
for _, v := range arr {
4-
counter[v]++
1+
func minSetSize(arr []int) (ans int) {
2+
mx := 0
3+
for _, x := range arr {
4+
mx = max(mx, x)
55
}
6-
var t []int
7-
for _, v := range counter {
8-
t = append(t, v)
6+
cnt := make([]int, mx+1)
7+
for _, x := range arr {
8+
cnt[x]++
99
}
10-
sort.Slice(t, func(i, j int) bool {
11-
return t[i] > t[j]
12-
})
13-
ans, n := 0, 0
14-
for _, cnt := range t {
15-
n += cnt
16-
ans++
17-
if n*2 >= len(arr) {
18-
break
10+
sort.Ints(cnt)
11+
for i, m := mx, 0; ; i-- {
12+
if cnt[i] > 0 {
13+
m += cnt[i]
14+
ans++
15+
if m >= len(arr)/2 {
16+
return
17+
}
1918
}
2019
}
21-
return ans
20+
}
21+
22+
func max(a, b int) int {
23+
if a > b {
24+
return a
25+
}
26+
return b
2227
}

0 commit comments

Comments
 (0)