Longest Continuous Increasing Subsequence
Description
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
Example 1:
Input: [5, 4, 2, 1, 3]
Output: 4
Explanation:
For [5, 4, 2, 1, 3], the LICS is [5, 4, 2, 1], return 4.
Example 2:
Input: [5, 1, 2, 3, 4]
Output: 4
Explanation:
For [5, 1, 2, 3, 4], the LICS is [1, 2, 3, 4], return 4.
思路:本题为直接去找一个数组的最长连续上升/下降子序列,直接从左往右枚举即可,每次到单调性变化的时候更新答案。
public class Solution {
public int longestIncreasingContinuousSubsequence(int[] A) {
if (A == null || A.length == 0) {
return 0;
}
int n = A.length;
int answer = 1;
// from left to right
int length = 1; // just A[0] itself
for (int i = 1; i < n; i++) {
if (A[i] > A[i - 1]) {
length++;
} else {
length = 1;
}
answer = Math.max(answer, length);
}
// from right to left
length = 1;
for (int i = n - 2; i >= 0; i--) {
if (A[i] > A[i + 1]) {
length++;
} else {
length = 1;
}
answer = Math.max(answer, length);
}
return answer;
}
}
Challenge
O(n) time and O(1) extra space.
Longest Continuous Increasing Subsequence的更多相关文章
- LeetCode 674. Longest Continuous Increasing Subsequence (最长连续递增序列)
Given an unsorted array of integers, find the length of longest continuous increasing subsequence. E ...
- [LeetCode] Longest Continuous Increasing Subsequence 最长连续递增序列
Given an unsorted array of integers, find the length of longest continuous increasing subsequence. E ...
- [Swift]LeetCode674. 最长连续递增序列 | Longest Continuous Increasing Subsequence
Given an unsorted array of integers, find the length of longest continuous increasing subsequence (s ...
- [Leetcode]674. Longest Continuous Increasing Subsequence
Given an unsorted array of integers, find the length of longest continuous increasing subsequence. E ...
- [LeetCode&Python] Problem 674. Longest Continuous Increasing Subsequence
Given an unsorted array of integers, find the length of longest continuousincreasing subsequence (su ...
- leetcode300. Longest Increasing Subsequence 最长递增子序列 、674. Longest Continuous Increasing Subsequence
Longest Increasing Subsequence 最长递增子序列 子序列不是数组中连续的数. dp表达的意思是以i结尾的最长子序列,而不是前i个数字的最长子序列. 初始化是dp所有的都为1 ...
- 674. Longest Continuous Increasing Subsequence最长连续递增子数组
[抄题]: Given an unsorted array of integers, find the length of longest continuous increasing subseque ...
- LeetCode Longest Continuous Increasing Subsequence
原题链接在这里:https://leetcode.com/problems/longest-continuous-increasing-subsequence/description/ 题目: Giv ...
- 674. Longest Continuous Increasing Subsequence@python
Given an unsorted array of integers, find the length of longest continuous increasing subsequence (s ...
- LeetCode 674. Longest Continuous Increasing Subsequence最长连续递增序列 (C++/Java)
题目: Given an unsorted array of integers, find the length of longest continuous increasing subsequenc ...
随机推荐
- [转帖]UCloud上市:利润暴跌84%、成本居高不下,结构化调整迫在眉睫
UCloud上市:利润暴跌84%.成本居高不下,结构化调整迫在眉睫 https://www.iyiou.com/p/116317.html [ 亿欧导读 ] 日前,上交所科创板上市委召开第27 ...
- 【转帖】linux date 显示指定时区的时间 借助TZ 环境变量 export TZ=Asia/Shanghai 或 America/New_York
linux date 显示指定时区的时间 借助TZ 环境变量 export TZ=Asia/Shanghai 或 America/New_York 2015-02-10 10:58:22 youcha ...
- (二)spring初次遇见shiro
文章目录 集成 Spring 集成中的坑 shiroFilter 的工作原理 权限配置细节 集成 Spring pom.xml 添加shiro相关的依赖 我使用的版本是 ${version.shiro ...
- Ubuntu 软件卸载脚本(卸载软件 + 移除配置文件 + 移除依赖项)
#!/bin/bash function z-apt-uninstall() { if [ ! $1 ] then echo "z-apt-uninstall error: software ...
- 剑指offer41:所有和为S的连续正数序列,例如,有多少种连续的正数序列的和为100
1 题目描述 小明很喜欢数学,有一天他在做数学作业时,要求计算出9~16的和,他马上就写出了正确答案是100.但是他并不满足于此,他在想究竟有多少种连续的正数序列的和为100(至少包括两个数).没多久 ...
- HCIA SWITCHING&ROUTTING 笔记——第一章 TCP/IP基础知识(1)
视频地址:https://ilearningx.huawei.com/courses/course-v1:HuaweiX+EBGTC00000336+Self-paced/courseware/abb ...
- 【Qt】Qt5.12编译MySQl5.7驱动(亲自测试成功)
目录 00. 目录 01. 安装Qt5.12 02. 打开MySQL源码项目 03. 编译MySQL驱动代码 04. 修改mysql.pro文件 05. 编译之后得到对应的库 06. 拷贝动态库到指定 ...
- 1.VBA Excel宏
Excel VBA宏 在这一章中,让我们了解如何编写一个简单的宏.让我们一步一步来. 第1步:首先,让我们能够在Excel20XX'开发'菜单.做同样的,点击 File >> Option ...
- 稀疏检出-使用git检索出仓库里的某一个目录文件,而不是整个仓库的所有文件
具体工作意义是从某一个Git仓库 克隆时,只克隆检测出这个仓库里的某些文件夹内容,而不是跟平常那样把整个仓库的内容都克隆下来 从1.7.0版本开始git提供稀疏检出的功能.所谓稀疏检出就是本地版本库检 ...
- Web Scraper 翻页——控制链接批量抓取数据(Web Scraper 高级用法)| 简易数据分析 05
这是简易数据分析系列的第 5 篇文章. 上篇文章我们爬取了豆瓣电影 TOP250 前 25 个电影的数据,今天我们就要在原来的 Web Scraper 配置上做一些小改动,让爬虫把 250 条电影数据 ...