-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path594_Longest_Harmonious_Subsequence.java
More file actions
49 lines (44 loc) · 1.51 KB
/
594_Longest_Harmonious_Subsequence.java
File metadata and controls
49 lines (44 loc) · 1.51 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
41
42
43
44
45
46
47
48
49
class Solution {
public int findLHS(int[] nums) {
int answer = 0;
int num_len = nums.length;
for (int i = 0; i < num_len - 1; i++) {
List<Integer> minus = new ArrayList<>();
List<Integer> plus = new ArrayList<>();
minus.add(nums[i]);
plus.add(nums[i]);
for (int j = i + 1; j < num_len; j++) {
if (nums[i] - nums[j] == 0) {
minus.add(nums[j]);
plus.add(nums[j]);
continue;
} else if (nums[i] - nums[j] == -1) {
plus.add(nums[j]);
} else if (nums[i] - nums[j] == 1) {
minus.add(nums[j]);
}
}
int ps = plus.size() == 1 ? 0 : plus.size();
int ms = minus.size() == 1 ? 0 : minus.size();
for (int j = 0; j < minus.size() - 1; j++) {
if (!minus.get(j).equals(minus.get(j + 1))) {
break;
}
if (j == minus.size() - 2) {
ms = 0;
}
}
for (int j = 0; j < plus.size() - 1; j++) {
if (!plus.get(j).equals(plus.get(j + 1))) {
break;
}
if (j == plus.size() - 2) {
ps = 0;
}
}
int bigger = ps > ms ? ps : ms;
answer = answer > bigger ? answer : bigger;
}
return answer;
}
}