问题

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3734 访问。

给定一个未经排序的整数数组,找到最长且连续的的递增序列。

输入: [1,3,5,4,7]

输出: 3

解释: 最长连续递增序列是 [1,3,5], 长度为3。尽管 [1,3,5,7] 也是升序的子序列, 但它不是连续的,因为5和7在原数组里被4隔开。

输入: [2,2,2,2,2]

输出: 1

解释: 最长连续递增序列是 [2], 长度为1。

注意:数组长度不会超过10000。


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

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.

Input: [2,2,2,2,2]

Output: 1

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

Note: Length of the array will not exceed 10,000.


示例

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3734 访问。

public class Program {

    public static void Main(string[] args) {
int[] nums = null; nums = new int[] { 1, 3, 5, 7 };
var res = FindLengthOfLCIS(nums);
Console.WriteLine(res); Console.ReadKey();
} private static int FindLengthOfLCIS(int[] nums) {
//没什么好说的,后面比前面大就计数,用max记录最大的连续的的递增序列
if(nums.Length == 0) return 0;
int count = 0, max = 0;
for(int i = 0; i < nums.Length - 1; i++) {
if(nums[i + 1] > nums[i]) {
count++;
} else {
max = Math.Max(max, count);
count = 0;
}
}
max = Math.Max(max, count);
return max + 1;
} }

以上给出1种算法实现,以下是这个案例的输出结果:

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3734 访问。

4

分析:

显而易见,以上算法的时间复杂度为:  。

最新文章

  1. Mybatis 源码分析--Configuration.xml配置文件加载到内存
  2. 安装Mysql提示1045错误解决方法
  3. [转]Composer 中国镜像
  4. Codeforces Round #115 A. Robot Bicorn Attack 暴力
  5. Linux 中查看网口流量的利器 -- sar
  6. IIS 10 安装URLRewrite组件 方式
  7. 常用 SQL 语句使用的总结
  8. BZOJ 3990: [SDOI2015]排序 [搜索]
  9. 152. Maximum Product Subarray(中等, 神奇的 swap)
  10. 4.5管道实现机制和模拟构建管道「深入浅出ASP.NET Core系列」
  11. Gitlab使用Webhook实现Push代码后的jenkins自动构建
  12. 记录一次.Net框架Bug发现和提交过程:.Net Framework和.Net Core均受影响
  13. .net core实现跨域
  14. QT中实现应用程序的单例化
  15. 【Android端ANR卡顿检测】BlockCanary检测
  16. Arduino和C51开发LCD1602显示屏
  17. javascript格式化json显示
  18. MyBatis_Study_004(动态代理)
  19. iis 部署 未在本地计算机上注册“Microsoft.Jet.OleDb.4.0”提供程序
  20. centos7 安装wxPython

热门文章

  1. SpringBoot代码生成器
  2. Spring Boot 2.x基础教程:EhCache缓存的使用
  3. [Qt2D绘图]-04绘制文字&amp;&amp;绘制路径
  4. Oracle基础概述
  5. 浅谈服务治理、微服务与Service Mesh(三) Service Mesh与Serverless
  6. 区间dp复习 之 乘积最大
  7. git配置httpd服务-web_dav模式
  8. 设计模式:singleton模式
  9. 什么是CSV
  10. 云小课|带你揭开IP地址的神秘身份