Skip to content

Commit 814ecc6

Browse files
add 5454
1 parent ebb1b40 commit 814ecc6

File tree

3 files changed

+58
-0
lines changed

3 files changed

+58
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ _If you like this project, please leave me a star._ ★
88

99
| # | Title | Solutions | Video | Difficulty | Tag
1010
|-----|----------------|---------------|--------|-------------|-------------
11+
|5454|[Least Number of Unique Integers after K Removals](https://leetcode.com/problems/least-number-of-unique-integers-after-k-removals/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_5454.java) | |Medium|Array, Sort|
1112
|5453|[Running Sum of 1d Array](https://leetcode.com/problems/running-sum-of-1d-array/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_5453.java) | |Easy|Array|
1213
|1476|[Subrectangle Queries](https://leetcode.com/problems/subrectangle-queries/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1476.java) | |Medium|Array|
1314
|1475|[Final Prices With a Special Discount in a Shop](https://leetcode.com/problems/final-prices-with-a-special-discount-in-a-shop/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1475.java) | |Easy|Array|
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.fishercoder.solutions;
2+
3+
import java.util.HashMap;
4+
import java.util.LinkedHashMap;
5+
import java.util.Map;
6+
7+
public class _5454 {
8+
public static class Solution1 {
9+
public int findLeastNumOfUniqueInts(int[] arr, int k) {
10+
Map<Integer, Integer> unSortedMap = new HashMap<>();
11+
for (int num : arr) {
12+
unSortedMap.put(num, unSortedMap.getOrDefault(num, 0) + 1);
13+
}
14+
//LinkedHashMap preserve the ordering of elements in which they are inserted
15+
LinkedHashMap<Integer, Integer> sortedMap = new LinkedHashMap<>();
16+
17+
unSortedMap.entrySet()
18+
.stream()
19+
.sorted(Map.Entry.comparingByValue())
20+
.forEachOrdered(x -> sortedMap.put(x.getKey(), x.getValue()));
21+
int leastUniq = 0;
22+
for (int key : sortedMap.keySet()) {
23+
int count = sortedMap.get(key);
24+
if (k >= count) {
25+
k -= count;
26+
} else {
27+
leastUniq++;
28+
}
29+
}
30+
return leastUniq;
31+
}
32+
}
33+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._5454;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static junit.framework.TestCase.assertEquals;
8+
9+
public class _5454Test {
10+
private static _5454.Solution1 solution1;
11+
private static int[] arr;
12+
13+
@BeforeClass
14+
public static void setup() {
15+
solution1 = new _5454.Solution1();
16+
}
17+
18+
@Test
19+
public void test1() {
20+
arr = new int[]{5, 5, 4};
21+
assertEquals(1, solution1.findLeastNumOfUniqueInts(arr, 1));
22+
}
23+
24+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy