LintCode Longest Increasing Continuous Subsequence
原题链接在这里:http://www.lintcode.com/en/problem/longest-increasing-continuous-subsequence/
题目:
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.
Notice
O(n) time and O(1) extra space.
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
.
题解:
从左向右 若连续递增 就更新 len, 再从右向左 若连续递增 就更新res.
Time Complexity: O(n). Space: O(1).
AC Java:
1 public class Solution { 2 public int longestIncreasingContinuousSubsequence(int[] A) { 3 if(A == null || A.length == 0){ 4 return 0; 5 } 6 7 int res = 1; 8 int len = 1; 9 for(int i = 1; i<A.length; i++){ 10 if(A[i] > A[i-1]){ 11 len++; 12 }else{ 13 len = 1; 14 } 15 res = Math.max(res, len); 16 } 17 18 len = 1; 19 for(int i = A.length-2; i>=0; i--){ 20 if(A[i] > A[i+1]){ 21 len++; 22 }else{ 23 len = 1; 24 } 25 res = Math.max(res, len); 26 } 27 28 return res; 29 } 30 }
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server如何跟踪自动统计信息更新?
· AI与.NET技术实操系列:使用Catalyst进行自然语言处理
· 分享一个我遇到过的“量子力学”级别的BUG。
· Linux系列:如何调试 malloc 的底层源码
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· 几个技巧,教你去除文章的 AI 味!
· 系统高可用的 10 条军规
· 对象命名为何需要避免'-er'和'-or'后缀
· 关于普通程序员该如何参与AI学习的三个建议以及自己的实践
· AI与.NET技术实操系列(八):使用Catalyst进行自然语言处理