Skip to content

Commit 8f683d4

Browse files
committed
feat: add solutions to lc problem: No.4545
No.3545.Minimum Deletions for At Most K Distinct Characters
1 parent 6fdcc26 commit 8f683d4

File tree

7 files changed

+171
-8
lines changed

7 files changed

+171
-8
lines changed

solution/3500-3599/3545.Minimum Deletions for At Most K Distinct Characters/README.md

+60-4
Original file line numberDiff line numberDiff line change
@@ -87,32 +87,88 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3500-3599/3545.Mi
8787

8888
<!-- solution:start -->
8989

90-
### 方法一
90+
### 方法一:计数 + 贪心
91+
92+
我们可以使用一个数组 $\textit{cnt}$ 来统计每个字符的出现频率。然后我们对这个数组进行排序,最后返回前 $26 - k$ 个元素的和。
93+
94+
时间复杂度 $O(|\Sigma| \times \log |\Sigma|)$,空间复杂度 $O(|\Sigma|)$,其中 $|\Sigma|$ 是字符集的大小,本题中 $|\Sigma| = 26$。
9195

9296
<!-- tabs:start -->
9397

9498
#### Python3
9599

96100
```python
97-
101+
class Solution:
102+
def minDeletion(self, s: str, k: int) -> int:
103+
return sum(sorted(Counter(s).values())[:-k])
98104
```
99105

100106
#### Java
101107

102108
```java
103-
109+
class Solution {
110+
public int minDeletion(String s, int k) {
111+
int[] cnt = new int[26];
112+
for (char c : s.toCharArray()) {
113+
++cnt[c - 'a'];
114+
}
115+
Arrays.sort(cnt);
116+
int ans = 0;
117+
for (int i = 0; i + k < 26; ++i) {
118+
ans += cnt[i];
119+
}
120+
return ans;
121+
}
122+
}
104123
```
105124

106125
#### C++
107126

108127
```cpp
109-
128+
class Solution {
129+
public:
130+
int minDeletion(string s, int k) {
131+
vector<int> cnt(26);
132+
for (char c : s) {
133+
++cnt[c - 'a'];
134+
}
135+
ranges::sort(cnt);
136+
int ans = 0;
137+
for (int i = 0; i + k < 26; ++i) {
138+
ans += cnt[i];
139+
}
140+
return ans;
141+
}
142+
};
110143
```
111144
112145
#### Go
113146
114147
```go
148+
func minDeletion(s string, k int) (ans int) {
149+
cnt := make([]int, 26)
150+
for _, c := range s {
151+
cnt[c-'a']++
152+
}
153+
sort.Ints(cnt)
154+
for i := 0; i+k < len(cnt); i++ {
155+
ans += cnt[i]
156+
}
157+
return
158+
}
159+
```
115160

161+
#### TypeScript
162+
163+
```ts
164+
function minDeletion(s: string, k: number): number {
165+
const cnt: number[] = Array(26).fill(0);
166+
for (const c of s) {
167+
++cnt[c.charCodeAt(0) - 97];
168+
}
169+
cnt.sort((a, b) => a - b);
170+
return cnt.slice(0, 26 - k).reduce((a, b) => a + b, 0);
171+
}
116172
```
117173

118174
<!-- tabs:end -->

solution/3500-3599/3545.Minimum Deletions for At Most K Distinct Characters/README_EN.md

+60-4
Original file line numberDiff line numberDiff line change
@@ -85,32 +85,88 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3500-3599/3545.Mi
8585

8686
<!-- solution:start -->
8787

88-
### Solution 1
88+
### Solution 1: Counting + Greedy
89+
90+
We can use an array $\textit{cnt}$ to count the frequency of each character. Then, we sort this array and return the sum of the first $26 - k$ elements.
91+
92+
The time complexity is $O(|\Sigma| \times \log |\Sigma|)$, and the space complexity is $O(|\Sigma|)$, where $|\Sigma|$ is the size of the character set. In this problem, $|\Sigma| = 26$.
8993

