Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 0b3d11c

Browse files
[LEET-3396] add 3396
1 parent fec696f commit 0b3d11c

File tree

2 files changed

+35
-0
lines changed
  • paginated_contents/algorithms/4th_thousand
  • src/main/java/com/fishercoder/solutions/fourththousand

2 files changed

+35
-0
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
| # | Title | Solutions | Video | Difficulty | Tag
22
|------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------|-------------------------------------------------------------------------------|------------|----------------------------------------------------------------------
3+
| 3396 | [Minimum Number of Operations to Make Elements in Array Distinct](https://leetcode.com/problems/minimum-number-of-operations-to-make-elements-in-array-distinct/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3396.java) | | Easy |
34
| 3370 | [Smallest Number With All Set Bits](https://leetcode.com/problems/smallest-number-with-all-set-bits/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3370.java) | | Easy |
45
| 3364 | [Minimum Positive Sum Subarray](https://leetcode.com/problems/minimum-positive-sum-subarray/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3364.java) | | Easy |
56
| 3360 | [Stone Removal Game](https://leetcode.com/problems/stone-removal-game/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3360.java) | | Easy |
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.fishercoder.solutions.fourththousand;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
6+
public class _3396 {
7+
public static class Solution1 {
8+
public int minimumOperations(int[] nums) {
9+
int ops = 0;
10+
Map<Integer, Integer> map = new HashMap<>();
11+
for (int num : nums) {
12+
map.put(num, map.getOrDefault(num, 0) + 1);
13+
}
14+
int i = 0;
15+
while (!allDistinct(map)) {
16+
ops++;
17+
int target = i + 3;
18+
for (; i < target && i < nums.length; i++) {
19+
map.put(nums[i], map.get(nums[i]) - 1);
20+
}
21+
}
22+
return ops;
23+
}
24+
25+
private boolean allDistinct(Map<Integer, Integer> map) {
26+
for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
27+
if (entry.getValue() > 1) {
28+
return false;
29+
}
30+
}
31+
return true;
32+
}
33+
}
34+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /