Skip to content

Commit 9b9713f

Browse files
committedJan 2, 2025
Add solution #3396
1 parent 8856e71 commit 9b9713f

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed
 

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,7 @@
361361
2490|[Circular Sentence](./2490-circular-sentence.js)|Easy|
362362
2529|[Maximum Count of Positive Integer and Negative Integer](./2529-maximum-count-of-positive-integer-and-negative-integer.js)|Easy|
363363
2535|[Difference Between Element Sum and Digit Sum of an Array](./2535-difference-between-element-sum-and-digit-sum-of-an-array.js)|Easy|
364+
3396|[Minimum Number of Operations to Make Elements in Array Distinct](./3396-minimum-number-of-operations-to-make-elements-in-array-distinct.js)|Easy|
364365
3402|[Minimum Operations to Make Columns Strictly Increasing](./3402-minimum-operations-to-make-columns-strictly-increasing.js)|Easy|
365366

366367
## License
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* 3396. Minimum Number of Operations to Make Elements in Array Distinct
3+
* https://leetcode.com/problems/minimum-number-of-operations-to-make-elements-in-array-distinct/
4+
* Difficulty: Easy
5+
*
6+
* You are given an integer array nums. You need to ensure that the elements in the array are
7+
* distinct. To achieve this, you can perform the following operation any number of times:
8+
* - Remove 3 elements from the beginning of the array. If the array has fewer than 3 elements,
9+
* remove all remaining elements.
10+
* Note that an empty array is considered to have distinct elements. Return the minimum number
11+
* of operations needed to make the elements in the array distinct.
12+
*/
13+
14+
/**
15+
* @param {number[]} nums
16+
* @return {number}
17+
*/
18+
var minimumOperations = function(nums) {
19+
const unique = new Set();
20+
21+
for (let i = nums.length - 1; i > -1; i--) {
22+
if (unique.has(nums[i])) {
23+
return Math.ceil((i + 1) / 3);
24+
}
25+
unique.add(nums[i]);
26+
}
27+
28+
return 0;
29+
};

0 commit comments

Comments
 (0)
Please sign in to comment.