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. EAP-MD5计算方法

    一.说明 领导要求确认设备进行802.1X认证时,是否直接将用户名密码明文传给交换机.配好端口镜像.搭好Radius环境后进行了抓包分析. 二.计算 2.1 802.1X认证过程 完整流程如下: 客户 ...

  2. Oracle单机Rman笔记[4]---RMAN联机备份

    备注:RMAN备份(仅支持基于spfile的备份,不支持基于init.ora配置的备份) 练习:开启ARCHIVELOG模式 \为归档的重做日志被指FRA和单独的归档日志目标 SQL>show ...

  3. sprigcloud Eureka Server环境搭建

    1.搭建springcloud的Erueka组件,现在搭建这些套件已经变的很方便了,进入https://start.spring.io/页面,如下图: 2.选择好Eureka Server,点击Gen ...

  4. Robot Framework自动化使用

    自动化测试框架---Robot Framework Robot Framework是用Python语言写的,所以在安装Robot Framework以前必须安装Python环境.Robot Frame ...

  5. Spring Boot:Web 综合开发

    Web 开发 Spring Boot Web 开发非常的简单,其中包括常用的 json 输出.filters.property.log 等 json 接口开发 在以前使用 Spring 开发项目,需要 ...

  6. java课堂笔记

  7. throw与throws

    throws可以单独使用(一直上抛) throw要么和try-catch-finally语句配套使用,要么与throws配套使用 /** * 总结: *    1.throws是方法抛出异常.如: p ...

  8. 最小生成树 HDU1301 (kuskal & prim)

    Kruskal:1.边排序,2.按边从小到大连接森林至树   3.并查集 #include <stdio.h> #include <stdlib.h> #include < ...

  9. 20145338 《网络对抗》 MSF基础应用

    20145338<网络对抗> MSF基础应用 实验内容 ·掌握metasploit的基本应用方式,掌握常用的三种攻击方式的思路. 具体需要完成(1)一个主动攻击;(2)一个针对浏览器的攻击 ...

  10. Android 偏门xml属性

    在 recycleView listview scroview 等等 活动的时候会出现蓝边 android:overScrollMode="never" 用次属性可以去掉 fadi ...