-
Notifications
You must be signed in to change notification settings - Fork 43
/
longest-harmonious-subsequence.py
120 lines (113 loc) · 3.21 KB
/
longest-harmonious-subsequence.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# Time: O(n)
# Space: O(n)
# We define a harmonious array is an array where the difference
# between its maximum value and its minimum value is exactly 1.
#
# Now, given an integer array, you need to find the length of its
# longest harmonious subsequence among all its possible subsequences.
#
# Example 1:
# Input: [1,3,2,2,5,2,3,7]
# Output: 5
# Explanation: The longest harmonious subsequence is [3,2,2,2,3].
# Note: The length of the input array will not exceed 20,000.
# V0
from collections
class Solution(object):
def findLHS(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
count = collections.Counter(nums)
res = 0
for num in count.keys():
if num + 1 in count:
res = max(res, count[num] + count[num + 1])
return res
# V1
# https://blog.csdn.net/fuxuemingzhu/article/details/79233752
from collections
class Solution(object):
def findLHS(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
counter = collections.Counter(nums)
nums_set = set(nums)
longest = 0
for num in nums_set:
if num + 1 in counter:
longest = max(longest, counter[num] + counter[num + 1])
return longest
# V1'
# https://blog.csdn.net/fuxuemingzhu/article/details/79233752
from collections
class Solution(object):
def findLHS(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
count = collections.Counter(nums)
res = 0
for num in count.keys():
if num + 1 in count:
res = max(res, count[num] + count[num + 1])
return res
# V1''
# http://bookshadow.com/weblog/2017/05/21/leetcode-longest-harmonious-subsequence/
from collections
class Solution(object):
def findLHS(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
cnt = collections.Counter(nums)
ans = 0
lastKey = lastVal = None
for key, val in sorted(cnt.items()):
if lastKey is not None and lastKey + 1 == key:
ans = max(ans, val + lastVal)
lastKey, lastVal = key, val
return ans
# V1'''
# https://www.jiuzhang.com/solution/longest-harmonious-subsequence/#tag-highlight-lang-python
class Solution:
"""
@param nums: a list of integers
@return: return a integer
"""
def findLHS(self, nums):
# write your code here
vis = {}
ans = 0
for num in nums:
if num in vis:
vis[num] += 1
else:
vis[num] = 1
for num in nums:
if num in vis and num - 1 in vis:
ans = max(ans, vis[num] + vis[num - 1])
return ans
# V2
# Time: O(n)
# Space: O(n)
import collections
class Solution(object):
def findLHS(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
lookup = collections.defaultdict(int)
result = 0
for num in nums:
lookup[num] += 1
for diff in [-1, 1]:
if (num + diff) in lookup:
result = max(result, lookup[num] + lookup[num + diff])
return result