|
| 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 | +``` |
0 commit comments