Max Subsequence

一个sequence,里面都是整数,求最长的subsequence的长度,使得这个subsquence的最大值和最小值相差不超过1. 比如[1,3,2,2,5,2,3,7]最长的subsequence是[3,2,2,2,3],所以应该返回5.

分析:

这题可以先排序,然后找出两个相差1的相邻值的最大长度。第二种方法可以用HashMap,保存每个值出现的个数,然后从当前值往下找比当前值大1的数出现的个数。

 1   public int maxSequence(int[] arr) {
 2         if (arr == null || arr.length == 0)
 3             return 0;
 4
 5         Map<Integer, Integer> map = new HashMap<>();
 6         for (int key : arr) {
 7             map.put(key, map.getOrDefault(key, 0) + 1);
 8         }
 9         int max = 0;
10         for (int key : arr) {
11             max = Math.max(max, map.get(key) + map.getOrDefault(key + 1, 0));
12         }
13         return max;
14     }
时间: 2024-10-15 07:46:33

Max Subsequence的相关文章

最大子序列和(Max Subsequence Sum)问题求解

给定(可能有负的)整数A1,A2,-,AN,求Ai,-,Aj和的最大值. 例如:输入4,-3,5,-2,-1,2,6,-2,最大子序列和为11(从A1到A7). 算法1:最直观的算法,穷举式地尝试所有可能.下标变量i表示子序列的开始位置,j表示结束位置,每次选定一个子序列Ai--Aj,再使用k遍历该子序列求子序列的和.代码如下: public static int maxSubSum1(int a[]){ ????????int maxSum=0; ???????? ????????for(in

寒假 9(max subsequence sum二分递归算法实现并debug,表的链表实现概念过程整理)

二分递归实现过程收获: 一个取max的函数,核心是我brute的排序函数: 递归啊,如果结果出错,检查的时候查具体步骤,递归这个指令没什么好检查的: 遍布每个得出结果的关键点的输出测试: 因为一开始把right设成了array length,后面出现了str[length],有随机错误: 声明为int的小数,编译器直接不足近似处理为整数. 某处加一个break point,左边就可以看运行信息 表的链表实现概念梳理: 用链表实现的表,没有固定的位置编号,仅可以从value上识别,寻找,一个ele

CLRS:Max_sunsequence_sum

#include<stdio.h>#include<stdlib.h>#include<time.h>#define ARRAY_SIZE 1000int buf [ARRAY_SIZE];int main(){ srand((unsigned int )time(0)); int i,j,n; while(scanf("%d",&n)!=EOF) { for(i=1;i<=n;i++)buf[i]=rand()%100-10; for

3.子数组的最大和

http://zhedahht.blog.163.com/blog/static/254111742007219147591/ http://blog.csdn.net/v_JULY_v/article/details/6444021 输入一个整形数组,数组里有正数也有负数.数组中连续的一个或多个整数组成一个子数组,每个子数组都有一个和.求所有子数组的和的最大值,要求时间复杂度为O(n). 例如输入的数组为1, -2, 3, 10, -4, 7, 2, -5,和最大的子数组为3, 10, -4,

POJ 2533 - Longest Ordered Subsequence(最长上升子序列) 题解

此文为博主原创题解,转载时请通知博主,并把原文链接放在正文醒目位置. 题目链接:http://poj.org/problem?id=2533 Description A numeric sequence of ai is ordered if a1 < a2 < ... < aN. Let the subsequence of the given numeric sequence (a1, a2, ..., aN) be any sequence (ai1, ai2, ..., aiK)

Longest Common Subsequence

Problem statement: Given two strings, find the longest common subsequence (LCS). Your code should return the length of LCS. Have you met this question in a real interview? Yes Clarification What's the definition of Longest Common Subsequence? https:/

ZOJ3349——Special Subsequence

Special Subsequence Time Limit: 5 Seconds      Memory Limit: 32768 KB There a sequence S with n integers , and A is a special subsequence thatsatisfies |Ai-Ai-1| <= d ( 0 <i<=|A|)) Now your task is to find the longest special subsequence of a cer

HDU 1003 Max Sum

题目: Problem Description Given a sequence a[1],a[2],a[3]......a[n], your job is to calculate the max sum of a sub-sequence. For example, given (6,-1,5,4,-7), the max sum in this sequence is 6 + (-1) + 5 + 4 = 14. Input The first line of the input cont

Common Subsequence

A subsequence of a given sequence is the given sequence with some elements (possible none) left out. Given a sequence X = < x1, x2, ..., xm > another sequence Z = < z1, z2, ..., zk > is a subsequence of X if there exists a strictly increasing