• LeetCode 594. Longest Harmonious Subsequence


    We define a harmonious array as an array where the difference between its maximum value and its minimum value is exactly 1.

    Given an integer array nums, return the length of its longest harmonious subsequence among all its possible subsequences.

    subsequence of array is a sequence that can be derived from the array by deleting some or no elements without changing the order of the remaining elements.

    Example 1:

    Input: nums = [1,3,2,2,5,2,3,7]
    Output: 5
    Explanation: The longest harmonious subsequence is [3,2,2,2,3].
    

    Example 2:

    Input: nums = [1,2,3,4]
    Output: 2
    

    Example 3:

    Input: nums = [1,1,1,1]
    Output: 0
    

    Constraints:

    • 1 <= nums.length <= 2 * 104
    • -109 <= nums[i] <= 109

    这题是要求一个数组里,最大和最小元素之差为1的subsequence的最长长度。想得太复杂了因为想到了以前头大的longest subsequence系列问题。看了答案才知道其实很简单,其实就是求这个数组里相差为1的数字的个数的max。

    1. Brute force

    首先是brute force的做法,对数组里每个元素都进行一遍操作:遍历整个数组,如果遇到了和它一样的或者比他大1的,就算进去。这里刚开始想着我既可以小1也可以大1,但其实因为在内层for loop里是又扫了一遍整个数组(对,注意这里要再扫一次整个数组),所以其实小1的情况肯定也会被cover进来,就只用看大1就行。另外还需要一个boolean flag来记录是否存在比它大1的,否则可能全都是同一个元素,这种也不算。然鹅这个做法TLE了。O(n^2)

    1. class Solution {
    2. public int findLHS(int[] nums) {
    3. int result = 0;
    4. for (int i = 0; i < nums.length; i++) {
    5. int count = 0;
    6. boolean found = false;
    7. for (int j = 0; j < nums.length; j++) {
    8. if (nums[j] == nums[i] + 1) {
    9. found = true;
    10. count++;
    11. } else if (nums[j] == nums[i]) {
    12. count++;
    13. }
    14. }
    15. if (found) {
    16. result = Math.max(result, count);
    17. }
    18. }
    19. return result;
    20. }
    21. }

    2. sorting

    刚开始想岔了,因为想着sebsequence是和顺序有关的所以不能sort……嗯……这里因为不care数字大小的顺序所以sort是没问题的。sort完以后,我们就只需要按顺序计算相邻的两个数字一共有多少个。这里要写出clean code也需要一点小技巧,which我刚开始就没get到。这里一共有两种情况,一种情况是当前这个数字是前面数字的+1,这时候我们就计算有多少个这个数字,然后再和前面的个数相加,和result比大小。另一种情况是当前这个数字是一个全新的开始,这时候只需要计算有多少个这个数字就行。所以是if else里面还要套一层while loop来算个数。外层看数字+1的时候和前面的数字比,内层计算个数的时候和后面的数字比。O(nlogn)

    Runtime: 16 ms, faster than 97.92% of Java online submissions for Longest Harmonious Subsequence.

    Memory Usage: 54.6 MB, less than 70.64% of Java online submissions for Longest Harmonious Subsequence.

    1. class Solution {
    2. public int findLHS(int[] nums) {
    3. int result = 0;
    4. Arrays.sort(nums);
    5. int prevCount = 0;
    6. for (int i = 0; i < nums.length; i++) {
    7. int count = 1; // the current number
    8. // if it's the first one for the +1 number
    9. if (i > 0 && nums[i] - nums[i - 1] == 1) {
    10. while (i + 1 < nums.length && nums[i + 1] == nums[i]) {
    11. i++;
    12. count++;
    13. }
    14. result = Math.max(result, count + prevCount);
    15. } else {
    16. // if it's a fresh start
    17. while (i + 1 < nums.length && nums[i + 1] == nums[i]) {
    18. count++;
    19. i++;
    20. }
    21. }
    22. prevCount = count;
    23. }
    24. return result;
    25. }
    26. }

    3. HashMap

    就,其实更简单了……就是计算这个数组里相邻的数字的个数之和max,那就用个hashmap记录每个数字出现的次数就行了。然后遍历一遍这个map的keyset,看看key + 1是否也在map里,如果在的话加加看有多少就行。O(n)。

    Runtime: 44 ms, faster than 46.59% of Java online submissions for Longest Harmonious Subsequence.

    Memory Usage: 67.1 MB, less than 40.93% of Java online submissions for Longest Harmonious Subsequence.

    1. class Solution {
    2. public int findLHS(int[] nums) {
    3. int result = 0;
    4. Map<Integer, Integer> map = new HashMap<>();
    5. for (int n : nums) {
    6. map.put(n, map.getOrDefault(n, 0) + 1);
    7. }
    8. for (int key : map.keySet()) {
    9. if (map.containsKey(key + 1)) {
    10. result = Math.max(result, map.get(key) + map.get(key + 1));
    11. }
    12. }
    13. return result;
    14. }
    15. }

    4. 改进版hashmap

    就是把上面那个的两个for loop简化成一个for loop,一遍put map的时候一边更新result。put完以后看看map里有没有比它大1或者小1的数字,如果有就加上它的count。这里需要判断both +1和-1因为是按照原数组的排列顺序来的。也是O(n)

    Runtime: 59 ms, faster than 20.81% of Java online submissions for Longest Harmonious Subsequence.

    Memory Usage: 68 MB, less than 25.78% of Java online submissions for Longest Harmonious Subsequence.

    1. class Solution {
    2. public int findLHS(int[] nums) {
    3. int result = 0;
    4. Map<Integer, Integer> map = new HashMap<>();
    5. for (int n : nums) {
    6. map.put(n, map.getOrDefault(n, 0) + 1);
    7. if (map.containsKey(n + 1)) {
    8. result = Math.max(result, map.get(n) + map.get(n + 1));
    9. }
    10. if (map.containsKey(n - 1)) {
    11. result = Math.max(result, map.get(n) + map.get(n - 1));
    12. }
    13. }
    14. return result;
    15. }
    16. }

  • 相关阅读:
    投标管理与工程实施管理的关键步骤及策略
    MySQL——进阶操作
    高性能队列Disruptor使用教程
    IDEA导入Eclipse项目的方法步骤(图文教程)
    人工智能、深度学习、机器学习常见面试题281~300
    游戏d3dcompiler_43.dll缺失怎么修复,分享5个快速有效的修复方法
    【novelai】colab存档
    Python如何远程连接设置密码的MongoDB库
    【搜索】—— 迭代加深/双向DFS/IDA*
    Redis安装教程
  • 原文地址:https://blog.csdn.net/qq_37333947/article/details/127653367