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.

Brute Force Method:

class Solution(object):
def findLHS(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
nums=sorted(nums)
i=0
pre_count=1
ans=0
i=0
while i<len(nums):
count=1
if i>0 and nums[i]-nums[i-1]==1:
while i<len(nums)-1 and nums[i]==nums[i+1]:
i+=1
count+=1
ans=max(ans,pre_count+count)
pre_count=count
else:
while i<len(nums)-1 and nums[i]==nums[i+1]:
i+=1
count+=1
pre_count=count
i+=1
return ans

  

HashMap Method:

from collections import Counter
class Solution(object):
def findLHS(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
nc=Counter(nums)
ans=0
for i in nc:
if i==1:
if nc[1]>0 and nc[2]>0:
ans=max(ans,nc[i]+nc[i+1])
else:
if nc[i]>0 and nc[i+1]>0:
ans=max(ans,nc[i]+nc[i+1])
if nc[i]>0 and nc[i-1]<0:
ans=max(ans,nc[i-1]+nc[i])
return ans

  

[LeetCode&Python] Problem 594. Longest Harmonious Subsequence的更多相关文章

  1. 【Leetcode_easy】594. Longest Harmonious Subsequence

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

  2. 594. Longest Harmonious Subsequence - LeetCode

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

  3. 【LeetCode】594. Longest Harmonious Subsequence 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 统计次数 日期 题目地址:https://leetc ...

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

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

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

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

  6. [LeetCode&Python] Problem 674. Longest Continuous Increasing Subsequence

    Given an unsorted array of integers, find the length of longest continuousincreasing subsequence (su ...

  7. [LeetCode&Python] Problem 720. Longest Word in Dictionary

    Given a list of strings words representing an English Dictionary, find the longest word in words tha ...

  8. [LeetCode&Python] Problem 409. Longest Palindrome

    Given a string which consists of lowercase or uppercase letters, find the length of the longest pali ...

  9. 594. Longest Harmonious Subsequence

    方法一:用一个map来辅助,代码简单,思路清晰 static int wing=[]() { std::ios::sync_with_stdio(false); cin.tie(NULL); ; }( ...

随机推荐

  1. java对象深度拷贝

    如何利用序列化来完成对象的拷贝呢?在内存中通过字节流的拷贝是比较容易实现的.把母对象写入到一个字节流中,再从字节流中将其读出来,这样就可以创建一个新的对象了,并且该新对象与母对象之间并不存在引用共享的 ...

  2. PHP7.X连接SQLSERVER数据库(CENTOS7)

    加入微软的源 curl https://packages.microsoft.com/config/rhel/7/prod.repo > /etc/yum.repos.d/mssqlreleas ...

  3. Go项目中beego的orm使用和gorm的使用

    按照beego官方文档练习ORM的使用,model创建完始终没找到办法创建表,于是使用gorm翻译文档和官方文档进行了练习,使用起来还是比较简单. 安装: 方法一:Terminal打开,go get ...

  4. python小总结4(文件)

    一.读文件 过程: a.打开文件:open() b.读取文件内容:read() readline() readlines() c.关闭文件:close() open(path,flag,encodin ...

  5. 详解Python的作用域和命名空间

    最近在学习Python,不得不说,Python真的是一门很好用的语言.但是学习的过程中关于变量作用域(scope)的命名空间(namespace)的问题真的把我给搞懵了.在查阅了相关资料之后,觉得自己 ...

  6. Linux快捷键总结

    使用Linux很久了,现对经常用到的快捷键做一个总结: 最重要的一个当然是tab了 [root@localhost ~]# cd /etc/sys sysconfig/ sysctl.conf sys ...

  7. python threading模块中的join()方法和setDeamon()方法的一些理解

    之前用多线程的时候看见了很多文章,比较常用的大概就是join()和setDeamon()了. 先说一下自己对join()的理解吧: def join(self, timeout=None): &quo ...

  8. R语言实战基本方法

    R语言的主要功能包括数据统计分析方法和数据可视化,数据分析在这一章中主要学习创建基本图形和基本数据分析 一 基本图形 1条形图 barplot(height)  //添加一个条形图,height是一个 ...

  9. float和position谁好?

    float从字面上的意思就是浮动,float能让元素从文档流中抽出,它并不占文档流的空间,典型的就是图文混排中文字环绕图片的效果了.不过需要注意的是清除浮动是我们可能需要注意的地方.而position ...

  10. type-of-python作业-判断字符串是否属于回文需要忽略其中的标点、空格与大小写

    type-of-python作业 作业练习:要想检查文本是否属于回文需要忽略其中的标点.空格与大小写.例如,"Rise to vote, sir."是一段回文文本,但是我们现有的程 ...