leetcode 594. Longest Harmonious Subsequence | 594. 最长和谐子序列
题目
https://leetcode-cn.com/problems/longest-harmonious-subsequence/
题解
我的解法
测试用例
[1]
[1,2]
[2,1]
[3,1]
[3,1,1]
[3,5,5]
[1,3,5,7]
[2,2]
[1,2,1,2,1,2,1,2,1,2]
[3,3,3,2,2,2,1,1,1]
[3,3,2]
[1,3,2,2,5,2,3,7]
import java.util.Arrays;class Solution {public int findLHS(int[] nums) {if (nums.length == 1) return 0;Arrays.sort(nums);int maxSum = 0;int numPre = Integer.MAX_VALUE;int cntPre = 0;int cntCur = 1;for (int i = 0; i < nums.length - 1; i++) {if (nums[i] == nums[i + 1]) {cntCur++;} else {if (numPre + 1 == nums[i]) { // update maxSum only when it's continuousmaxSum = Math.max(maxSum, cntPre + cntCur);}cntPre = cntCur;cntCur = 1;numPre = nums[i];}}// boundary: the last numberif (nums[nums.length - 2] != nums[nums.length - 1]) {cntPre = cntCur;cntCur = 1;}if (numPre + 1 == nums[nums.length - 1]) { // update maxSum only when it's continuousmaxSum = Math.max(maxSum, cntPre + cntCur);}return maxSum;}
}
评论区优雅解法
排序后,用两个指针,类似于窗口
class Solution {public int findLHS(int[] nums) {Arrays.sort(nums);int begin = 0,res = 0;for(int end = 0;end < nums.length;end++){while(nums[end] - nums[begin] > 1)begin++;if(nums[end] - nums[begin] == 1)res = Math.max(res,end - begin + 1);}return res;}
}
leetcode 594. Longest Harmonious Subsequence | 594. 最长和谐子序列相关推荐
- leetcode 300. Longest Increasing Subsequence | 300. 最长递增子序列(动态规划)
题目 https://leetcode.com/problems/longest-increasing-subsequence/ 题解 难得有官方题解的一道题. 参考:https://leetcode ...
- leetcode 1143. Longest Common Subsequence | 1143. 最长公共子序列(动态规划,暴力递归->傻缓存->dp)
题目 https://leetcode.com/problems/longest-common-subsequence/ 题解 经典的 暴力递归 -> 傻缓存 -> dp 题目,以 &qu ...
- leetcode (Longest Harmonious Subsequence)
Title:Longest Harmonious Subsequence 594 Difficulty:Easy 原题leetcode地址: https://leetcode.com/probl ...
- LeetCode 594. Longest Harmonious Subsequence
题目: We define a harmonious array is an array where the difference between its maximum value and its ...
- 594. Longest Harmonious Subsequence
本题题意: 在一个数组中,找一个这样的和谐数组,他们的最大值和最小值的差值正好是1. Input: [1,3,2,2,5,2,3,7] Output: 5 Explanation: The longe ...
- leetcode(300)—— Longest Increasing Subsequence(最长递增子序列)
参考 Python 解法: 动态规划 -- 最长递增子序列(LIS) 原题位置:Longest Increasing Subsequence | LeetCode OJ 题目的说明: 严格递增: 子序 ...
- leetcode 516. Longest Palindromic Subsequence | 516. 最长回文子序列(递归 -> 傻缓存 ->DP)
题目 https://leetcode.com/problems/longest-palindromic-subsequence/ 题解 1.递归(超时) 递归 -> 傻缓存 ->DP c ...
- C#LeetCode刷题之#594-最长和谐子序列(Longest Harmonious Subsequence)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3800 访问. 和谐数组是指一个数组里元素的最大值和最小值之间的差 ...
- Leetcode 594. 最长和谐子序列 C++
Leetcode 594. 最长和谐子序列 题目 和谐数组是指一个数组里元素的最大值和最小值之间的差别正好是1. 现在,给定一个整数数组,你需要在所有可能的子序列中找到最长的和谐子序列的长度. 示例: ...
最新文章
- makefile使用宏及用法$(宏标识符) $(cc)_宏编程的艺术
- WebBrowser加载完毕后再往下执行
- hive 中某个字段等于0_快速了解hive
- [算法笔记]分块算法从入门到TLE
- spark 的RDD各种转换和动作
- fastjson 序列化 不包括转义字符_fastjson漏洞复现
- cocos2d cocostudio
- python 官方 中文 文档
- linux合并ts文件吗,UNIX LINUX MACOS shell 下载合并*.ts视频
- 难崩日记——从入门到入土的求生之路(二):文件上传中的路径问题
- Python 三大神器:pip,virtualenv(virtualenvwrapper),fabric
- 给销售组织分配分销渠道
- 计算机科学与技术双一流排名,计算机科学与技术学科排行榜(大学名单大全2020版)...
- C# 使用Microsoft Speech朗读文本
- 霹雳吧啦Wz语义分割学习笔记P11
- 来自腾讯的高性能服务器架构思路
- 中文分词-转载3_一个北京程序员
- TCL电子上半年收入同比增103.7%;中芯国际二季度营收同比大增43%;友成基金会与亚马逊中国联合开展公益项目 | 全球TMT...
- 轻松快捷的安装Testlink,终于可以轻松搞定!
- HR面试经验总结 | HR面试常问问题
热门文章
- CodeForces - 1316D Nash Matrix(构造+dfs)
- POJ - 2516 Minimum Cost(最小费用最大流)
- 中石油训练赛 - 01 Matrix(构造)
- Jupyter Notebook导入自定义模块
- osqa java_从LSM-Tree、COLA-Tree谈到StackOverflow、OSQA(召唤前端)
- python扫描局公网ip_公网IP检测(python)
- java jfreechar鱼刺图_java使用jfreechar绘制饼型统计图
- python中strip是什么意思啊_Python中的strip()的理解
- 【数据结构】线性表的顺序存储结构(c语言实现)
- TCP keepalive的详解(解惑)