Max Subsequence
一个sequence,里面都是整数,求最长的subsequence的长度,使得这个subsquence的最大值和最小值相差不超过1. 比如[1,3,2,2,5,2,3,7]最长的subsequence是[3,2,2,2,3],所以应该返回5.
分析:
这题可以先排序,然后找出两个相差1的相邻值的最大长度。第二种方法可以用HashMap,保存每个值出现的个数,然后从当前值往下找比当前值大1的数出现的个数。
public int maxSequence(int[] arr) {
if (arr == null || arr.length == )
return ;
Map<Integer, Integer> map = new HashMap<>();
for (int key : arr) {
map.put(key, map.getOrDefault(key, ) + );
}
int max = ;
for (int key : arr) {
max = Math.max(max, map.get(key) + map.getOrDefault(key + , ));
}
return max;
}
Max Subsequence的更多相关文章
- 寻找最大连续子序列/Find the max contiguous subsequence
寻找最大连续子序列 给定一个实数序列X1,X2,...Xn(不需要是正数),寻找一个(连续的)子序列Xi,Xi+1,...Xj,使得其数值之和在所有的连续子序列数值之和中为最大. 一般称这个子序列为最 ...
- Solutions for the Maximum Subsequence Sum Problem
The maximum subarray problem is the task of finding the contiguous subarray within a one-dimensional ...
- 3.子数组的最大和[MaximumContinuousSubArray]
[题目]: 输入一个整形数组,数组里有正数也有负数.数组中连续的一个或多个整数组成一个子数组,每个子数组都有一个和.求所有子数组的和的最大值,要求时间复杂度为O(n). 例如输入的数组为1, -2, ...
- CLRS:Max_sunsequence_sum O(n*n) O(nlgn) O(n)
#include<stdio.h>#include<stdlib.h>#include<time.h>#define ARRAY_SIZE 1000int buf ...
- [LeetCode] Wiggle Subsequence 摆动子序列
A sequence of numbers is called a wiggle sequence if the differences between successive numbers stri ...
- [LeetCode] Increasing Triplet Subsequence 递增的三元子序列
Given an unsorted array return whether an increasing subsequence of length 3 exists or not in the ar ...
- [LeetCode] Longest Increasing Subsequence 最长递增子序列
Given an unsorted array of integers, find the length of longest increasing subsequence. For example, ...
- 动态规划求最长公共子序列(Longest Common Subsequence, LCS)
1. 问题描述 子串应该比较好理解,至于什么是子序列,这里给出一个例子:有两个母串 cnblogs belong 比如序列bo, bg, lg在母串cnblogs与belong中都出现过并且出现顺序与 ...
- UVA 11404 Palindromic Subsequence[DP LCS 打印]
UVA - 11404 Palindromic Subsequence 题意:一个字符串,删去0个或多个字符,输出字典序最小且最长的回文字符串 不要求路径区间DP都可以做 然而要字典序最小 倒过来求L ...
随机推荐
- thinkphp 3.2 视图模型 实例 视图查询结果 二维数组 合并
使用视图模型查询的时候 结果是这样的 array(6) { [0] => array(5) { ["picTitle"] => string(7) "标题2& ...
- WebGrid Helper with Check All Checkboxes
WebGrid Helper with Check All Checkboxes myEvernote Link Tuesday, September 13, 2011ASP.NET ASP.NET ...
- 在Linux主机上搭建SVN,用于同步提交修改,实现本地提交线上预览(SVN Hook功能实现)
原文地址: http://blog.csdn.net/ROVAST/article/details/44887707?ref=myread 注:上文中钩子使用中有错误,正确的使用如下,上文中忘记了up ...
- CentOS更改yum源与更新系统
[1] 首先备份/etc/yum.repos.d/CentOS-Base.repo mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/Cent ...
- Cucumber命令行接口
1. cucumber的命令行选项 首先查看命令行选项.和其它命令行工具一样,cucumber提供了—help选项.下面是cucumber帮助的一个缩减版本: $ cucumber --help -r ...
- MySql集群FAQ----mysql主从配置与集群区别、集群中需要多少台计算机呢?为什么? 等
抽取一部分显示在这里,如下, What's the difference in using Clustervs using replication? 在复制系统中,一个MySQL主服务器会更新一个或多 ...
- Makefile-2
一.命令行参数 -n/--just-print/--dry-run/--recon 只显示命令,但不会执行命令,用于调试 makefile. -s/--slient/--quiet 禁止命令的显示 ...
- 2015年12月01日 GitHub入门学习(二)手把手教你Git安装
序:Mac与Linux中,Mac都预装了Git,各版本的Linux也都提供了Git的软件包.下面手把手教你Windows下的安装. 一.Git Windows GUI 下载地址 msysgit htt ...
- [译]Probable C# 6.0 features illustrated
原文: http://damieng.com/blog/2013/12/09/probable-c-6-0-features-illustrated ========================= ...
- jquery 使用textarea
问题: 若在textarea标签中键入一个回车时,将插入2个字符\n\r ,将在datagrid不能正确显示. \n: 回车符 \r: 换行符 解决方案: txt = txt.Replace(&qu ...