-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathmay4.java
More file actions
40 lines (35 loc) · 1.03 KB
/
may4.java
File metadata and controls
40 lines (35 loc) · 1.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
// Use Java 11
import java.util.HashMap;
import java.util.concurrent.atomic.AtomicInteger;
public class Solution {
public int solution(int[] A, int L, int R) {
var mapper = new HashMap<Integer, Integer>();
for (int a : A) {
mapper.put(a, mapper.getOrDefault(a, 0) + 1);
}
AtomicInteger result = new AtomicInteger();
mapper.forEach((key, value) -> {
int count = Math.min(value, 2);
if (count == 2) {
if (key >= L) {
count--;
}
if (key <= R) {
count--;
}
}
else if (key >= L && key <= R)
{
count--;
}
if (count > 0) {
result.addAndGet(count);
}
});
return result.get();
}
}