zoukankan      html  css  js  c++  java
  • 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;
        }
    };
  • 相关阅读:
    [dj]Django外键(ForeignKey)操作以及related_name的作用
    [java]快速创建servlet和springmvc测试项目
    [sql]mybatis中动态sql
    [sql]mysql中使用 where 1=1和 0=1 的作用及好处
    [sql]sql中分组查询/union/join
    JavaScripts学习日记——DOM SAX JAXP DEMO4J XPath
    JavaScripts学习日记——XML DTD Schema
    JavaScripts学习日记——DOM
    JavaScripts学习日记——BOM
    JavaScripts学习日记——ECMAscript
  • 原文地址:https://www.cnblogs.com/wdw828/p/6887388.html
Copyright © 2011-2022 走看看