-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolution.py
39 lines (36 loc) · 1.5 KB
/
solution.py
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
class Solution(object):
def findLHS(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
max_length = 0
harmonious_map = {}
for num in nums:
key_prev = str(num - 1) + 'X' + str(num)
key_next = str(num) + 'X' + str(num + 1)
if harmonious_map.get(key_prev) == None:
harmonious_map[key_prev] = {
'length': 1,
'base': num,
'is_harmonious': False
}
else:
harmonious_map[key_prev]['length'] += 1
if harmonious_map[key_prev]['base'] != num:
harmonious_map[key_prev]['is_harmonious'] = True
if harmonious_map[key_prev]['is_harmonious'] == True:
max_length = max(max_length, harmonious_map[key_prev]['length'])
if harmonious_map.get(key_next) == None:
harmonious_map[key_next] = {
'length': 1,
'base': num,
'is_harmonious': False
}
else:
harmonious_map[key_next]['length'] += 1
if harmonious_map[key_next]['base'] != num:
harmonious_map[key_next]['is_harmonious'] = True
if harmonious_map[key_next]['is_harmonious'] == True:
max_length = max(max_length, harmonious_map[key_next]['length'])
return max_length