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;
        }
    };
  • 相关阅读:
    走进DOM:HTML DOM
    iOS 去掉UITableView风格为group时候的最顶部的空白距离
    Codeforces 394D Physical Education and Buns 胡搞
    查询出每一个雇员的姓名,工资,部门名称,工资在公司的等级及其领导的姓名,领导的工资,以及领导所相应的等级
    CCBAnimationManager
    sendto 和 recvfrom 函数
    三张图让你高速明确activity与fragment生命周期的异同点
    EWS 流通知订阅邮件
    [EWS]如何: 通过使用 Exchange 中的 EWS 流有关邮箱事件的通知
    async、await正确姿势
  • 原文地址:https://www.cnblogs.com/wdw828/p/6887388.html
Copyright © 2011-2022 走看看