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 ...
随机推荐
- ProFTPD <=1.3.5 mod_copy 未授权文件复制漏洞
poc如下: #!/usr/bin/env python# coding=utf-8 """Site: http://www.beebeeto.com/Framework ...
- phpcms v9 0day
index.php?m=member&c=index&a=login 后缀 username=phpcms&password=123456%26username%3d%2527 ...
- MYTOP安装和使用
安装 mytop 1. 在 /etc/yum.repo.d 新建一个文件 21andy.com.repo [21Andy.com] name=21Andy.com Packages for Enter ...
- 解决安装VS2013提示“已停止工作”问题
新安装操作系统(win8.1),手动安装各种驱动,安装VS2013,报错,见下图: 原因:显卡驱动问题. 解决办法:卸载intel显卡驱动这碧池.(系统会自动给你适配合适的)
- Reachability(判断网络是否连接)
类似于一个网络状况的探针. [[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(reachabili ...
- jsp应用
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding= ...
- 【8-21】java学习笔记03
内部类(静态内部类&非静态内部类) 静态外部类成员方法(如main方法)不能直接访问内部类,但是可以通过外部类的方法,在其中创建内部类实例对象,间接使用: 非静态内部类可以直接访问外部类的私有 ...
- Linux里startup.sh 和 shutdown.sh
最近用socket编写了一个服务端程序,监听1024端口,检测客户端发来的请求,所在Linux里写启动和停止的脚本: 在Eclipse里java写好程序,右击导出生成 Runnable JAR fil ...
- Javascript 中我很想说说的 this
this是每一个想要深入学习Javascript的人必过的一关,我为this看过很多书查过很多资料,虽然对this有了一定的了解并且也经常使用this,但是如果有人问我 this是什么呀? 我依旧不 ...
- sql事务和锁
摘自:http://www.cnblogs.com/lxconan/archive/2011/10/20/sql_transaction_n_locks_1.html 最近在项目中进行压力测试遇到了数 ...