Skip to content

Commit 3c323f6

Browse files
add 3545
1 parent 9ac3e7e commit 3c323f6

3 files changed

Lines changed: 69 additions & 0 deletions

File tree

  • paginated_contents/algorithms/4th_thousand
  • src

‎paginated_contents/algorithms/4th_thousand/README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@
7878
| 3581 | [Count Odd Letters from Number](https://leetcode.com/problems/count-odd-letters-from-number/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3581.java) | | Easy |
7979
| 3569 | [Count Islands With Total Value Divisible by K](https://leetcode.com/problems/count-islands-with-total-value-divisible-by-k/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3569.java) | | Medium | Union Find
8080
| 3550 | [Smallest Index With Digit Sum Equal to Index](https://leetcode.com/problems/smallest-index-with-digit-sum-equal-to-index/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3550.java) | | Easy |
81+
| 3545 | [Minimum Deletions for At Most K Distinct Characters](https://leetcode.com/problems/minimum-deletions-for-at-most-k-distinct-characters/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3545.java) | | Easy |
8182
| 3516 | [Find Closest Person](https://leetcode.com/problems/find-closest-person/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3516.java) | | Easy |
8283
| 3507 | [Minimum Pair Removal to Sort Array I](https://leetcode.com/problems/minimum-pair-removal-to-sort-array-i/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3507.java) | | Easy |
8384
| 3502 | [Minimum Cost to Reach Every Position](https://leetcode.com/problems/minimum-cost-to-reach-every-position/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3502.java) | | Easy |
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.fishercoder.solutions.fourththousand;
2+
3+
import java.util.*;
4+
5+
public class _3545 {
6+
public static class Solution1 {
7+
public int minDeletion(String s, int k) {
8+
Map<Character, Integer> map = new HashMap<>();
9+
for (char c : s.toCharArray()) {
10+
map.put(c, map.getOrDefault(c, 0) + 1);
11+
}
12+
List<Integer> frequencies = new ArrayList<>();
13+
for (char c : map.keySet()) {
14+
frequencies.add(map.get(c));
15+
}
16+
if (frequencies.size() <= k) {
17+
return 0;
18+
}
19+
Collections.sort(frequencies);
20+
int deletions = 0;
21+
for (int i = 0; i < frequencies.size() && (frequencies.size() - i) > k; i++) {
22+
deletions += frequencies.get(i);
23+
}
24+
return deletions;
25+
}
26+
}
27+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.fishercoder.fourththousand;
2+
3+
import com.fishercoder.solutions.fourththousand._3545;
4+
import org.junit.jupiter.api.BeforeEach;
5+
import org.junit.jupiter.api.Test;
6+
7+
import static org.junit.jupiter.api.Assertions.assertEquals;
8+
9+
public class _3545Test {
10+
private _3545.Solution1 solution1;
11+
12+
@BeforeEach
13+
public void setup() {
14+
solution1 = new _3545.Solution1();
15+
}
16+
17+
@Test
18+
public void test1() {
19+
assertEquals(1, solution1.minDeletion("abc", 2));
20+
}
21+
22+
@Test
23+
public void test2() {
24+
assertEquals(0, solution1.minDeletion("aabb", 2));
25+
}
26+
27+
@Test
28+
public void test3() {
29+
assertEquals(2, solution1.minDeletion("yyyzz", 1));
30+
}
31+
32+
@Test
33+
public void test4() {
34+
assertEquals(2, solution1.minDeletion("adx", 1));
35+
}
36+
37+
@Test
38+
public void test5() {
39+
assertEquals(3, solution1.minDeletion("wund", 1));
40+
}
41+
}

0 commit comments

Comments
 (0)