作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/


题目地址:https://leetcode.com/problems/longest-harmonious-subsequence/description/

题目描述

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.

题目大意

一个和谐子序列是数组中的最大值和最小值的差值恰好是1,求给定数组的最长和谐子序列。

解题方法

统计次数

重点是对题目进行抽象,这个题可以把和谐子序列抽象成一个只存在相邻两个数字的数组,位置无所谓的。

那么,我们应该,先数每个数字出现的次数,然后对每个数字num,找num+1是否存在,如果存在就记录两个和的最大值。

from collections import Counter
class Solution(object):
def findLHS(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
counter = 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

二刷的时候写了同样的解法,并且更简单一点:

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

C++代码如下:

class Solution {
public:
int findLHS(vector<int>& nums) {
map<int, int> d;
for (int n : nums) d[n] ++;
int res = 0;
for (auto n : d) {
if (d.count(n.first + 1))
res = max(res, n.second + d[n.first + 1]);
}
return res;
}
};

日期

2018 年 2 月 1 日
2018 年 11 月 19 日 —— 周一又开始了
2018 年 12 月 7 日 —— 恩,12月又过去一周了

【LeetCode】594. Longest Harmonious Subsequence 解题报告(Python & C++)的更多相关文章

  1. LeetCode 594. Longest Harmonious Subsequence (最长的协调子序列)

    We define a harmonious array is an array where the difference between its maximum value and its mini ...

  2. 【LeetCode】873. Length of Longest Fibonacci Subsequence 解题报告(Python)

    [LeetCode]873. Length of Longest Fibonacci Subsequence 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: ...

  3. 【LeetCode】673. Number of Longest Increasing Subsequence 解题报告(Python)

    [LeetCode]673. Number of Longest Increasing Subsequence 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https:/ ...

  4. 【LeetCode】376. Wiggle Subsequence 解题报告(Python)

    [LeetCode]376. Wiggle Subsequence 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.c ...

  5. 【LeetCode】392. Is Subsequence 解题报告(Python)

    [LeetCode]392. Is Subsequence 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/is-subseq ...

  6. 594. Longest Harmonious Subsequence - LeetCode

    Question 594. Longest Harmonious Subsequence Solution 题目大意:找一个最长子序列,要求子序列中最大值和最小值的差是1. 思路:构造一个map,保存 ...

  7. 【Leetcode_easy】594. Longest Harmonious Subsequence

    problem 594. Longest Harmonious Subsequence 最长和谐子序列 题意: 可以对数组进行排序,那么实际上只要找出来相差为1的两个数的总共出现个数就是一个和谐子序列 ...

  8. [LeetCode&Python] Problem 594. Longest Harmonious Subsequence

    We define a harmonious array is an array where the difference between its maximum value and its mini ...

  9. 594. Longest Harmonious Subsequence强制差距为1的最长连续

    [抄题]: We define a harmonious array is an array where the difference between its maximum value and it ...

随机推荐

  1. LVS-三种模式的配置详情

    NAT模式 实验环境 LVS1 VIP 192.168.31.66 DIP 192.168.121.128 WEB1 192.168.121.129 WEB2 192.168.121.130 安装与配 ...

  2. Linux— file命令 用于辨识文件类型

    Linux file命令用于辨识文件类型. 通过file指令,我们得以辨识该文件的类型. 语法 file [-bcLvz][-f <名称文件>][-m <魔法数字文件>...] ...

  3. words in English that contradict themselves

    [S1E10, TBBT]Leonard: I don't get it. I already told her a lie. Why would I replace it with a differ ...

  4. LeetCode398-随机数索引

    原题链接:[398. 随机数索引]:https://leetcode-cn.com/problems/random-pick-index/ 题目描述: 给定一个可能含有重复元素的整数数组,要求随机输出 ...

  5. 【Reverse】每日必逆0x02

    BUU SimpleRev 附件 https://files.buuoj.cn/files/7458c5c0ce999ac491df13cf7a7ed9f1/SimpleRev 题解 查壳 拖入iad ...

  6. 基于DataX将数据从Sqlserver同步到Oracle

    DataX是阿里云推出的一款开源的ETL工具,通过配置json文件实现不同数据库之间的数据同步.先有需求是从Sqlserver同步数据到Oracle,网上关于DataX的介绍很多. 框架设计 Data ...

  7. Java_zip_多源文件压缩到指定目录下

    依赖: <!-- https://mvnrepository.com/artifact/org.apache.commons/commons-compress --> <depend ...

  8. 【HarmonyOS】【多线程与并发】EventHandler

    EventHandler与EventRunner EventHandler相关概念 ● EventHandler是一种用户在当前线程上投递InnerEvent事件或者Runnable任务到异步线程上处 ...

  9. 模板方法模式(Template Method Pattern)——复杂流程步骤的设计

    模式概述 在现实生活中,很多事情都包含几个实现步骤,例如请客吃饭,无论吃什么,一般都包含点单.吃东西.买单等几个步骤,通常情况下这几个步骤的次序是:点单 --> 吃东西 --> 买单. 在 ...

  10. 为什么要用urlencode()函数进行url编码

    URLEncode就是将URL中特殊部分进行编码.URLDecoder就是对特殊部分进行解码. 因为当字符串数据以url的形式传递给web服务器时,字符串中是不允许出现空格和特殊字符的 譬如:你要传的 ...