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. mysql权限参考

    mysql日常管理和应用工作中,大家经常会涉及到授权问题,下面,我们就列举下权限相关的参考. 1.管理权限(Administrative Privileges) Privilege Name      ...

  2. Cognos命名空间不可用

    1. 问题描述 启动Cognos失败,报错代码为0146. 2. 问题分析 namespace 配置有问题,检查configuration 3. 解决方案 如果检查不出问题,删除$COGNOS_HOM ...

  3. MSDN订户下载权限被屏蔽的办法

    使用Chrome浏览器,在加载完成页面之后,按F12,在控制台选项卡当中输入下面代码,即可解除屏蔽. $("#SubMigratedMessageArea").remove(); ...

  4. S2T40,第四章,简答5

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  5. ImportError libcublas.so.9.0

    What to do when you've installed cuda and tensorflow, but you get this error right after you import ...

  6. node-express-1

    安装: express v4.0以后的安装: npm install express-generator -g 建立项目 express -t ejs blog 安装依赖 cd blog && ...

  7. JS中一些常用的兼容写法

    1.滚动条到顶端的距离var scrollTop = document.documentElement.scrollTop || document.body.scrollTop;2.滚动条到左端的距离 ...

  8. js求最大值最小值

    比较数组中数值的大小是比较常见的操作,比较大小的方法有多种,比如可以使用自带的sort()函数,代码如下: <html> <head> <meta charset=&qu ...

  9. IoT experitment

    Abstract: In order to solve the problems of complex experiment management, complicated teaching task ...

  10. jQuery对象与DOM对象互相转换

    1.jQuey对象转DOM对象 a. [index]   代码如下: var $a = $("#a"); //jQuery对象 var a = $a[]; //DOM对象 b. g ...