LeetCode [594] Longest Harmonious Subsequence

  • 求一个最长的子序列,使得它的最大值和最小值的差恰好为 1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
/*
* [594] Longest Harmonious Subsequence
*
* https://leetcode.com/problems/longest-harmonious-subsequence/description/
*
* algorithms
* Easy (41.66%)
* Total Accepted: 26.9K
* Total Submissions: 64.5K
* Testcase Example: '[1,3,2,2,5,2,3,7]'
*
* 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.
*
*
*
*/

犯了几个错误……

局部变量没有赋初值……

然后调用了太多次 map 的操作…… 感觉复杂度有点爆炸

事实上这件事情并不需要 map,用 unordered_map 就好了…… 然后这样的话就只需要顺序地扫一遍就可以啦= =