LeetCode Longest Harmonious Subsequence
原题链接在这里:https://leetcode.com/problems/longest-harmonious-subsequence/description/
题目:
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.
题解:
在HashMap<Integer, Integer> hm中计数nums中每个element出现次数.
再看hm的key加一是否也在hm中能找到, 若能计算个数sum更新res.
Time Complexity: O(n). Space: O(n).
AC Java:
class Solution {
public int findLHS(int[] nums) {
if(nums == null || nums.length == 0){
return 0;
}
int res = 0;
HashMap<Integer, Integer> hm = new HashMap<Integer, Integer>();
for(int n : nums){
hm.put(n, hm.getOrDefault(n, 0)+1);
}
for(int i : hm.keySet()){
if(hm.containsKey(i+1)){
res = Math.max(res, hm.get(i)+hm.get(i+1));
}
}
return res;
}
}
LeetCode Longest Harmonious Subsequence的更多相关文章
- [LeetCode] Longest Harmonious Subsequence 最长和谐子序列
We define a harmonious array is an array where the difference between its maximum value and its mini ...
- LeetCode 594. Longest Harmonious Subsequence (最长的协调子序列)
We define a harmonious array is an array where the difference between its maximum value and its mini ...
- 【LeetCode】594. Longest Harmonious Subsequence 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 统计次数 日期 题目地址:https://leetc ...
- [LeetCode&Python] Problem 594. Longest Harmonious Subsequence
We define a harmonious array is an array where the difference between its maximum value and its mini ...
- C#LeetCode刷题之#594-最长和谐子序列(Longest Harmonious Subsequence)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3800 访问. 和谐数组是指一个数组里元素的最大值和最小值之间的差 ...
- 594. Longest Harmonious Subsequence - LeetCode
Question 594. Longest Harmonious Subsequence Solution 题目大意:找一个最长子序列,要求子序列中最大值和最小值的差是1. 思路:构造一个map,保存 ...
- [Swift]LeetCode594. 最长和谐子序列 | 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_easy】594. Longest Harmonious Subsequence
problem 594. Longest Harmonious Subsequence 最长和谐子序列 题意: 可以对数组进行排序,那么实际上只要找出来相差为1的两个数的总共出现个数就是一个和谐子序列 ...
随机推荐
- PAT 天梯赛 L1-040. 最佳情侣身高差 【水】
题目链接 https://www.patest.cn/contests/gplt/L1-040 AC代码 #include <iostream> #include <cstdio&g ...
- Safari通过JavaScript获取系统语言
IE6 IE7 IE8 Firefox Chrome Safari Opera navigator.language undefined zh-CN zh-CN navigator.userLan ...
- 转:MFC中屏蔽ESC和回车关闭对话框
解决方法是在 CDialog::PreTranslateMessage() 的重载函数中将ESC和回车按键的消息处理掉. 直接上代码: CResultCollectorDlg::PreTranslat ...
- DEC VT100 terminal
- 【LeetCode】【定制版排序】Sort Colors
之前转载过一篇STL的sort方法底层详解的博客:https://www.cnblogs.com/ygh1229/articles/9806398.html 但是我们在开发中会根据自己特定的应用,有新 ...
- python 课堂笔记-for语句
for i in range(10): print("----------",i) for j in range(10): print("world",j) i ...
- Java技术路线
1.计算机基础: 1.1数据机构基础: 主要学习: 1.向量,链表,栈,队列和堆,词典.熟悉 2.树,二叉搜索树.熟悉 3.图,有向图,无向图,基本概念 4.二叉搜索A,B,C类熟练,9大排序熟悉. ...
- STP生成树协议原理与算法解析
转:https://wenku.baidu.com/view/2e52b91d866fb84ae45c8d34.html
- IPU VPU GPU的关系
转:https://blog.csdn.net/dragon101788/article/details/78404669 Video graphics system[IPU, VPU and GPU ...
- UvaLive 5811 概率DP
题意 : 有54张牌 问抽多少张牌能使每种花色都至少是给定的数字 两张王牌可以被选择为任何花色 高放学长真是太腻害辣! 设置dp[][][][][x][y] 前四维代表四种真的花色的数量 后两维xy代 ...