Skip to content

Commit 9f67246

Browse files
committed
solve problem Longest Harmonious Subsequence
1 parent a6d89f5 commit 9f67246

File tree

5 files changed

+136
-0
lines changed

5 files changed

+136
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ All solutions will be accepted!
156156
|645|[Set Mismatch](https://leetcode-cn.com/problems/set-mismatch/description/)|[java/py/js](./algorithms/SetMismatch)|Easy|
157157
|58|[Length Of Last Word](https://leetcode-cn.com/problems/length-of-last-word/description/)|[java/py/js](./algorithms/LengthOfLastWord)|Easy|
158158
|14|[Longest Common Prefix](https://leetcode-cn.com/problems/longest-common-prefix/description/)|[java/py/js](./algorithms/LongestCommonPrefix)|Easy|
159+
|594|[Longest Harmonious Subsequence](https://leetcode-cn.com/problems/longest-harmonious-subsequence/description/)|[java/py/js](./algorithms/LongestHarmoniousSubsequence)|Easy|
159160

160161
# Database
161162
|#|Title|Solution|Difficulty|
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Longest Harmonious Subsequence
2+
This problem is easy to solve by hashmap
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
class Solution {
2+
public int findLHS(int[] nums) {
3+
int maxLength = 0;
4+
Map<String, Map<String, Object>> harmoniousMap = new HashMap<String, Map<String, Object>>();
5+
6+
for (int num : nums) {
7+
String prevKey = String.valueOf(num - 1) + "X" + String.valueOf(num),
8+
nextKey = String.valueOf(num) + "X" + String.valueOf(num + 1);
9+
10+
if (harmoniousMap.get(prevKey) == null) {
11+
Map<String, Object> m = new HashMap<String, Object>();
12+
m.put("length", 1);
13+
m.put("base", num);
14+
m.put("isHarmonious", false);
15+
harmoniousMap.put(prevKey, m);
16+
} else {
17+
Map<String, Object> m = harmoniousMap.get(prevKey);
18+
m.put("length", (Integer) m.get("length") + 1);
19+
if (!((Integer) m.get("base")).equals(num)) {
20+
m.put("isHarmonious", true);
21+
}
22+
if ((Boolean) m.get("isHarmonious")) {
23+
maxLength = Math.max(maxLength, (Integer) m.get("length"));
24+
}
25+
}
26+
27+
if (harmoniousMap.get(nextKey) == null) {
28+
Map<String, Object> m = new HashMap<String, Object>();
29+
m.put("length", 1);
30+
m.put("base", num);
31+
m.put("isHarmonious", false);
32+
harmoniousMap.put(nextKey, m);
33+
} else {
34+
Map<String, Object> m = harmoniousMap.get(nextKey);
35+
m.put("length", (Integer) m.get("length") + 1);
36+
if (!((Integer) m.get("base")).equals(num)) {
37+
m.put("isHarmonious", true);
38+
}
39+
if ((Boolean) m.get("isHarmonious")) {
40+
maxLength = Math.max(maxLength, (Integer) m.get("length"));
41+
}
42+
}
43+
}
44+
45+
return maxLength;
46+
}
47+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {number}
4+
*/
5+
var findLHS = function(nums) {
6+
let maxLength = 0,
7+
harmoniousMap = new Map()
8+
9+
nums.forEach(num => {
10+
let prevKey = `${num - 1}X${num}`,
11+
nextKey = `${num}X${num + 1}`
12+
13+
if (harmoniousMap.get(prevKey) === undefined) {
14+
harmoniousMap.set(prevKey, {
15+
length: 1,
16+
base: num,
17+
isHarmonious: false
18+
})
19+
} else {
20+
harmoniousMap.get(prevKey).length++
21+
if (harmoniousMap.get(prevKey).base !== num) {
22+
harmoniousMap.get(prevKey).isHarmonious = true
23+
}
24+
if (harmoniousMap.get(prevKey).isHarmonious) {
25+
maxLength = Math.max(maxLength, harmoniousMap.get(prevKey).length)
26+
}
27+
}
28+
29+
if (harmoniousMap.get(nextKey) === undefined) {
30+
harmoniousMap.set(nextKey, {
31+
length: 1,
32+
base: num,
33+
isHarmonious: false
34+
})
35+
} else {
36+
harmoniousMap.get(nextKey).length++
37+
if (harmoniousMap.get(nextKey).base !== num) {
38+
harmoniousMap.get(nextKey).isHarmonious = true
39+
}
40+
if (harmoniousMap.get(nextKey).isHarmonious) {
41+
maxLength = Math.max(maxLength, harmoniousMap.get(nextKey).length)
42+
}
43+
}
44+
})
45+
46+
return maxLength
47+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
class Solution(object):
2+
def findLHS(self, nums):
3+
"""
4+
:type nums: List[int]
5+
:rtype: int
6+
"""
7+
max_length = 0
8+
harmonious_map = {}
9+
10+
for num in nums:
11+
key_prev = str(num - 1) + 'X' + str(num)
12+
key_next = str(num) + 'X' + str(num + 1)
13+
if harmonious_map.get(key_prev) == None:
14+
harmonious_map[key_prev] = {
15+
'length': 1,
16+
'base': num,
17+
'is_harmonious': False
18+
}
19+
else:
20+
harmonious_map[key_prev]['length'] += 1
21+
if harmonious_map[key_prev]['base'] != num:
22+
harmonious_map[key_prev]['is_harmonious'] = True
23+
if harmonious_map[key_prev]['is_harmonious'] == True:
24+
max_length = max(max_length, harmonious_map[key_prev]['length'])
25+
26+
if harmonious_map.get(key_next) == None:
27+
harmonious_map[key_next] = {
28+
'length': 1,
29+
'base': num,
30+
'is_harmonious': False
31+
}
32+
else:
33+
harmonious_map[key_next]['length'] += 1
34+
if harmonious_map[key_next]['base'] != num:
35+
harmonious_map[key_next]['is_harmonious'] = True
36+
if harmonious_map[key_next]['is_harmonious'] == True:
37+
max_length = max(max_length, harmonious_map[key_next]['length'])
38+
39+
return max_length

0 commit comments

Comments
 (0)