-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLC594.java
More file actions
20 lines (17 loc) · 765 Bytes
/
Copy pathLC594.java
File metadata and controls
20 lines (17 loc) · 765 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class Solution {
public int findLHS(int[] nums) {
//Mapping the numbers with their frequencies in the array.
Map<Integer, Integer> frequencyMapping = new TreeMap<>();
for (int n : nums) frequencyMapping.put(n, frequencyMapping.getOrDefault(n, 0) + 1);
//Iterating the map to find consecutive numbers as keys (if any) and adding their total frequencies to find the maximum among them.
int max = 0;
for(Integer num : frequencyMapping.keySet()) {
if (frequencyMapping.containsKey(num + 1)) {
int combinedLength = frequencyMapping.get(num) + frequencyMapping.get(num + 1);
max = Math.max(combinedLength, max);
}
}
return max;
}
}
//Solved