[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); ; }( ...
随机推荐
- Oracle中 “ORA-14551: 无法在查询中执行 DML 操作” 如何解决
在编写一个数据库函数时,方法实现需要查询后进行修改,出现ora-14551的错误 create or replace function fun_DxcBillSn(tabType integer,ta ...
- Vasya And Password(CodeForces - 1051A)
Vasya came up with a password to register for EatForces — a string ss. The password in EatForces sho ...
- 汽车行业解决方案_K2助力车企实现费控/生产“端到端流程”
如今汽车行业正面对一轮全球范围内新变革周期,这种“变革”一方面来源于在新能源技术.人工智能.信息技术.物联网技术等高新科技地猛烈敲击,另一方面源于全球的经济政策变幻莫测,贸易保护时代地到来,车企深陷发 ...
- iptables介绍
iptables防火墙可以用于创建过滤(filter)与NAT规则.所有Linux发行版都能使用iptables. iptables的结构:iptables-->Tables-->Chai ...
- 详细介绍Ubuntu 16.04系统环境安装Docker CE容器的过程
由于项目的需要,我们在很多软件配置环境中需要用到Docker容器,这个时候我们可以用自己的VPS主机搭建.在这篇文章中,笔者将会利用Ubuntu 16.04系统环境安装Docker CE容器的过程.如 ...
- python简单爬虫 用lxml库解析数据
目标:爬取湖南大学2018年本科招生章程 url:http://admi.hnu.edu.cn/info/1026/2993.htm 页面部分图片: 使用工具: Python3.7 火狐浏览器 PyC ...
- flv文件解析(纯c解析代码)
参考链接: 1. FLV科普12 FLV脚本数据解析-Metadata Tag解析 https://blog.csdn.net/cabbage2008/article/details/50500021 ...
- Python之字典方法
def clear(self): # 清除所有内容 """ D.clear() -> None. Remove all items from D. "&q ...
- Problem 4: Largest palindrome product
A palindromic number reads the same both ways. The largest palindrome made from the product of two 2 ...
- java富文本编辑器KindEditor
在页面写一个编辑框: <textarea name="content" class="form-control" id="content&quo ...