[LeetCode] 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.
这道题给了我们一个数组,让我们找出最长的和谐子序列,关于和谐子序列就是序列中数组的最大最小差值均为1。由于这里只是让我们求长度,并不需要返回具体的子序列。所以我们可以对数组进行排序,那么实际上我们只要找出来相差为1的两个数的总共出现个数就是一个和谐子序列的长度了。明白了这一点,我们就可以建立一个数字和其出现次数之间的映射,利用 TreeMap 的自动排序的特性,那么我们遍历 TreeMap 的时候就是从小往大开始遍历,我们从第二个映射对开始遍历,每次跟其前面的映射对比较,如果二者的数字刚好差1,那么就把二个数字的出现的次数相加并更新结果 res 即可,参见代码如下:
解法一:
class Solution {
public:
int findLHS(vector<int>& nums) {
if (nums.empty()) return ;
int res = ;
map<int, int> m;
for (int num : nums) ++m[num];
for (auto it = next(m.begin()); it != m.end(); ++it) {
auto pre = prev(it);
if (it->first == pre->first + ) {
res = max(res, it->second + pre->second);
}
}
return res;
}
};
其实我们并不用向上面那种解法那样用 next 和 prev 来移动迭代器,因为其用到了 TreeMap 的自动排序功能,所以才可以利用 next 和 prev。其实我们还可以用 HashMap 来做,先遍历一遍,建立每个数字跟其出现次数之间的映射,然后再遍历每个数字的时候,只需在 HashMap 中查找该数字加1是否存在,存在就更新结果 res,这样更简单一些,参见代码如下:
解法二:
class Solution {
public:
int findLHS(vector<int>& nums) {
int res = ;
unordered_map<int, int> m;
for (int num : nums) ++m[num];
for (auto a : m) {
if (m.count(a.first + )) {
res = max(res, m[a.first] + m[a.first + ]);
}
}
return res;
}
};
我们其实也可以在一个 for 循环中搞定,遍历每个数字时,先累加其映射值,然后查找该数字加1是否存在,存在的话用 m[num] 和 m[num+1] 的和来更新结果 res,同时,还要查找该数字减1是否存在,存在的话用 m[num] 和 m[num-1] 的和来更新结果 res,这样也是可以的,参见代码如下:
解法三:
class Solution {
public:
int findLHS(vector<int>& nums) {
int res = ;
unordered_map<int, int> m;
for (int num : nums) {
++m[num];
if (m.count(num + )) {
res = max(res, m[num] + m[num + ]);
}
if (m.count(num - )) {
res = max(res, m[num] + m[num - ]);
}
}
return res;
}
};
下面方法不用任何 map,但是需要对数组进行排序,当数组有序了之后,我们就可以一次遍历搞定了。这实际上用到了滑动窗口 Sliding Window 的思想,用变量 start 记录当前窗口的左边界,初始化为0。用 new_start 指向下一个潜在窗口的左边界,初始化为0。i为当前窗口的右边界,从1开始遍历,首先验证当前窗口的差值是否小于1,用 nums[i] 减去 nums[start],若不满足,则将 start 赋值为 new_start,即移动到下一个窗口。然后看当前数字跟之前一个数字是否相等,若不相等,说明当前数字可能是下一个潜在窗口的左边界,将 new_start 赋值为i。然后再看窗口的左右边界值是否刚好为1,因为题目中说了差值必须正好为1,由于我们对数组排序了,所以只要左右边界差值正好为1,那么这个窗口包含的数字就可以组成满足题意的子序列,用其长度来更新结果 res 即可,参见代码如下:
解法四:
class Solution {
public:
int findLHS(vector<int>& nums) {
int res = , start = , new_start = ;
sort(nums.begin(), nums.end());
for (int i = ; i < nums.size(); ++i) {
if (nums[i] - nums[start] > ) start = new_start;
if (nums[i] != nums[i - ]) new_start = i;
if (nums[i] - nums[start] == ) res = max(res, i - start + );
}
return res;
}
};
参考资料:
https://leetcode.com/problems/longest-harmonious-subsequence/
LeetCode All in One 题目讲解汇总(持续更新中...)
[LeetCode] Longest Harmonious Subsequence 最长和谐子序列的更多相关文章
- [LeetCode] Longest Increasing Subsequence 最长递增子序列
Given an unsorted array of integers, find the length of longest increasing subsequence. For example, ...
- poj 2533 Longest Ordered Subsequence 最长递增子序列
作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4098562.html 题目链接:poj 2533 Longest Ordered Subse ...
- leetcode300. Longest Increasing Subsequence 最长递增子序列 、674. Longest Continuous Increasing Subsequence
Longest Increasing Subsequence 最长递增子序列 子序列不是数组中连续的数. dp表达的意思是以i结尾的最长子序列,而不是前i个数字的最长子序列. 初始化是dp所有的都为1 ...
- lintcode 77.Longest Common Subsequence(最长公共子序列)、79. Longest Common Substring(最长公共子串)
Longest Common Subsequence最长公共子序列: 每个dp位置表示的是第i.j个字母的最长公共子序列 class Solution { public: int findLength ...
- C#LeetCode刷题之#594-最长和谐子序列(Longest Harmonious Subsequence)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3800 访问. 和谐数组是指一个数组里元素的最大值和最小值之间的差 ...
- LeetCode Longest Harmonious Subsequence
原题链接在这里:https://leetcode.com/problems/longest-harmonious-subsequence/description/ 题目: We define a ha ...
- [LeetCode] Longest Palindromic Subsequence 最长回文子序列
Given a string s, find the longest palindromic subsequence's length in s. You may assume that the ma ...
- LeetCode 300. Longest Increasing Subsequence最长上升子序列 (C++/Java)
题目: Given an unsorted array of integers, find the length of longest increasing subsequence. Example: ...
- [LeetCode] 300. Longest Increasing Subsequence 最长递增子序列
Given an unsorted array of integers, find the length of longest increasing subsequence. Example: Inp ...
随机推荐
- java日期格式大全 format SimpleDateFormat
http://www.cnblogs.com/loveyakamoz/archive/2011/08/18/2145047.html
- SQL中哪些情况会引起全表扫描
1.模糊查询效率很低:原因:like本身效率就比较低,应该尽量避免查询条件使用like:对于like '%...%'(全模糊)这样的条件,是无法使用索引的,全表扫描自然效率很低:另外,由于匹配算法的关 ...
- [日常] Codeforces Round #440 Div.2 大力翻车实况
上次打了一发ABC然后大力翻车...上午考试又停电+Unrated令人非常滑稽...下午终于到了CF比赛... 赛前大力安利了一发然后拉了老白/ $ljm$ / $wcx$ 一起打, 然后搞了个 TI ...
- Java基础学习笔记二 Java基础语法
注释 注释用来解释和说明程序的文字,注释是不会被执行的. 单行注释 //这是一条单行注释 public int i; 多行注释 /* 这是 * 一段注释, * 它跨越了多个行 */ public vo ...
- java基础之类与对象
[类 & 对象] 1.类:具有一系列相同属性(特征)和方法(行为)的个体的集合,称为类. 类是一个抽象的概念,只能说类具有哪些属性,而不能直接对属性进行赋值. 例如:人类有身高的属性,但是不能 ...
- drbd(四):drbd多节点(drbd9)
1.drbd多节点简介 在drbd9以前,drbd一直只能配置两个节点,要么是primary/secondary,要么是primary/primary.虽然在这些版本上也能配置第三个节点实现三路节点的 ...
- 代码中输入数字自动筛选出最大值,使用array,for loop and if (21.9.2017)
# include <stdio.h> # define N main(){ int a, b; ,,,,,,,,,,,,,,,,}; //array中输入需要排序的数字 ]; ; a & ...
- C语言——第一次作业(2)
1.写程序证明p++等价于(p)++还是等价于*(p++)? #include <stdio.h> int main() { int *p,a=5; p = &a; printf( ...
- JAVA_SE基础——57.有了包之后类与类之间的访问使用import语句
代码1访问代码2 代码1: class Demo3 { public static void main(String[] args) { Demo4 a = new Demo4(); a.print( ...
- 一个诚实的孩纸选Python的原因
我之所以会选择python语言程序设计这门课,是因为我一开始预选选的选修课都没选上,然后在补选的时候,在别人选剩的课里面选择了python. 上了两节课之后,我发现python还挺有意思的,挺喜欢py ...