[LintCode] Longest Increasing Continuous Subsequence

news/2024/6/1 19:33:02

Problem 最长连续递增/递减子序列

Give an integer array,find the longest increasing continuous subsequence in this array.
An increasing continuous subsequence:
Can be from right to left or from left to right.
Indices of the integers in the subsequence should be continuous.

Example

For [5, 4, 2, 1, 3], the LICS is [5, 4, 2, 1], return 4.
For [5, 1, 2, 3, 4], the LICS is [1, 2, 3, 4], return 4.

Note

设置正向计数器,后一位增加则计数器加1,否则置1。反向计数器亦然。
每一次比较后将较大值存入max。

Solution

O(1) space, O(n) time

public class Solution {
    public int longestIncreasingContinuousSubsequence(int[] A) {
        if (A == null || A.length == 0) return 0;
        int n = A.length;
        int count = 1, countn = 1, max = 1;
        int i = 1;
        while (i != n) {
            if (A[i] >= A[i-1]) {
                count++;
                countn = 1;
                max = Math.max(max, count);
            }
            else {
                countn++;
                count = 1;
                max = Math.max(max, countn);
            }
            i++;
        }
        return max;
    }
}

DP using two dp arrays, O(n) space

public class Solution {
    public int longestIncreasingContinuousSubsequence(int[] A) {
        if (A == null || A.length == 0) return 0;
        int n = A.length;
        if (n == 1) return 1;
        int[] dp = new int[n];
        int[] pd = new int[n];
        int maxdp = 0, maxpd = 0;
        dp[0] = 1;
        for (int i = 1; i < n; i++) {
            dp[i] = A[i] >= A[i-1] ? dp[i-1]+1 : 1;
            maxdp = Math.max(maxdp, dp[i]);
        }
        pd[n-1] = 1;
        for (int i = n-2; i >= 0; i--) {
            pd[i] = A[i] >= A[i+1] ? pd[i+1]+1 : 1;
            maxpd = Math.max(maxpd, pd[i]);
        }
        return Math.max(maxdp, maxpd);
    }
}

DP using one dp arrays, O(n) space

public class Solution {
    public int longestIncreasingContinuousSubsequence(int[] A) {
        if (A == null || A.length == 0) return 0;
        int n = A.length;
        if (n == 1) return 1;
        int[] dp = new int[n];
        int maxdp = 0, maxpd = 0;
        dp[0] = 1;
        for (int i = 1; i < n; i++) {
            dp[i] = A[i] >= A[i-1] ? dp[i-1]+1 : 1;
            maxdp = Math.max(maxdp, dp[i]);
        }
        for (int i = 1; i < n; i++) {
            dp[i] = A[i] <= A[i-1] ? dp[i-1]+1 : 1;
            maxpd = Math.max(maxpd, dp[i]);
        }
        return Math.max(maxdp, maxpd);
    }
}

http://www.niftyadmin.cn/n/4256024.html

相关文章

【Docker基础】:Docker常用命令

Docker基础一、Docker是什么&#xff1f;二、相关网址三、安装四、Docker镜像4.1 父镜像4.2 基础镜像4.3 利用 Dockerfile 来创建镜像4.4、镜像的实现原理五、docker常用命令5.1、docker服务命令5.1.1、启动docker服务5.1.2、重启docker服务5.2、镜像操作命令5.2.1、从仓库获取…

virtualbox虚拟机只能安装32位不能安装64位linux系统的解决办法

virtualbox虚拟机只能安装32位不能安装64位linux系统的解决办法一、问题现象&#xff1a;一、出现不能安装的原因&#xff1a;二、解决办法1.进入bios2.切换到security页面&#xff0c;设置Virtualizaiton结语&#xff1a;设置完重启后&#xff0c;就解决了此问题&#xff0c;可…

bash:/build.sh:/bin/bash^M:bad interpreter:No such file or directory报错解决方法

原因&#xff1a;build.sh文件格式为dos格式导致 解决方法&#xff1a; 1. vi build.sh 2. :set ff 3. :set fileformatunix 4. :wq

***客户端出现“无法完成连接尝试”的解决方法

***客户端出现“无法完成连接尝试”的解决方法在Windows 7、Windows 8、Windows 10的客户端&#xff0c;在连接***服务器的时候&#xff0c;如果出现“无法完成连接尝试”&#xff0c;如图1-1所示。图1-1 无法完成连接尝试在图1-1中单击“详细信息”&#xff0c;此时会提示“服…

Linux复制出现^C的解决方法

鼠标左键拖动选中字段&#xff0c;松开鼠标后会自动出现^C,有时候正在执行脚本时因为这个操作会终止。。。 原因&#xff1a;后台打开了有道词典的缘故 解决方法&#xff1a; 把后台的有道词典关闭

seessionStorage兼容性

ie8是支持的&#xff0c;只是需要放到服务上端去测试转载于:https://www.cnblogs.com/AlexNull/p/5179222.html

curl: (35) SSL connect error报错,解决方法

curl: (35) SSL connect error报错 原因&#xff1a;无法在服务器使用curl命令访问https域名,nss版本有点旧  解决方法&#xff1a;&#xff08;更新nss使用下面的命令&#xff09; yum -y update nss

yum update出现报错:could not retrieve mirrorlist http://mirrorlist.centos.org的解决方法

yum update出现报错&#xff1a; could not retrieve mirrorlist http://mirrorlist.centos.org 原因&#xff1a;DNS配置错误 解决方法&#xff1a; 使用vi打开DNS的配置文件进行修改: vi /etc/sysconfig/network-scripts/ifcfg-eth0把文件中的下面两个字段配置和下面保持一…