Skip to content

Commit 8fd3a65

Browse files
authored
Create Minimum Number of Operations to Make Array Empty.java
1 parent dd34462 commit 8fd3a65

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution {
2+
public int minOperations(int[] nums) {
3+
Map<Integer, Long> map = Arrays.stream(nums)
4+
.boxed()
5+
.collect(
6+
Collectors.groupingBy(Function.identity(), HashMap::new, Collectors.counting()));
7+
int operations = 0;
8+
for (Long value : map.values()) {
9+
if (value == 1) {
10+
return -1;
11+
}
12+
operations += (int) (value / 3 + (value % 3 != 0 ? 1 : 0));
13+
}
14+
return operations;
15+
}
16+
}

0 commit comments

Comments
 (0)