[LeetCode&Python] Problem 594. Longest Harmonious Subsequence
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的更多相关文章
- 【Leetcode_easy】594. Longest Harmonious Subsequence
problem 594. Longest Harmonious Subsequence 最长和谐子序列 题意: 可以对数组进行排序,那么实际上只要找出来相差为1的两个数的总共出现个数就是一个和谐子序列 ...
- 594. Longest Harmonious Subsequence - LeetCode
Question 594. Longest Harmonious Subsequence Solution 题目大意:找一个最长子序列,要求子序列中最大值和最小值的差是1. 思路:构造一个map,保存 ...
- 【LeetCode】594. Longest Harmonious Subsequence 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 统计次数 日期 题目地址:https://leetc ...
- LeetCode 594. Longest Harmonious Subsequence (最长的协调子序列)
We define a harmonious array is an array where the difference between its maximum value and its mini ...
- 594. Longest Harmonious Subsequence强制差距为1的最长连续
[抄题]: We define a harmonious array is an array where the difference between its maximum value and it ...
- [LeetCode&Python] Problem 674. Longest Continuous Increasing Subsequence
Given an unsorted array of integers, find the length of longest continuousincreasing subsequence (su ...
- [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 ...
- [LeetCode&Python] Problem 409. Longest Palindrome
Given a string which consists of lowercase or uppercase letters, find the length of the longest pali ...
- 594. Longest Harmonious Subsequence
方法一:用一个map来辅助,代码简单,思路清晰 static int wing=[]() { std::ios::sync_with_stdio(false); cin.tie(NULL); ; }( ...
随机推荐
- 回文的范围——算法面试刷题2(for google),考察前缀和
如果一个正整数的十进制表示(没有前导零)是一个回文字符串(一个前后读取相同的字符串),那么它就是回文.例如,数字5, 77, 363, 4884, 11111, 12121和349943都是回文. 如 ...
- null 和System.DBNull.Value
row[column]的值为DBNull.Value的话,说明它是从数据库中取到值了,对应了数据库中的空值:但如果row[column]的值为null的话,说明没有从数据库中取到值. DBNull.V ...
- git版本控制系统更新
版本控制系统: 一.概念: 版本控制系统(Version Control System):是一种记录一个或若干文件内容变化,以便将来查阅特定版本修订情况的系统. 二.版本控制系统分类 1.本地版本控制 ...
- 用generator 根据oracle表生成java代码,数据库与代码字段不一致
前两天用generator生成java代码时发现,生成的javabean和数据库里的字段对应不上,不是少几个就是有几个字段不一样,感觉很怪异,后来发现日志里边这个表转换成bean是日志打印了好几遍,所 ...
- 记初学python的一些心得
人生苦短,我用python! 其实我自学python也很长一段时间了,但总是去更换学习资料,搞的现在学的不是很好,因为没更换次资料都要从头开始学起,那么分享下我的学习战况吧,不是很好,还将就的能看. ...
- Python面向对象基础知识
面向对象是一种编程方式,此编程方式的实现是基于对类和对象的使用 类是一个模板,模板中包装了多个“函数”供使用(可以讲多函数中公用的变量封装到对象中) 对象,根据模板创建的实例(即:对象),实例用于调用 ...
- C#异常:未将对象引用设置到对象的实例。
异常:未将对象引用设置到对象的实例. 一般是定义的变量或者数组等,没有赋初始值. 赋初始值后问题解决.
- echarts折线图
https://echarts.baidu.com/examples/#chart-type-bar
- 2018工业信息安全技能大赛华东赛区初赛 第2题 writeup
2018工业信息安全技能大赛华东赛区初赛 第2题 解题思路 本题主要考察点是对常见工控协议的理解(modbus/tcp和s7comm),题目目标是寻找出报文中某条异常报文流量.很让人疑惑的是,题目中并 ...
- windows处理PHP定时任务
我用的是bat文件处理定时任务,bat文件是可执行文件,由一系列命令构成,其中可以包含对其他程序的调用 创建一个bat文件,编辑文本,添加需要的php文件,前面路径是你的PHP执行程序,后面路径是文件 ...