博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
674 Longest Continuous Increasing Subsequence
阅读量:7029 次
发布时间:2019-06-28

本文共 1377 字,大约阅读时间需要 4 分钟。

Given an unsorted array of integers, find the length of longest continuous increasing subsequence.

Example 1:

Input: [1,3,5,4,7] Output: 3 Explanation: The longest continuous increasing subsequence is [1,3,5], its length is 3. Even though [1,3,5,7] is also an increasing subsequence, it's not a continuous one where 5 and 7 are separated by 4.

Example 2:

Input: [2,2,2,2,2] Output: 1 Explanation: The longest continuous increasing subsequence is [2], its length is 1.


这题挺好的,我想到两种方法,一种brute force,O(n2)不好;另一种,看到LIS就思维定势地想到了DP。

DP:

我写的leetcode submissions没保存,摘抄一个别人的:

public int findLengthOfLCIS(int[] nums) {        if (nums == null || nums.length == 0) return 0;        int n = nums.length;        int[] dp = new int[n];                int max = 1;        dp[0] = 1;        for (int i = 1; i < n; i++) {            if (nums[i] > nums[i - 1]) {                dp[i] = dp[i - 1] + 1;            }            else {                dp[i] = 1;            }            max = Math.max(max, dp[i]);        }                return max;    }复制代码

但其实这题有更好的方法,

public int findLengthOfLCIS(int[] nums) {        int res = 0, cnt = 0;        for(int i = 0; i < nums.length; i++){            if(i == 0 || nums[i-1] < nums[i]) res = Math.max(res, ++cnt);            else cnt = 1;        }        return res;    }复制代码

用O(1)记录某个值然后随着滚动而重置,这种思想。

转载于:https://juejin.im/post/5a3133ff6fb9a0451238f352

你可能感兴趣的文章
Eclipse里maven的project报Unbound classpath variable: 'M2_REPO/**/***/***.jar
查看>>
新旅程CSS 基础篇分享一
查看>>
查看内核函数调用的调试方法【原创】
查看>>
个人项目中遇到的问题
查看>>
byte与base64string的相互转化以及加密算法
查看>>
20145103 《Java程序设计》第3周学习总结
查看>>
ubuntu声音系统
查看>>
哈哈更新资源列表2
查看>>
冲刺第一周第五天
查看>>
Java 接口
查看>>
Android 微信第三方登录
查看>>
硬盘的读写原理
查看>>
实例 centos自动挂载、备份windows共享文件夹,并删除第7日前当天的备份
查看>>
LNMP下动静分离部署phpmyadmin软件包
查看>>
如何写更好的自动化测试用例
查看>>
60再谈指针
查看>>
repost
查看>>
android异步加载AsyncTask
查看>>
GCC Stack-Smashing Protector
查看>>
虚拟机Visualbox安装Ubuntu Server
查看>>