Skip to content

Commit 4f5e0fa

Browse files
EdgeAshazl397985856
authored andcommitted
feat: daily 2019-06-20;594. Longest Harmonious Subsequence (azl397985856#60)
* feat: longest harmonious subsequence * daily: Longest Harmonious Subsequence, option 1 * pref: make code universal. * pref: get better answer. * alternative solutions. * Update 2019-06-20.md * Update 2019-06-20.md
1 parent c822de9 commit 4f5e0fa

File tree

2 files changed

+109
-0
lines changed

2 files changed

+109
-0
lines changed

Diff for: daily/2019-06-20.md

+102
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
# 毎日一题 - 594. Longest Harmonious Subsequence
2+
3+
## 信息卡片
4+
* 时间:2019-06-20
5+
* 题目链接:https://leetcode.com/problems/longest-harmonious-subsequence/
6+
* tag:`Array`
7+
8+
## 题目描述
9+
```
10+
We define a harmounious array as an array where the difference between its maximum value and its minimum value is exactly 1.
11+
12+
Now, given an integer array, you need to find the length of its longest harmonious subsequence among all its possible subsequences.
13+
14+
Example 1:
15+
16+
Input: [1,3,2,2,5,2,3,7]
17+
Output: 5
18+
Explanation: The longest harmonious subsequence is [3,2,2,2,3].
19+
```
20+
21+
## 思路
22+
1. 将数组中的值作为一个对象中的属性,出现的次数就是属性值
23+
2. 属性差一的值相加,获取最大的,否则返回0;
24+
25+
## 参考答案
26+
```js
27+
/**
28+
* @param {number[]} nums
29+
* @return {number}
30+
* 使用ES6中的Map
31+
*/
32+
var findLHS = function(nums) {
33+
if(!nums.length) return 0;
34+
const map = new Map();
35+
let max = 0;
36+
for(let i = 0; i<nums.length; i++) {
37+
let target = nums[i]
38+
if (map.has(target)) {
39+
map.set(target, map.get(target)+1);
40+
} else {
41+
map.set(target, 1);
42+
}
43+
}
44+
for (let key of map.keys()) {
45+
if(map.has(key+1)) {
46+
max = Math.max(map.get(key)+map.get(key+1), max);
47+
}
48+
}
49+
return max
50+
};
51+
```
52+
53+
### 其它优秀解法
54+
```js
55+
/**
56+
* @param {number[]} nums
57+
* @return {number}
58+
* for...in遍历
59+
*/
60+
var findLHS = function(nums) {
61+
if(!nums.length) return 0;
62+
const counts = {};
63+
let max = 0;
64+
for(let i = 0; i<nums.length; i++) {
65+
if (counts[nums[i]]) {
66+
counts[nums[i]] += 1;
67+
} else {
68+
counts[nums[i]] = 1;
69+
}
70+
}
71+
for (let key in counts) { // for...in性能低
72+
if(counts[+key+1]) {
73+
max = Math.max(counts[key]+counts[+key+1], max)
74+
}
75+
}
76+
return max
77+
};
78+
79+
/**
80+
* @param {number[]} nums
81+
* @return {number}
82+
* 普通遍历
83+
*/
84+
var findLHS = function(nums) {
85+
if(!nums.length) return 0;
86+
const counts = {};
87+
let max = 0;
88+
for(let i = 0; i<nums.length; i++) {
89+
if (counts[nums[i]]) {
90+
counts[nums[i]] += 1;
91+
} else {
92+
counts[nums[i]] = 1;
93+
}
94+
}
95+
for (let i = 0; i < nums.length; i++) { // 有多余的无效遍历
96+
if (counts[nums[i] + 1]) {
97+
max = Math.max(max, counts[nums[i]] + counts[nums[i] + 1]);
98+
}
99+
}
100+
return max
101+
};
102+
```

Diff for: daily/README.md

+7
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,13 @@ tag: `sql`
7272

7373
时间: 2019-06-19
7474

75+
### [594.longest-harmonious-subsequence](./2019-06-20.md)
76+
77+
tag:`Array`
78+
79+
时间:2019-06-20
80+
81+
7582
### [nth-highest-salary](./2019-06-21.md)
7683

7784
tag: `sql`

0 commit comments

Comments
 (0)