[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); ; }( ...
随机推荐
- C#中dll调用方法
我不创造方法,只做方法的搬运工. https://www.cnblogs.com/Asuphy/p/4206623.html
- Promise 错误处理
Promise 是一个异步返回单个结果的函数或方法 不使用 `catch()` 时,在 `success handler` 里的错误无法被捕捉到 使用 `catch()` 时,在 `succe ...
- apm固定翼调试方法
APM飞控传说是大神的神器新手的噩梦,APM是个便宜又好用的飞控~刚开始给我的天行者X5按APM飞控的时候也查询搜索了很多,参数值,修改和混控和混控量的修改翻遍了资料发现咱们论坛教程比较少,所以开帖总 ...
- oracle数据库 查看被锁定表及解锁方法
最近做项目,遇到了更新超时的问题,反复检查数据都正常,纳闷完了忽然想到是不是表被锁定了,一看果然,解决办法如下: --锁表查询语句 SELECT object_name, machine, s.sid ...
- <Python Text Processing with NLTK 2.0 Cookbook>代码笔记
如下是<Python Text Processing with NLTK 2.0 Cookbook>一书部分章节的代码笔记. Tokenizing text into sentences ...
- Python面向对象基础知识
面向对象是一种编程方式,此编程方式的实现是基于对类和对象的使用 类是一个模板,模板中包装了多个“函数”供使用(可以讲多函数中公用的变量封装到对象中) 对象,根据模板创建的实例(即:对象),实例用于调用 ...
- sorry
登录的时候密码忘了 重置了之后才登录上 这是有多久没登录了 好囧呀 近段时间学习Python也断断续续的 马上春节要到了 随后的20多天里 应该更没有时间学习了 想想都很忧伤 明明想很努力来着 但是总 ...
- 三张图搞懂JavaScript的原型对象与原型链
对于新人来说,JavaScript的原型是一个很让人头疼的事情,一来prototype容易与__proto__混淆,二来它们之间的各种指向实在有些复杂,其实市面上已经有非常多的文章在尝试说清楚,有一张 ...
- 以方法调用的原理解释Ruby中“puts ‘Hello‘”
这里尽管缺少消息发送所需要的点(.)以及该消息的显示接收者,却依然发送了消息puts并传递了参数“Hello”给一个对象:默认对象self.在程序运行期间,虽然作为self的对象通过特定规则发生改变, ...
- 第一周CTF (合天CTF)
0X00 第一周 0x01 基础 50 . 下载下来发现是一个exe文件 ,尝试了一下没打开,然后把后缀名一个一个的试一试,改成txt用记事本打开,得到了一个“假冒的flag” *(别问我怎么知道的) ...