首页 > 其他分享 >leetcode 594. Longest Harmonious Subsequence 最长和谐子序列(简单).md

leetcode 594. Longest Harmonious Subsequence 最长和谐子序列(简单).md

时间:2022-08-24 23:45:21浏览次数:185  
标签:md 594 nums 和谐 priorityQueue 数组 序列 Map leetcode

一、题目大意

https://leetcode.cn/problems/longest-harmonious-subsequence

和谐数组是指一个数组里元素的最大值和最小值之间的差别 正好是 1 。

现在,给你一个整数数组 nums ,请你在所有可能的子序列中找到最长的和谐子序列的长度。

数组的子序列是一个由数组派生出来的序列,它可以通过删除一些元素或不删除元素、且不改变其余元素的顺序而得到。

示例 1:

输入:nums = [1,3,2,2,5,2,3,7]
输出:5
解释:最长的和谐子序列是 [3,2,2,2,3]

示例 2:

输入:nums = [1,2,3,4]
输出:2

示例 3:

输入:nums = [1,1,1,1]
输出:0

提示:

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

二、解题思路

思路:题目给我们一个数组,让我们找出最长的和谐子序列,和谐子序列就是序列中数组的最大最小差值均为1,这里只让我们求长度,而不需要返回具体的子序列。所以我们可以对数组进行排序,实际上只要找出来相差为1的两个数的总共出现个数就是一个和谐子序列长度了。

三、解题方法

3.1 Java实现

public class Solution {
    public int findLHS(int[] nums) {
        Map<Integer, Integer> countMap = new HashMap<>();
        for (int num : nums) {
            countMap.put(num, countMap.getOrDefault(num, 0) + 1);
        }

        // a-b由小到大 b-a由大到小
        PriorityQueue<Map.Entry<Integer, Integer>> priorityQueue
                = new PriorityQueue<>(Comparator.comparingInt(Map.Entry::getKey));
        for (Map.Entry<Integer, Integer> entry : countMap.entrySet()) {
            priorityQueue.add(entry);
        }

        int ans = 0;
        Map.Entry<Integer, Integer> pre = priorityQueue.poll();
        while (!priorityQueue.isEmpty()) {
            Map.Entry<Integer, Integer> cur = priorityQueue.poll();
            if (cur.getKey() - pre.getKey() == 1) {
                ans = Math.max(ans, cur.getValue() + pre.getValue());
            }
            pre = cur;
        }

        return ans;
    }
}

四、总结小记

  • 2022/8/24 什么时候能水果自由呀

标签:md,594,nums,和谐,priorityQueue,数组,序列,Map,leetcode
From: https://www.cnblogs.com/okokabcd/p/16622701.html

相关文章

  • cmd--常用cmd命令
    netshwlanshowprofiles------查看当前系统已经保存的网络netshwlanshowprofilename="网络名称"key=clear-----------查看指定WiFi密码netuser---------查看此系......
  • LeetCode 重排链表算法题解 All In One
    LeetCode重排链表算法题解AllInOnejs/ts实现重排链表重排链表原理图解//快慢指针重排链表https://leetcode.com/problems/reorder-list/https://le......
  • leetcode-1460. 通过翻转子数组使两个数组相等
    1460.通过翻转子数组使两个数组相等图床:blogimg/刷题记录/leetcode/1460/刷题代码汇总:https://www.cnblogs.com/geaming/p/16428234.html题目思路首先,这是一道“简......
  • leetcode150:逆波兰表达式求值
    packagecom.mxnet;importjava.util.Stack;publicclassSolution150{publicstaticvoidmain(String[]args){}/***根据逆波兰表示法,......
  • wwm.LeetCodeHelper C#刷题帮助类库
    wwm.LeetCodeHelper仓库地址:https://gitee.com/wwmin/www.leetcode.helper1.说明wwm.LeetCodeHelper是一款帮助在本地用C#做LeetCode题的一个库,具有自动拉取题生成c......
  • LeetCode 21 合并两个有序链表
    /***Definitionforsingly-linkedlist.*structListNode{*intval;*ListNode*next;*ListNode():val(0),next(nullptr){}*ListN......
  • vala.md
    Vala目录Valahelloworldhelloworld文件:meson.buildproject('vala-layer-shell',['vala','c'],version:'0.1.0',license:'MIT',meson_version:'>=0.......
  • 动态规划——leetcode55、跳跃游戏
    题目描述: 解题方法:动态规划动态规划解题步骤:确定状态:最后一步:如果能跳到最后一个下标,我们考虑他的最后一步到n-1(最后一个下标),这一步是从i跳过......
  • #前端算法救赎系列#LeetCode01.两数之和
    1.两数之和给定一个整数数组nums和一个整数目标值target,请你在该数组中找出和为目标值target的那两个整数,并返回它们的数组下标。示例1:输入:nums=[2,7,11,1......
  • LeetCode 142. 环形链表 II
    思路:快慢指针法:当快指针与慢指针相遇时,分别从起点,相遇点开始走,相遇即为环入口/***Definitionforsingly-linkedlist.*structListNode{*intval;*......