Loading [MathJax]/jax/output/CommonHTML/config.js
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >【leetcode刷题】T52-最长和谐子序列

【leetcode刷题】T52-最长和谐子序列

作者头像
木又AI帮
修改于 2019-07-18 02:06:56
修改于 2019-07-18 02:06:56
55600
代码可运行
举报
文章被收录于专栏:木又AI帮木又AI帮
运行总次数:0
代码可运行

【英文题目】(学习英语的同时,更能理解题意哟~)

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:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
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.

【中文题目】

和谐数组是指一个数组里元素的最大值和最小值之间的差别正好是1。

现在,给定一个整数数组,你需要在所有可能的子序列中找到最长的和谐子序列的长度。

示例 1:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
输入: [1,3,2,2,5,2,3,7]
输出: 5
原因: 最长的和谐数组是:[3,2,2,2,3].

说明: 输入的数组长度最大不超过20,000.

【思路】

对所有元素进行计数,对大小相邻的元素其计数进行求和,取得最大值。

【代码】

python版本

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
class Solution(object):
    def findLHS(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        min0 = sys.maxsize
        max0 = -sys.maxsize
        d = {}
        for n in nums:
            d[n] = d.get(n, ) + 
            min0 = min(min0, n)
            max0 = max(max0, n)
        res = 
        for i in d.keys():
            if i+ in d:
                res = max(res, d[i] + d[i+])
        return res
本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2019-04-27,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 木又AI帮 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
暂无评论
推荐阅读
编辑精选文章
换一批
leetcode-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 subseque
chenjx85
2018/05/22
4390
​LeetCode刷题实战522:最长特殊序列 II
算法的重要性,我就不多说了吧,想去大厂,就必须要经过基础知识和业务逻辑面试+算法面试。所以,为了提高大家的算法能力,这个公众号后续每天带大家做一道算法题,题目就从LeetCode上面选 !
程序员小猿
2022/03/03
2930
​LeetCode刷题实战521: 最长特殊序列 Ⅰ
算法的重要性,我就不多说了吧,想去大厂,就必须要经过基础知识和业务逻辑面试+算法面试。所以,为了提高大家的算法能力,这个公众号后续每天带大家做一道算法题,题目就从LeetCode上面选 !
程序员小猿
2022/03/03
2170
​LeetCode刷题实战516:最长回文子序列
算法的重要性,我就不多说了吧,想去大厂,就必须要经过基础知识和业务逻辑面试+算法面试。所以,为了提高大家的算法能力,这个公众号后续每天带大家做一道算法题,题目就从LeetCode上面选 !
程序员小猿
2022/03/03
2560
​LeetCode刷题实战516:最长回文子序列
打卡群刷题总结0924——最长上升子序列
链接:https://leetcode-cn.com/problems/longest-increasing-subsequence
木又AI帮
2020/09/28
3060
​LeetCode刷题实战491:递增子序列
算法的重要性,我就不多说了吧,想去大厂,就必须要经过基础知识和业务逻辑面试+算法面试。所以,为了提高大家的算法能力,这个公众号后续每天带大家做一道算法题,题目就从LeetCode上面选 !
程序员小猿
2022/03/03
2820
【leetcode刷题】T21-132模式
Given a sequence of n integers a1, a2, …, an, a 132 pattern is a subsequence ai, aj, ak such that i < j < k and ai < ak < aj. Design an algorithm that takes a list of n numbers as input and checks whether there is a 132 pattern in the list.
木又AI帮
2019/07/17
5790
LeetCode 594. 最长和谐子序列(map)
来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/longest-harmonious-subsequence 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
Michael阿明
2020/07/13
4350
LeetCode 594. 最长和谐子序列(map)
【leetcode刷题】T165-最长上升子序列
https://leetcode-cn.com/problems/longest-increasing-subsequence/
木又AI帮
2019/09/17
4510
《剑指offer》第28天:最长上升子序列(高频)
首先我们分析题目,要找的是最长上升子序列(Longest Increasing Subsequence,LIS)。因为题目中没有要求连续,所以 LIS可能是连续的,也可能是非连续的。 同时,LIS符合可以从其子问题的最优解来进行构建的条件。所以我们可以尝试用动态规划来进行求解。首先我们定义状态:
程序员小浩
2020/09/14
4860
【leetcode刷题】T2-3Sum
昨天分享了2sum,今天分享leetcode第2篇文章,第15题—3sum,地址是:https://leetcode.com/problems/3sum/
木又AI帮
2019/07/17
3810
【leetcode刷题】T16-乘积最大子序列
今天分享leetcode第16篇文章,也是leetcode第152题—乘积最大子序列(Maximum Product Subarray),地址是:https://leetcode.com/problems/maximum-product-subarray/
木又AI帮
2019/07/18
5360
【leetcode刷题】T112-验证二叉搜索树
节点的左子树只包含小于当前节点的数。节点的右子树只包含大于当前节点的数。所有左子树和右子树自身必须也是二叉搜索树。
木又AI帮
2019/07/17
5260
594. 最长和谐子序列
和谐数组是指一个数组里元素的最大值和最小值之间的差别 正好是 1 。 现在,给你一个整数数组 nums ,请你在所有可能的子序列中找到最长的和谐子序列的长度。 可以通过删除一些元素或不删除元素、且不改变其余元素的顺序而得到。 示例 1: 输入:nums = [1,3,2,2,5,2,3,7] 输出:5 解释:最长的和谐子序列是 [3,2,2,2,3] 示例 2: 输入:nums = [1,2,3,4] 输出:2 示例 3: 输入:nums = [1,1,1,1] 输出:0
编程张无忌
2021/06/10
2690
[LeetCode]Longest Continuous Increasing Subsequence 最长连续增长序列 [LeetCode]Longest Continuous Incr
链接:https://leetcode.com/problems/longest-continuous-increasing-subsequence/description/ 难度:Easy 题目:674. Longest Continuous Increasing Subsequence Given an unsorted array of integers, find the length of longest continuous increasing subsequence (subarray). Example 1:
尾尾部落
2018/09/04
3890
Data Structures and Algorithms Basics(014):Sliding Window
Sliding Window 目录: 1,删除重复元素 2,删除后,重复值不超过两个 3,删除元素 4,最大均值子数组 5,最长连续递增子序列 6,最短子数组之和 7,实现strStr()函数 8,子数组乘积小于K 9,不含重复字符的最长子串 10,查找重组子串 11,最小窗口子串 12,最多有K个不同字符的最长子串 13,滑动窗口最大值 # 1,删除重复元素: def removeDuplicates(alist): if not alist: return 0 tail
用户5473628
2019/08/08
3930
【Leetcode -575.分糖果 -594.最长和谐子序列】
题目:Alice 有 n 枚糖,其中第 i 枚糖的类型为 candyType[i] 。Alice 注意到她的体重正在增长,所以前去拜访了一位医生。
YoungMLet
2024/03/01
1020
LintCode 最长上升子序列题目分析代码
说明 最长上升子序列的定义: 最长上升子序列问题是在一个无序的给定序列中找到一个尽可能长的由低到高排列的子序列,这种子序列不一定是连续的或者唯一的。https://en.wikipedia.org/wiki/Longest_increasing_subsequence
desperate633
2018/08/22
2630
LintCode 最长上升子序列题目分析
给定一个整数序列,找到最长上升子序列(LIS),返回LIS的长度。 说明最长上升子序列的定义: 最长上升子序列问题是在一个无序的给定序列中找到一个尽可能长的由低到高排列的子序列,这种子序列不一定是连续的或者唯一的。https://en.wikipedia.org/wiki/Longest_increasing_subsequence
desperate633
2018/08/22
2440
LintCode 最长上升子序列题目分析
【leetcode刷题】T50-连续数组
Given a binary array, find the maximum length of a contiguous subarray with equal number of 0 and 1.
木又AI帮
2019/07/17
4100
相关推荐
leetcode-594-Longest Harmonious Subsequence
更多 >
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
本文部分代码块支持一键运行,欢迎体验
本文部分代码块支持一键运行,欢迎体验