9094
<!-- tabs:start -->
9195

9296
#### Python3
9397

9498
```python
95-
99+
class Solution:
100+
def minDeletion(self, s: str, k: int) -> int:
101+
return sum(sorted(Counter(s).values())[:-k])
96102
```
97103

98104
#### Java
99105

100106
```java
101-
107+
class Solution {
108+
public int minDeletion(String s, int k) {
109+
int[] cnt = new int[26];
110+
for (char c : s.toCharArray()) {
111+
++cnt[c - 'a'];
112+
}
113+
Arrays.sort(cnt);
114+
int ans = 0;
115+
for (int i = 0; i + k < 26; ++i) {
116+
ans += cnt[i];
117+
}
118+
return ans;
119+
}
120+
}
102121
```
103122

104123
#### C++
105124

106125
```cpp
107-
126+
class Solution {
127+
public:
128+
int minDeletion(string s, int k) {
129+
vector<int> cnt(26);
130+
for (char c : s) {
131+
++cnt[c - 'a'];
132+
}
133+
ranges::sort(cnt);
134+
int ans = 0;
135+
for (int i = 0; i + k < 26; ++i) {
136+
ans += cnt[i];
137+
}
138+
return ans;
139+
}
140+
};
108141
```
109142
110143
#### Go
111144
112145
```go
146+
func minDeletion(s string, k int) (ans int) {
147+
cnt := make([]int, 26)
148+
for _, c := range s {
149+
cnt[c-'a']++
150+
}
151+
sort.Ints(cnt)
152+
for i := 0; i+k < len(cnt); i++ {
153+
ans += cnt[i]
154+
}
155+
return
156+
}
157+
```
113158

159+
#### TypeScript
160+
161+
```ts
162+
function minDeletion(s: string, k: number): number {
163+
const cnt: number[] = Array(26).fill(0);
164+
for (const c of s) {
165+
++cnt[c.charCodeAt(0) - 97];
166+
}
167+
cnt.sort((a, b) => a - b);
168+
return cnt.slice(0, 26 - k).reduce((a, b) => a + b, 0);
169+
}
114170
```
115171

116172
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution {
2+
public:
3+
int minDeletion(string s, int k) {
4+
vector<int> cnt(26);
5+
for (char c : s) {
6+
++cnt[c - 'a'];
7+
}
8+
ranges::sort(cnt);
9+
int ans = 0;
10+
for (int i = 0; i + k < 26; ++i) {
11+
ans += cnt[i];
12+
}
13+
return ans;
14+
}
15+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
func minDeletion(s string, k int) (ans int) {
2+
cnt := make([]int, 26)
3+
for _, c := range s {
4+
cnt[c-'a']++
5+
}
6+
sort.Ints(cnt)
7+
for i := 0; i+k < len(cnt); i++ {
8+
ans += cnt[i]
9+
}
10+
return
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution {
2+
public int minDeletion(String s, int k) {
3+
int[] cnt = new int[26];
4+
for (char c : s.toCharArray()) {
5+
++cnt[c - 'a'];
6+
}
7+
Arrays.sort(cnt);
8+
int ans = 0;
9+
for (int i = 0; i + k < 26; ++i) {
10+
ans += cnt[i];
11+
}
12+
return ans;
13+
}
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
class Solution:
2+
def minDeletion(self, s: str, k: int) -> int:
3+
return sum(sorted(Counter(s).values())[:-k])
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
function minDeletion(s: string, k: number): number {
2+
const cnt: number[] = Array(26).fill(0);
3+
for (const c of s) {
4+
++cnt[c.charCodeAt(0) - 97];
5+
}
6+
cnt.sort((a, b) => a - b);
7+
return cnt.slice(0, 26 - k).reduce((a, b) => a + b, 0);
8+
}

0 commit comments

Comments
 (0)