Skip to content

Commit bd09409

Browse files
committed
Add solution #1481
1 parent 1f9adf1 commit bd09409

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@
170170
1472|[Design Browser History](./1472-design-browser-history.js)|Medium|
171171
1475|[Final Prices With a Special Discount in a Shop](./1475-final-prices-with-a-special-discount-in-a-shop.js)|Easy|
172172
1480|[Running Sum of 1d Array](./1480-running-sum-of-1d-array.js)|Easy|
173+
1481|[Least Number of Unique Integers after K Removals](./1481-least-number-of-unique-integers-after-k-removals.js)|Medium|
173174
1486|[XOR Operation in an Array](./1486-xor-operation-in-an-array.js)|Easy|
174175
1492|[The kth Factor of n](./1492-the-kth-factor-of-n.js)|Medium|
175176
1496|[Path Crossing](./1496-path-crossing.js)|Easy|
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* 1481. Least Number of Unique Integers after K Removals
3+
* https://leetcode.com/problems/least-number-of-unique-integers-after-k-removals/
4+
* Difficulty: Medium
5+
*
6+
* Given an array of integers arr and an integer k. Find the least number of
7+
* unique integers after removing exactly k elements.
8+
*/
9+
10+
/**
11+
* @param {number[]} arr
12+
* @param {number} k
13+
* @return {number}
14+
*/
15+
var findLeastNumOfUniqueInts = function(arr, k) {
16+
const map = new Map();
17+
arr.forEach(n => map.set(n, map.has(n) ? map.get(n) + 1 : 1));
18+
19+
const sorted = [...map.values()].sort((a, b) => a - b);
20+
while (k && sorted.length && sorted[0] <= k) {
21+
k -= sorted[0];
22+
sorted.shift();
23+
}
24+
25+
return sorted.length;
26+
};

0 commit comments

Comments
 (0)