• 594. Longest Harmonious Subsequence


    Problem statement:

    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].

    Solution one: two pointers, sliding window(AC)

    This is the first problem of leetcode weekly contest 33. It asks the longest of harmonious subsequence. The definition of harmonious subsequence is: the difference between its maximum value and its minimum value is exactly 1. 

    As what described, such as subsequence, max and min value, the relative position of each element in harmonious subsequence does not matter. We can sort the array by ascending order and use two pointers(left and right) behaving like a sliding window to find the max length harmonious subsequence from the beginning to end. 

    The basic idea:

    • Sort the array in ascending order. O(nlogn)
    • Two pointers: left = 0, right = 0; loop from the beginning to end. O(n)
      • nums[right] - nums[left] == 1 ---> right++ && update the max length
      • nums[right] - nums[left] > 1 ---> left++
      • nums[right] - nums[left] < 1 ---> right++

    Time complexity O(nlogn). The most time consuming step is sorting. Space complexity is O(1).

    class Solution {
    public:
        int findLHS(vector<int>& nums) {
            sort(nums.begin(), nums.end());
            int left = 0;
            int right = 0;
            int max_len = 0;
            while(left <= right && right < nums.size()){
                if(nums[right] - nums[left] == 1){
                    max_len = max(max_len, right - left + 1);
                    right++;
                } else if(nums[right] - nums[left] < 1){
                    right++;
                } else {
                    left++;
                }
            }
            return max_len;
        }
    };

    Solution two: hash table without sorting(AC)

    This idea comes from longest harmonious subsequence article in leetcode. It reduces the time complexity to O(n) at the cost of O(n) of space complexity.

    It employs a hash table(<value, # of this value>) to find the answer.

    The basic idea:

    • Loop from the beginning to end, count the number of a value.
    • Loop the hash table, find whether the value which is greater 1 than current value in hash table exists. 
      • Yes, update the max length by the sum of the # of two values.
      • No, continue.

    Time complexity is O(n). Space complexity is O(n).

    class Solution {
    public:
        // this is hash table version
        int findLHS(vector<int>& nums) {
            unordered_map<int, int> hash_table;
            for(auto num : nums){
                hash_table[num]++;
            }
            // find the element which is just greater 1 than current element in hash table
            // update the max len by the sum of their count
            int max_len = 0;
            for(auto it : hash_table){
                if(hash_table.count(it.first + 1)){
                    max_len = max(max_len, it.second + hash_table[it.first + 1]);
                }
            }
            return max_len;
        }
    };
  • 相关阅读:
    微软企业库4.1学习笔记(十一)企业库的核心类 Virus
    微软企业库4.1学习笔记(七)创建对象 续集1 Virus
    微软企业库4.1学习笔记(五)对象创建和依赖注入方法 Virus
    微软企业库4.1学习笔记(十六)缓存模块4 服务器场中的缓存使用 Virus
    Android知识汇总
    移动UI研究学习汇总
    iOS独立存储Demo(调试过可以运行)
    The Official Preppy Handbook 目录
    一个好的App架构应该包括什么?
    转身再不仅仅只是开发人员
  • 原文地址:https://www.cnblogs.com/wdw828/p/6887388.html
Copyright © 2020-2023  润新知